dprichard Posted September 20, 2007 Share Posted September 20, 2007 I have two select boxes on the same page both filled dynamically and am using a do / while loop to put the information in. The first one works fine, but the second one is blank. I can't figure out why the second box shows nothing if it works on the first one. First Box Code: <select name="emp_econtact_pphone_type" id="emp_econtact_pphone_type"> <option>Choose One</option> <?php do { ?> <option value="<?php echo $row_phone['ptid']; ?>"><?php echo $row_phone['ptname']; ?></option> <?php } while ($row_phone = mysql_fetch_array($phone)) ?> </select> Second Box Code: <select name="emp_econtact2_pphone_type" id="emp_econtact2_pphone_type"> <option>Choose One</option> <?php do { ?> <option value="<?php echo $row_phone['ptid']; ?>"><?php echo $row_phone['ptname']; ?></option> <?php } while ($row_phone = mysql_fetch_array($phone)) ?> </select> Any assistance would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/70001-2-select-box-filled-dynamically-with-do-while-loop/ Share on other sites More sharing options...
redarrow Posted September 20, 2007 Share Posted September 20, 2007 assoc <<<< not array....... Quote Link to comment https://forums.phpfreaks.com/topic/70001-2-select-box-filled-dynamically-with-do-while-loop/#findComment-351588 Share on other sites More sharing options...
dprichard Posted September 20, 2007 Author Share Posted September 20, 2007 Do I need to use mysql_fetch_assoc in my query as well as in each do / while loop? I tried changing it to assoc with the same result. I have 4 of those boxes total on the page, and only the first one shows any results from the database. Quote Link to comment https://forums.phpfreaks.com/topic/70001-2-select-box-filled-dynamically-with-do-while-loop/#findComment-351610 Share on other sites More sharing options...
dprichard Posted September 20, 2007 Author Share Posted September 20, 2007 Okay, so i found out with mysql_data_seek, I can reset the count to the first record like this: <?php mysql_data_seek(($phone), 0); ?> The only other issue I have is that the secondary boxes all have a blank row then the rest of the records. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/70001-2-select-box-filled-dynamically-with-do-while-loop/#findComment-351723 Share on other sites More sharing options...
Psycho Posted September 20, 2007 Share Posted September 20, 2007 Why loop through the list twice? Just loop through the list once creating a variable of the select options and then create the two select lists: <?php $phone_options = "<option>Choose One</option>\n"; while ($row_phone = mysql_fetch_array($phone)) { $phone_options . = "<option value=\"$row_phone['ptid']\">$row_phone['ptname']</option>\n"; } ?> <select name="emp_econtact_pphone_type" id="emp_econtact_pphone_type"> <?php echo $phone_options; ?> </select> <select name="emp_econtact2_pphone_type" id="emp_econtact2_pphone_type"> <?php echo $phone_options; ?> </select> isn't that simpler? Never create two sets of code to do the exact same thing if at all possible. Eventually you will edit one and not the other and have inconsistencies. Quote Link to comment https://forums.phpfreaks.com/topic/70001-2-select-box-filled-dynamically-with-do-while-loop/#findComment-351730 Share on other sites More sharing options...
sasa Posted September 20, 2007 Share Posted September 20, 2007 or insert line mysql_data_seek($phine, 0); $row_phone = mysql_fetch_array($phone); before 2nd loop Quote Link to comment https://forums.phpfreaks.com/topic/70001-2-select-box-filled-dynamically-with-do-while-loop/#findComment-351759 Share on other sites More sharing options...
Psycho Posted September 20, 2007 Share Posted September 20, 2007 or insert line mysql_data_seek($phine, 0); $row_phone = mysql_fetch_array($phone); before 2nd loop Because, it is bad practice to have two sets of code doing the same thing when you want the results to be the same. You should have one function/process and utilize that. If he was to want a slight modification to the value for those options in the future it would be very easy to fix one list and overlook the other. By creating the list with one pass and reusing it you are ensured the list will be exactly the same between the two fields. Quote Link to comment https://forums.phpfreaks.com/topic/70001-2-select-box-filled-dynamically-with-do-while-loop/#findComment-351832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.