Connecting Tech Pros Worldwide Help | Site Map

MySQL results into SELECT drop down alternative?

Newbie
 
Join Date: Nov 2006
Location: Kanata, ON
Posts: 5
#1: Nov 22 '06
I'm about to create a form that will be using a sql query array that i'd like to put into drop down list.... 28 times in one page. Is there an alternative to building the same 28 sql queries?

eg, i'd like to avoid having to run so many queries in one page with the following code 28 times.... or is it completely healthy?

[PHP]
<SELECT>
<?php

include 'includes/db.inc';
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
$SQL = " SELECT home,homenum FROM ohcn_homes ORDER BY home ";
$retid = mysql_db_query($db, $SQL, $cid);
if (!$retid) { echo( mysql_error()); }
else {

while ($row = mysql_fetch_array($retid))
{
/* display menu items */
echo '<OPTION VALUE="' . $row[homenum] . '">' . $row[home] . "</OPTION>";
}
}
?>
</SELECT>
[/PHP]

Thoughts, suggestions?
All help welcome... Thanks!
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Nov 22 '06

re: MySQL results into SELECT drop down alternative?


I am not sure I understand the problem, but I'll try. You want to build 28 drop-down lists from 28 dynamic different queries on the database. And you don't want to write 28 different functions in your code. Right?

You can handle this by having your queries stored in an array, so you can pull them one by one. Actually it is a 2-dimensional array, so you can also store the text shown before the drop down box and the name of the select. The latter so you can handle the responses via the name of the select box.

I assume that you use the same database for each query! Otherwise you have to store the dbname in the array also. Example of your code (sorry for the make-up changes but I like structured code):[php]
<?php
$queries = array(
array('text' => "Select home",
'query' => "SELECT home,homenum FROM ohcn_homes ORDER BY home",
'selname' => "homes"),
array('text' => "Select myhome",
'query' => "SELECT myhome,myhomenum FROM my_homes ORDER BY myhome",
'selname' => "myhomes"),
array('text' => "Select col1",
'query' => "SELECT col1, col2 FROM other_table ORDER BY col1",
'selname' => "cols")
);
include 'includes/db.inc';
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) {
echo("Server connection failed : " . mysql_error() . "\n");
}
mysql_connect_db($db, $cid) {
or die("Connect to db failed: " . mysql_error());
}
echo '<html><head><title>Select test</title></head><body>';
// -----------------------------------------------------------
// loop through $SQL array, select data and make dropdown list
// -----------------------------------------------------------
for ($i=0; $i < count($queries); $i++) {
echo "<label style='width:100px;'>{$queries[$i]['text']}</label><select style='width:100px;' name='" . $queries[$i]['selname'] . "'>";
$retid = mysql_query($queries[$i]['query']);
if (!$retid) {
die("Select failed on: {$queries[$i]['query']} MySQL error=" . mysql_error());
} // End IF
else {
while ($row = mysql_fetch_array($retid)) {
/* display menu items */
echo "<OPTION VALUE='{$row[0]}'>{$row[1]}</OPTION>";
} // End WHILE
echo '</select><br />';
}
} // end FOR
echo '</body></html>';
?>[/php]Ronald :cool:
Newbie
 
Join Date: Nov 2006
Location: Kanata, ON
Posts: 5
#3: Nov 28 '06

re: MySQL results into SELECT drop down alternative?


Thanks ronverdonk!

I was looking for an array type solution, so i will certainly give this a shot.

Stewy
Reply