Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
OK, total noob here trying to teach himself PHP/mySQL by reading books
and playing with code from the web and from trying to write increasingly
difficult (for me) code. Here's what I'm trying to do:

I have a db with a few tables. I have managed to write a simple query to
one of these tables that then displays all the fields of each record
that matches the query. Now I want to take it one step further, I want
to create a drop down selection box where the user can select any of
these records. I managed to create the box, but can only display one
field of the records (so if the fields were firstname and lastname, I
have only figured out how to show one of the two in the dropdown).

Any hints would be appreciated,or pointers to good web resources for

Here's the query code. I have deleted the form part as it is crap and
I'd like to see how it is properly done.
$query  = "SELECT ID, first, last FROM master WHERE fhl is null";
$result = mysql_query($query) or die('Error, query failed');

$tsv  = array();
$html = array();

while($row = mysql_fetch_array($result, MYSQL_NUM))
    $tsv[]  = implode("\t", $row);
    $html[] = "<tr><td>" .implode("</td><td>", $row) .

$tsv = implode("\r\n", $tsv);
$html = "<table>" . implode("\r\n", $html) . "</table>";

Re: dropdowns

finbogey jones wrote:
Quoted text here. Click to load it

Well, first of all I don't just implode the data; I prefer to access the  
individual columns myself.

But you can do something like:

   <select name="myselect">
     while ($row = mysql_fetch_array($result)) {
       echo "<option value=$row['id']>" . $row[firstname] . " "  
.$row[lastname] . "</option>\n";
Of course you'll need to adjust based on your particular column names in  
your database.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: dropdowns

Jerry Stuckle wrote:
Quoted text here. Click to load it
Thanks for the info, I'll try that out tonight.

Re: dropdowns

Except that it is the form bit we need :)

you have generated the HTML for the <FORM method=\"post\"
action=\"somepage.php\"> and for the <SELECT value=\"recordID\">....

You now use the object / array to generate the option bit of the code.

$HTML = "<OPTION value=\"$result['value']\"> $result['Fname']


and then end the </SELECT >

You will now see in the HTML

<OPTION value="17">Fred Smith</OPTION>
<OPTION value="18">John Jones</OPTION>
<OPTION value="19">Peter Parker</OPTION>

Basically whatever you put between <OPTION> and </OPTION> will show in
the drop down list but the value is what gets passed back to the server
as part of the form submit and will end up in $_POST['recordID']

Hope this helps

Re: dropdowns

obiron wrote:
Quoted text here. Click to load it

Thank you very much. I now have the form looking right and am moving on  
to the page that takes the value and changes the db with the selection.


Re: dropdowns

glad to be of help and thankyou for saying thankyou.. Buts lets leave
it there or we could be going at it for a week.

Hopefully if the not quite noobees help the noobees, the xperts will
help the not quite noobees and the gurus will help the xperts.  that
way everyone gets to contribute and we all get anwers

Site Timeline