473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

dropdowns

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>";
Dec 5 '06 #1
5 1448
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.
js*******@attglobal.net
==================
Dec 6 '06 #2
Jerry Stuckle wrote:
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.
Thanks for the info, I'll try that out tonight.

Dec 6 '06 #3
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']
$result['Sname']</OPTION>";

PRINT $HTML;

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 <OPTIONand </OPTIONwill 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

Dec 7 '06 #4
obiron wrote:
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']
$result['Sname']</OPTION>";

PRINT $HTML;

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 <OPTIONand </OPTIONwill 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
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.
fin
Dec 8 '06 #5
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

Dec 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Crescionini Sascha | last post by:
Hello NG If I let appear a DIV over a HTML site with dropdowns, this dropdowns are still visible. Is there a way to push them to the back? All other form elements arent visible. greets...
0
by: Jeffrey | last post by:
Hi... I have several dropdowns located in a datagrid footer. Now say i modify the selection in dropdown1. I want to refresh the content of the other dropdowns with the result of a database...
3
by: Damon | last post by:
I am working on a site in which I would like to have two dropdowns that will allow a user to navigate through the administrative pages of the site. The first would allow the user to choose the...
1
by: Andy | last post by:
What I want to do is to populate multiple dropdowns when editing. Presumably... a) I should use a DataReader so that I can get each ResultSet for each dropdown control, and that should cut down...
2
by: bingomanatee | last post by:
I have developed what amounts to a fancy shopping cart wizard for a scientific instrument using VB.NET. We are having some disturbing phenomena relating to dropdown controls. On my system and...
0
by: nkparimi | last post by:
Hi, Here's what I'm trying: given an html table, to freeze the column headers and/or left column in IE. I understand that this is possible thru style sheets in IE, as suggested in the following:...
3
by: Simon Harvey | last post by:
Hi everyone, I keep getting a problem with dropdownlist controls. It sounds really stupid, but my app is screwed as long as this keeps happening. It seems to spontaneously happen and then I...
1
by: Stimp | last post by:
I have 3 dropdowns: country, county and district. I'm using ajax to dynamically populate the county and district dropdowns when country dropdown is changed (and similarly the district dropdown...
0
by: bogorman | last post by:
Am trying to add a "video" to a webpage which is based on a template containing a javascript menu. The site has hundreds of pages all based on this template and the menu works fine The page can be...
0
by: sbart | last post by:
I am programing in asp.net vb using AjaxToolkit cascadingDropDown. I do not have access to AjaxToolkit cascading dropdown event handlers. I have a series of dropdowns. The selection from one of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.