Re: newbie: ye ol' nemesis using mysql_fetch_array to load data

From: Date: Thu, 08 Mar 2001 23:47:34 +0000
Subject: Re: newbie: ye ol' nemesis using mysql_fetch_array to load data
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-43090@lists.php.net to get a copy of this message
At 3:37 PM -0800 3/8/01, Nicole Lallande wrote:
$len = mysql_num_rows($result); for ($i=0; $i<=$len; $i++) { echo "<option value=\"$catid[$i]\">$catid</option>"; }
I believe your problem is in that code becuase it's ambiguous and because $catid is an array and you're treating it as a regular variable. what you should do is something like:
       while ($row = mysql_fetch_array($result)) {
         $rowid[] = $row["dnaProd_ID"];
         $catid[] = $row["Cat_ID"];
         $desc[] = $row["Description"];
         $size[] = $row["Pkg_size"];
         $price[] = $row["Price"];
       }
for($i = 0; $i < sizeof($catid); $i++) { $cat = $catid[$i]; echo "<option value=\"$cat\">$cat</option>"; } or you could build this up during your first loop: $optionValues = "";
       while ($row = mysql_fetch_array($result)) {
$cat = $row["Cat_ID"]; $optionValues .= "<option value=\"$cat\">$cat</option>\n";
       }
then just echo $optionValues whenever you want. OR you can create a generic function you can use anytime you want to make an option list, just pass in an array of options: GenerateOptionValues($optionArr) { $string = ""; foreach($optionArr as $option} { $string .= "<option value=\"$option\">$option</option>\n"; } return $string; } or if you need different values from the options, pass in an associateive array: GenerateOptionValues($optionArr) { $string = ""; foreach($optionArr as $option => $value} { $string .= "<option value=\"$value\">$option</option>\n"; } return $string; } alright, sorry for going on and on. -aaron

« previous php.general (#43090) next »