Jump to content

Drop Down from sql table


nashsaint

Recommended Posts

Hi,  I wanted to populate a drop down menu from sql table but my code doesn't work.. help please.

 

 

Here's my Code:

<select name="select">';
     // default is 0, no entry will be selected.
     db_createlist($linkID,0,"select pull_id, pc from job_log_pull","Please select one ...");

</select>

 

 

 

And here's the function:

function db_createlist($linkID,$default,$query,$blank)
{
    if($blank)
    {
        print("<option select value=\"0\">$blank</option>");
    }

    $resultID = pg_exec($linkID,$query);
    $num       = pg_numrows($resultID);

    for ($i=0;$i<$num;$i++)
    {
        $row = pg_fetch_row($resultID,$i);

        if($row[0]==$default)$dtext = "selected";
        else $dtext = "";

        print("<option $dtext value=\"$row[0]\">$row[1]</option>");
    }
}

Link to comment
Share on other sites

use a while loop.

 

$q = mysql_query() or die(mysql_error());
echo "<option value=\"blank\">Please select.</option>";
while ($r = mysql_fetch_array($q)) {
echo "<option value=\"" . $r['id'] . "\">" . $r['name'] . "</option>";
}

Link to comment
Share on other sites

I tried unidox code but I couldn't get it to work.  I tried another approach and it seems to be working but now I don't know how to capture the user's selection from the select, since this form is under another Form statement.

Any help how to extract the user selection?

 

Thanks.

                        $pcquery = "SELECT pull_id, pc FROM job_log_pull";
                        $pcresult = @mysql_query($pcquery);
                        echo '<form action=blah method=Post>
                        <select name=category>';
                            // get resultset as object
                            while ($pcrow = mysql_fetch_object($pcresult))
                                {
                            echo "<option value=" . $pcrow->pull_id . ">" . $pcrow->pc .  "</option><br>";
                                }
                        echo '</select>
                        </form>';
                        mysql_free_result($pcresult);

Link to comment
Share on other sites

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.