|
Posted by Jerry Stuckle on 12/06/06 03:36
finbogey jones wrote:
> 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
> examples.
>
> 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) .
> "</td></tr>";
> }
>
> $tsv = implode("\r\n", $tsv);
> $html = "<table>" . implode("\r\n", $html) . "</table>";
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">
<?php
while ($row = mysql_fetch_array($result)) {
echo "<option value=$row['id']>" . $row[firstname] . " "
..$row[lastname] . "</option>\n";
?>
</select>
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.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|