visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Populate drop down list from multi dimential array?


By: ChrisCobb U.S.A.  Date: 09/06/2003 00:00:00  English French  Points: 50 Status: Answered
Quality : Excellent
I have a table containing two columns (country and country_id) and I want to read the list from the DB and display the country list within a drop down list, with the associated country_id being posted from the form.

I'm lost! Basically I need advice how to:
(1) Assign values to a multi dimensional array from values read from a DB
(2) Using multi dimensional array in a foreach loop

This is as far as I've got:

<form name="QuickSearchForm" id="QuickSearchForm" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<?
$sql = "SELECT * FROM countries";
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result)) {
$CountryArray[][] = $myrow["country_id"] . $myrow["country"]; // HELP!!
}

sort($CountryArray);
echo "Country:<select name=country>";
foreach($CountryArray as $option){ // HELP!!
echo "<option value=$option_id>$option</option>"; // HELP!!
}
echo "</select>
";
?>
<input type="Submit" name="submit" value="Search">
</form>
By: sumotimor Date: 09/06/2003 01:57:00 English  Type : Comment
Here is a handy function I use for select lists generated from database values. When performing the query, the first field is the option value, and the second field is the display.

<?php
echo ("Country: <select name=\"country\">");
sql2option("select country_id, country FROM countries ORDER BY country", $_REQUEST['country']);
echo ("</select>");
echo ("<input type=\"submit\" value=\"Search\">");

function sql2option($sql, $selected=null) {
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)) {
$selected = ($row[0] == $selected) ? " SELECTED" : "";
echo "<option value=\"" . $row[0] . "\" $selected>" . $row[1] . "</option>\n";
}
}
?>
By: VGR Date: 09/06/2003 02:03:00 English  Type : Assist
groumph &reg;

<?php
$PHP_SELF=$_SERVER['PHP_SELF'];
// here connect to DB
$sql = "SELECT * FROM countries ORDER BY country ASC;";
$result = mysql_query($sql) or die(mysql_error()); // of else you simply can't write the while below
$CountryArray=array(); // initialization !
while ($myrow = mysql_fetch_array($result)) {
$CountryArray[] = array('country_id'=>$myrow["country_id"], 'country'=>$myrow["country"]);
} // while
// completely useless if you use properly the DB sort($CountryArray);
// now display FORM
echo <<<EOS
<form name="QuickSearchForm" id="QuickSearchForm" method="post" action="$PHP_SELF">
Country:<select name=country>
EOS;
foreach($CountryArray as $index=>$value){
echo "<option value={$value['country_id']}>{$value['country']}</option>";
} // foreach
echo <<<EOS
</select>

<input type="Submit" name="submit" value="Search">
</form>
EOS;
?>
By: SeCuRi[T] Date: 09/06/2003 02:14:00 English  Type : Answer
I changed your original concept, I ain't using arrays anymore... I directly paste the <option> tag, which is much easier. The ORDER BY is made directly in the query, instead of sorting the array like you were doing. Hope this helps :)

/******** BOF ********\

<form name="QuickSearchForm" id="QuickSearchForm" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<?php
$sql = "SELECT * FROM countries ORDER BY country";
$result = mysql_query($sql);

echo '<select name="country" size="1">';

while ($myrow = mysql_fetch_array($result))
{
echo '<option value="'.$myrow["country_id"].'">'.$myrow["country"].'</option>';
}
?>
</select>
<br />
<input type="Submit" name="submit" value="Search">
</form>

/******** EOF ********\

Best regards, SeCuRi[T]
By: VGR Date: 09/06/2003 02:29:00 English French  Type : Comment
les grands esprits se rencontrent ;-)

Absolutely the best solution, except if you prefer (or have to) separate the DB processing from the place where you display the SELECT OPTIONs, or if you need the countries (and their IDs) for an other purpose, or later on. You would do multiple times the same time-consuming DB job.

but SecurIt, for security ;-) you should not write this :

$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result))

what happens if $result is "an invalid resource" because mysql_query() failed ? Hum ? ;-)
By: SeCuRi[T] Date: 09/06/2003 02:35:00 English French  Type : Comment
lol You speak french I see :) Eh bien moi aussi :)

then i guess i'd just add an if around the mysql_query :)

<form name="QuickSearchForm" id="QuickSearchForm" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<?php
$sql = "SELECT * FROM couries ORDER BY contry";
if ($result = mysql_query($sql))
{
echo '<select name="country" size="1">';

while ($myrow = mysql_fetch_array($result))
{
echo '<option value="'.$myrow["country_id"].'">'.$myrow["country"].'</option>';
}

?>

</select>
<br />
<input type="Submit" name="submit" value="Search">
<?php
}
else
{
echo 'Error...';
}
?>
</form>
By: SeCuRi[T] Date: 09/06/2003 02:43:00 French  Type : Comment
Et alors, qui gagne la confrontation des grands esprits?
By: VGR Date: 09/06/2003 02:44:00 French  Type : Comment
celui qui sera choisi par le poseur de "la" Question ;-)
By: RQuadling Date: 10/06/2003 05:58:00 English  Type : Comment
Hey! Play fair. English please.
By: VGR Date: 10/06/2003 18:30:00 English  Type : Comment
nothing to do with the beef :-)
By: ChrisCobb Date: 12/06/2003 19:45:00 English  Type : Comment
Had to split the points - great minds indeed! Thanks to:

VGR for answering my original question and showing how I can use multi dimensional arrays.

SeCuRi[T] for re-implementing my idea in the way I should have approached it in the first place!

Cheers, -Chris

Do register to be able to answer

EContact
browser fav
page generated in 335.917000 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page