Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT</strong> Killed that other stuff now that you have code up.</p> <pre><code>// Lets make our table2 the primary so we can organize our values in the $items array. $sql = "SELECT table1.id as table1id, table1.name, table2.id as table2id, table2.email FROM table2 LEFT JOIN table1 ON table2.emailid = table1.id"; // Do you need this WHERE? I'm thinking not. // WHERE table1.id = {$id}"; $items = array(); while($row = mysqli_fetch_array($result)) { // This will get set every time through the loop, but this is ok. // You can check to see if its set and then set or not set. $items[$row['table1id']]['name'] = $row['name']; // We will keep appending email values here to the table1id array. $items[$row['table1id']]['emails'][] = $row['email']; } </code></pre> <p>Lets do the HTML markup now that we have all the items in a nice array for us.</p> <pre><code>&lt;?php foreach($items as $id =&gt; $item): ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $item['name']; ?&gt;&lt;/td&gt; // Do you really need this hidden field? &lt;td&gt;&lt;input type="hidden" name="table1id" value="&lt;?php echo $id; ?&gt;" /&gt;&lt;?php echo $id; ?&gt;&lt;/td&gt; &lt;td&gt; // Updated this to be unique for each id iteration. &lt;select name="email_&lt;?php echo $id; ?&gt;"&gt; // Might want to consider a is_array() check on this value. &lt;?php foreach ($item['email'] as $email): ?&gt; &lt;?php $selected = $email == $_POST['email_'. $id] ? ' selected="selected"' : ''; ?&gt; &lt;option value="&lt;?php echo $email; ?&gt;"&lt;?php echo $selected; ?&gt;&gt; &lt;?php echo $email; ?&gt;&lt;/option&gt; &lt;?php endforeach; ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php endforeach; ?&gt; </code></pre> <p>I have not tested this so it may not work as a copy and paste. But I think this is can get you going correctly now.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload