Jump to content

Creating a Function to prevent repeated code? Noob needs help.


brendanmoore

Recommended Posts

Hello, This is my first post and I thank you all for taking the time to read my problem and help if you can.

 

I am creating an online database of two-way radio equipment for a company, I'm using MySQL and PHP (obviously) with a little help from dreamweaver now, I have several <select> drop down list's that dynamically fill with various data from the Database. Thats pretty easy I have done it with this code.

 

	$makeUnique = array(); //creates array
	do {  
		$makeUnique[].=$row_all_radioProducts['RadMan']; //fills array with Data from the MySQL RadMan = Radio Manufacturer
	} 
		while ($row_all_radioProducts = mysql_fetch_assoc($all_radioProducts)); //fills array until all data is entered
  				$makeUnique=array_unique($makeUnique);	//makes all the data unique
  				natcasesort($makeUnique);				//sorts it alphabetically
  				foreach ($makeUnique as $value) {		//for ever item in the unique array 
  					echo "<option value=" . "\"" . $value ."\"";
				if($value == $check_RadMan) {		//another piece of code earlier checks to see if that Radio manufacturer is already selected
					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);

  }}

 

This code sits in between the <select> and </select> tags and works fine. The problem is I have 7 select box's with all that code repeated with the exception of

$makeUnique[].=$row_all_radioProducts['RadMan'];

which i just change the 'RadMan' to 'RadType' or 'RadPrice' etc etc. so can I make this into a simple function? and just pass that only variable that changes? I'm sure you can but i dont know how to do it.

function print_Rad ($parameter) {
/*your code*/
switch ($parameter) {    //parameter should contain one of the 3 values
  case 'RadMan' : $makeUnique[].=$row_all_radioProducts['RadMan'];
             break;
  case 'RadType' : $makeUnique[].=$row_all_radioProducts['RadType']; 
              break;
  case 'RadPrice' : $makeUnique[].=$row_all_radioProducts['RadPrice'];
             break;
}
/*your code*/
}

 

function print_Rad ($parameter) {
/*your code*/
switch ($parameter) {    //parameter should contain one of the 3 values
  case 'RadMan' : $makeUnique[].=$row_all_radioProducts['RadMan'];
             break;
  case 'RadType' : $makeUnique[].=$row_all_radioProducts['RadType']; 
              break;
  case 'RadPrice' : $makeUnique[].=$row_all_radioProducts['RadPrice'];
             break;
}
/*your code*/
}

 

Thanks for the reply.

Now that i wrapped this code in a function I'm getting a MySQL error relating to the

while ($row_all_radioProducts = mysql_fetch_assoc($all_radioProducts));

line

 

Any ideas?

I have made a bit of progress:

 

I have created this function:

 


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);
  }}
}

 

This is <select> code.

 

<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 fills in one result only:

 

<option value="Motorola">Motorola</option>

 

using ALL the code within the <select> tags (not as a seperate function) produces ALL (three) results.

Why isnt this function repeating?

I actually get a MySQL error:

 

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

 

this is this line:

 

while ($row_all_radioProducts = mysql_fetch_assoc($all_radioProducts));

 

Don't know why?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.