brendanmoore Posted October 24, 2008 Share Posted October 24, 2008 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. Link to comment https://forums.phpfreaks.com/topic/129930-creating-a-function-to-prevent-repeated-code-noob-needs-help/ Share on other sites More sharing options...
feidakila Posted October 24, 2008 Share Posted October 24, 2008 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*/ } Link to comment https://forums.phpfreaks.com/topic/129930-creating-a-function-to-prevent-repeated-code-noob-needs-help/#findComment-673570 Share on other sites More sharing options...
brendanmoore Posted October 24, 2008 Author Share Posted October 24, 2008 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? Link to comment https://forums.phpfreaks.com/topic/129930-creating-a-function-to-prevent-repeated-code-noob-needs-help/#findComment-673574 Share on other sites More sharing options...
brendanmoore Posted October 24, 2008 Author Share Posted October 24, 2008 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? Link to comment https://forums.phpfreaks.com/topic/129930-creating-a-function-to-prevent-repeated-code-noob-needs-help/#findComment-673590 Share on other sites More sharing options...
brendanmoore Posted October 24, 2008 Author Share Posted October 24, 2008 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? Link to comment https://forums.phpfreaks.com/topic/129930-creating-a-function-to-prevent-repeated-code-noob-needs-help/#findComment-673616 Share on other sites More sharing options...
prexep Posted October 24, 2008 Share Posted October 24, 2008 What is $all_radioProducts value? Link to comment https://forums.phpfreaks.com/topic/129930-creating-a-function-to-prevent-repeated-code-noob-needs-help/#findComment-673622 Share on other sites More sharing options...
brendanmoore Posted October 24, 2008 Author Share Posted October 24, 2008 What is $all_radioProducts value? Its mysql_query($query_all_radioProducts, $pce_exeter_com) or die(mysql_error()) and $query_all_radioProducts = "SELECT * FROM PCEradio ORDER BY PCEref ASC"; Link to comment https://forums.phpfreaks.com/topic/129930-creating-a-function-to-prevent-repeated-code-noob-needs-help/#findComment-673626 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.