padams Posted August 28, 2007 Share Posted August 28, 2007 I've created a recordset and want to use it to populate the contents of multiple drop-down menus. To do this once is easy enough, but the number of drop-down menus is created dynamically based on a number entered on a previous page. I've done a 'do while' loop to create all of the drop-downs, and nested another 'do while' loop inside the <SELECT> field to populate the contents from the recordset. However, only one drop-down menu populates, the others stay blank. I've run into similar problems before, and it seems that I can't use the same recordset more than once. Is that correct, or have I made a fundamental mistake? Code to create drop-downs and populate them ($trycount is the number entered on a previous page): <?php $count = 1; do { ?> Try <?php echo $count; ?>: <select name="try<?php echo $i; ?>"> <?php do { ?> <option value="<?php echo $rstrylists['playerID']; ?>"><?php echo $rstrylists['playerFirstName']; ?> <?php echo $rstrylists['playerLastName']; ?></option> <?php } while ($rstrylists = mysql_fetch_assoc($trylists_query)) ?></select> <?php $count++; } while ($count <= $trycount) ?> Quote Link to comment https://forums.phpfreaks.com/topic/67141-solved-using-same-recordset-multiple-times/ Share on other sites More sharing options...
roopurt18 Posted August 28, 2007 Share Posted August 28, 2007 mysql_fetch_assoc is updating an internal record pointer so that after the first full loop is finished, it will appear empty. Check out this function: http://www.php.net/manual/en/function.mysql-data-seek.php As a side note, if your result sets are large you will find that looping over it many times is slow an inefficient. You should try and organize your code so that you loop over it only once. Quote Link to comment https://forums.phpfreaks.com/topic/67141-solved-using-same-recordset-multiple-times/#findComment-336731 Share on other sites More sharing options...
padams Posted August 29, 2007 Author Share Posted August 29, 2007 Great, thanks. That did it. I used mysql_data_Seek in the middle of one of my 'do while' loops to resend the pointer to the start of the recordset. Thanks for the help. <?php $count = 1; do { ?> Try <?php echo $count; ?>: <select name="try<?php echo $count; ?>"> <?php do { ?> <option value="<?php echo $rstrylists['playerID']; ?>"><?php echo $rstrylists['playerFirstName']; ?> <?php echo $rstrylists['playerLastName']; ?></option> <?php } while ($rstrylists = mysql_fetch_assoc($trylists_query)) ?></select><br> <?php $count++; mysql_data_seek($trylists_query, 0); <------------------ } while ($count <= $trycount) ?> Quote Link to comment https://forums.phpfreaks.com/topic/67141-solved-using-same-recordset-multiple-times/#findComment-336846 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.