473,395 Members | 1,653 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,395 software developers and data experts.

MySQL results into SELECT drop down alternative?

5
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!
Nov 22 '06 #1
2 3785
ronverdonk
4,258 Expert 4TB
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:
Nov 22 '06 #2
stewy
5
Thanks ronverdonk!

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

Stewy
Nov 28 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: JDJones | last post by:
Using PHP and MySQL. Trying to put a list of categories into a drop down select option of a form like: <form name="form" action="<? print $_SERVER?>" method="get"> <select name="subject">...
0
by: Neal | last post by:
I have a simple survey with a drop down that lets users select different depts in our organization. Displaying the results to a different page works fine, but I would like to create another, same...
4
by: Ka | last post by:
I install a mysql server in default installation with latin charset, but I want to use GBK(a chinese charset), so that I can store and search chinese words directly. so, I download, unpack and...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
2
by: Boujii | last post by:
Greetings, I have been attempting to make a drop down menu of countries. From this menu I wish to create a variable in order to INPUT into mysql database. I have no trouble making the drop down menu,...
7
by: dongletran06 | last post by:
Hi, Please help me find out what wrong with my codes in inputting from my form to mysql database using drop down menu. Below is the codes I used. Only the drop down is not working but the "input...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
2
by: trochia | last post by:
Hello all, I am fairly new to php etc, and I have a database 1) I have already did a search for: "Results within results" on this site, in PHP & MySQL forums ( I think) properly...and one search...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.