Page 1 of 1

[Newbie] Code does not work as Function. Help!

Posted: Fri Oct 24, 2008 6:08 am
by brendanmoore
Hello I have created some code to Fill a HTML select box from MySQL data, the DATA is already loaded into an array this code makes it unique, sorts it and prints it. when this not used as a function it works fine. My problem is a have 7 select boxes and i have the code written 7 times, with only two variable names changed. So i intend to optimise into a function but I'm stuck I cannot get it to work. This is the function.

Code: Select all

function fillInSelect($si){
 
$makeUnique = array();
do {  
    $makeUnique[].=$si;
  
} 
while ($row_all_radioProducts = mysql_fetch_assoc($all_radioProducts));
  $makeUnique=array_unique($makeUnique);
  natcasesort($makeUnique);
  foreach ($makeUnique as $value) {
  echo "<option value=" . "\"" . $value ."\"";
  if($value == $check_RadMan){echo " selected=\"selected\"";}
  echo ">" . $value ."</option>\n";
  $rows = mysql_num_rows($all_radioProducts);
  if($rows > 0) {
      mysql_data_seek($all_radioProducts, 0);
      $row_all_radioProducts = mysql_fetch_assoc($all_radioProducts);
  }}
}
and this is the select box code:

Code: Select all

 <select name="selectRadMan" id="selectRadMan">
        <option value="ALL" <?php if($check_RadMan == "ALL"){echo "selected=\"selected\"";} ?>>Manufacturer (All)</option>
        <?php fillInSelect($row_all_radioProducts['RadMan']); ?>
            </select>
This only outputs 1 result, when there should be 5 (for this 'RadMan') so its clearly not looping but i do not understand why. Please Help.

Thanks in advance. Brendan

Re: [Newbie] Code does not work as Function. Help!

Posted: Fri Oct 24, 2008 6:29 am
by aceconcepts
On line 5 you do not need to use a decimal point to append the value, the square brackets "[]" do this for you as it acts as an array:

Code: Select all

 
//change
$makeUnique[].=$si;
 
//to
$makeUnique[]=$si;
 

Re: [Newbie] Code does not work as Function. Help!

Posted: Fri Oct 24, 2008 6:41 am
by brendanmoore
Changes made thanks. But still does not work
I have turned on PHP errors to try and resolve this. I get this:

Code: Select all

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in radio.php line 144


line 144 refers to line 8 in the first code block in the first post. It must refer to the while loop and thats why its not working.

Re: [Newbie] Code does not work as Function. Help!

Posted: Fri Oct 24, 2008 6:44 am
by onion2k
You haven't passed $all_radioProducts to the function, so as far as the function is concerned that variable is not set.

Re: [Newbie] Code does not work as Function. Help!

Posted: Fri Oct 24, 2008 6:56 am
by brendanmoore
onion2k wrote:You haven't passed $all_radioProducts to the function, so as far as the function is concerned that variable is not set.
the $all_radioProducts is a mysql_query() if i set that in the function i get a memory full error and the page does not load.

also tried something like this and have the same problem

Code: Select all

function fillInSelect($si,$sg){
 
$makeUnique = array();
do {  
    $makeUnique[].=$si;
  
} 
while ($row_all_radioProducts = $sg));
  $makeUnique=array_unique($makeUnique);
  natcasesort($makeUnique);
  foreach ($makeUnique as $value) {
  echo "<option value=" . "\"" . $value ."\"";
  if($value == $check_RadMan){echo " selected=\"selected\"";}
  echo ">" . $value ."</option>\n";
  $rows = mysql_num_rows($all_radioProducts);
  if($rows > 0) {
      mysql_data_seek($all_radioProducts, 0);
      $row_all_radioProducts = mysql_fetch_assoc($all_radioProducts);
  }}
}
and this

Code: Select all

 <select name="selectRadMan" id="selectRadMan">
        <option value="ALL" <?php if($check_RadMan == "ALL"){echo "selected=\"selected\"";} ?>>Manufacturer (All)</option>
        <?php fillInSelect($row_all_radioProducts['RadMan'],mysql_fetch_assoc($all_radioProducts); ?>
            </select>
I think I have a general view of what i'm doing wrong however my knowledge is poor to say the least... does this function have to Ask MySQL for data everytime its called, i thought that once the data is loaded in to a PHP array it can be used many times?