Jump to content

2 Select Box Filled Dynamically with Do While Loop


dprichard

Recommended Posts

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.