Jump to content

Recommended Posts

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) ?>

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.

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) ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.