Jump to content

Populating a dropdown from SQL Server db is not populating?


kelechi

Recommended Posts

 <?php
     $tsql = "SELECT bids.BidType +'[' + CAST(COUNT(*) as varchar)+ ']' solicitationName
FROM bids
   inner JOIN status ON bids.Bidstatus = status.statusid where status.status='Open'
GROUP BY bids.BidType
UNION
SELECT status.status +'[' + CAST(COUNT(*) as varchar)+ ']' solicitationName
FROM bids
   INNER JOIN status ON bids.Bidstatus = status.statusid where status.status='Open'
GROUP BY status.status
UNION
SELECT status.status +'[' + CAST(COUNT(*) as varchar)+ ']' solicitationName
FROM bids
   INNER JOIN status ON bids.Bidstatus = status.statusid
GROUP BY status.status";
     $stmt = sqlsrv_query( $conn, $tsql);
     if( $stmt === false )
     {
      echo "Error in executing query.</br>";
      die( print_r( sqlsrv_errors(), true));
     }
      while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){
       echo '<option value="' . $row['solicitationName'] . '" name="' . $row['solicitationName']. '">' . $row['solicitationName']. '</option>';
     }
  ?>
 </select>

Hello gurus,

 

I am querying sql server database and trying to dynamically populate the values into a dropdown.

So far, I don't even see a dropdown, let alone the values.

 

Any ideas?

 

 

I don't see an opening <select> tag in your code. Plus, option tags don't have a name attribute. Also, it is better to build (the necessary) html in your code and output it later. This helps to separate the logic from the output

 

 

<?php
    $tsql = "SELECT bids.BidType +'[' + CAST(COUNT(*) as varchar)+ ']' solicitationName
             FROM bids
             INNER JOIN status ON bids.Bidstatus = status.statusid where status.status='Open'
             GROUP BY bids.BidType
               UNION
             SELECT status.status +'[' + CAST(COUNT(*) as varchar)+ ']' solicitationName
             FROM bids
             INNER JOIN status ON bids.Bidstatus = status.statusid where status.status='Open'
             GROUP BY status.status
               UNION
             SELECT status.status +'[' + CAST(COUNT(*) as varchar)+ ']' solicitationName
             FROM bids
             INNER JOIN status ON bids.Bidstatus = status.statusid
             GROUP BY status.status";
    $stmt = sqlsrv_query( $conn, $tsql);
    if( $stmt === false )
    {
        echo "Error in executing query.</br>";
        die( print_r( sqlsrv_errors(), true));
    }
    $solicitationOptions = '';
    while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
    {
       $solicitationOptions .= "<option value=\"{$row['solicitationName']}\">{$row['solicitationName']}</option>\n";
    }
 
 //Perform ALL logic needed for the page - then output the HTML
  ?>
 <html>
 <head></head>
 <body>
 
<select name="">
    <?php echo solicitationOptions; ?> 
</select>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.