0

I have two drops down and I am able to display the records in one dropdown from a database. I am getting the second drop down list bank.I have to display same records in my second drop down but it is not displaying. If I delete first drop down then records are displaying in second drop down.I haven't change any think just copy paste the code.Would you help me in this?

  <?php
    $sql_emails="SELECT * FROM request";
    $result_emails = $conn->query($sql_emails);

    //first drop down 
  ?>
    <select class="selectpicker" name="first" data-live-search="true">
          <option style="color:#bbb;" value="" disabled selected>Select emails</option>
        <?php while($row = mysqli_fetch_array($result_emails)) { ?>
               <option value="<?php echo $row['Email'];?>"><?php echo $row['Email'];?></option>
        <?php } ?>
    </select>
    <?php mysqli_data_seek( $result_emails, 0 );  ?>
    <select class="selectpicker" name="second" data-live-search="true" id="learner-emails">
        <option style="color:#bbb;" value="" disabled selected>Select emails</option>
        <?php while($row = mysqli_fetch_array($result_emails)){ ?>
        <option value="<?php echo $row['Email'];?>"><?php echo $row['Email'];?></option>
        <?php } ?>
    </select>
1
  • Thanks for reply Mr.Amit, added name. Commented Dec 30, 2016 at 5:45

3 Answers 3

2
mysqli_data_seek( $result_emails, 0 ); use this before second dropdown
Sign up to request clarification or add additional context in comments.

4 Comments

Cool! simple and easy.Thanks for help Mr.Nishant Saini
@NarendraVerma any time please marked it as an answer
Mr.Nishant, Can you explain me this and should I used your code every time if I need a result more than one drop down?
you can achieve this with two ways 1. mysqli_data_seek() function second 2. save data in array when you are getting it in first time. and use that array according to your requirement. In first case you have to restart query pointer from the position 0 , use has to use mysqli_data_seek() function
0

Put the rows into an array at the beginning of your script.

$rows = [];
while($row = mysqli_fetch_array($result_emails))
    $rows[] = $row;

Then, on the html sections, loop over the array. You can loop over it as many times as you want. You can't do that with the query results.

<?php foreach($rows as $row):;?>
<option value="<?php echo $row['Email'];?>"><?php echo $row['Email'];?></option>
<?php endfor;?>

Comments

0

mysqli_fetch_array() moves the pointer forward each time you call it. You need mysqli_data_seek() to set the pointer back to the start and then call mysqli_fetch_array() again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.