Jump to content

Populating a dropdown from SQL Server db is not populating?


Go to solution Solved by Psycho,

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?

 

 

  • Solution

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>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.