1

I am new to php, I would like to generate the same select drop down list box. This is the code I have, but it only works for the first drop down, not for the second one...

This is the code I am using without any success :

<?php 
 while($row = mysqli_fetch_array($result))  
      {  
           $output .= '  
                <tr>  
                     <td>'.$row["id"].'</td>  
     				 <td class="last_name"  >
					 <select id="t1"><option>'.$row["t1"].'</option>';			
							 while($rowcode=mysqli_fetch_array($resulttasks))
									{ $output .= '<option value="'.$rowcode["id"].'">'.$rowcode["code"].'</option>' ; }
							 $output .='</select>
					 </td>  
					 <td class="last_name"  >
					 <select id="t2"><option>'.$row["t2"].'</option>';			
							 while($rowcode=mysqli_fetch_array($resulttasks))
									{ $output .= '<option value="'.$rowcode["id"].'">'.$rowcode["code"].'</option>'; }
							 $output .='</select>
					 </td>  
			</tr>';
?>

Thanks in advance for the help !

2
  • id="t1" ID will not repeat use different IDs or use class here. Commented Apr 10, 2019 at 10:36
  • 1
    before the second loop of "t2", try putting mysqli_data_seek($resulttasks,0); Commented Apr 10, 2019 at 10:39

3 Answers 3

2

Duplicate identifiers is not the issue, before the second loop of "t2" select, try putting mysqli_data_seek($resulttasks,0);

Explanation: duplicate identifiers are only for DOM and it has nothing to do with parsing HTML in PHP. If you guys notice, he is using the same result set two times, PHP does not run another loop on same data once process already. So, we have to reset to 0th row if we want to run another loop on the same result set.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot ! can you tell me why I have to do this ?
You can check my comment above in the first answer, and if my solution works for you then do some favor :).
0

The id attribute must be unique, replace the duplicate ones by common classes :

<select id="t1">

Must be :

<select class="t1">

So when you loop through the $result you'll not generate duplicate identifiers.

5 Comments

I think it's already unique <select id="t1"> & <select id="t2"> or am i mistaken ?
@drb, u are using loop
Inside a foreach, it will be duplicated if the foreach will loop 3 times we'll end up with 3 duplicate id's.
duplicate identifiers are only for DOM and it has nothing to do with parsing HTML in PHP. If you guys notice, he is using the same result set two times, PHP does not run another loop on same data once process already. So, we have to reset to 0th row if we want to run another loop on the same result set.
Good catch @DanishHakimKhan +1
0

hope this helps you change your id with loop example

$index = 1;
    for($i = 0; $i < $count_array; $i ++) {
        echo '<select id="t1' . $index . '"></select>';
        $index ++;
    }

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.