Fetch MySql data into "; – rahul Mar 02 '16 at 04:37

  • why are you trying to double fetch it? you already got it inside your container – Kevin Mar 02 '16 at 04:39
  • There are so many red flags in this code i don't know where to start. Please read PHP manual a little more. I wanted to write a detailed answer, but then I realized it won't help you, because you are not yet familiar with basic language syntax, and i can't teach you all of it here. – Muhammed Mar 02 '16 at 04:40
  • 3 Answers3

    1

    No need to fetch it over again. You've already done that here:

    $uname = array();
    while ($rows = mysqli_fetch_array($uresult)) {
        $uname[] = $rows['name'];
    }
    

    So inside the other while block, just use that names that already gathered:

    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
            echo "<td align='center'>" . $row['emp_no']. "</td>";
            echo "<td align='center'>" . $row['emp_name']. "</td>";
            echo "<td align='center'>" . $row['task'] . "</td>";
            echo "
            <td align='center'>
                <select name='select1'>
            ";
            foreach($uname as $names) {
                echo '<option>' . $names . '</option>';
            }
            echo "
                </select>
            </td>";
        echo "</tr>";
    }
    

    Or just create an HTML string of <options> with the $uname, then just echo it inside the while:

    $uname = '';
    while ($rows = mysqli_fetch_array($uresult)) {
        $uname .= '<option>' . $rows['name'] . '</option>';
    }
    

    Then on the while block:

    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
            echo "<td align='center'>" . $row['emp_no']. "</td>";
            echo "<td align='center'>" . $row['emp_name']. "</td>";
            echo "<td align='center'>" . $row['task'] . "</td>";
            echo "
            <td align='center'>
                <select name='select1'>
            ";
    
            echo $uname; // collection of HTML string options
    
            echo "
                </select>
            </td>";
        echo "</tr>";
    }
    
    Kevin
    • 41,694
    • 12
    • 53
    • 70
    • this is what i meant by pre populating select. But why make that foreach run inside while, run it outside once, in while() you can have a constant string. – Muhammed Mar 02 '16 at 04:46
    • @MuhammedM. yup i already made the edit – Kevin Mar 02 '16 at 04:47
    • Thank you. And I am strong opponent of echo's inside while, it slows down performance, decreases readability. I prefer assigning values to a dummy variable inside loop and outputing it after the loop ($out.="..") – Muhammed Mar 02 '16 at 04:49
    • Thank You i got the required result.I know i am beginner & learning it – Fresher Mar 02 '16 at 06:10
    1

    Error is with below line you have php code inside the string

    echo "<td align='center'> <select name='select1'> <?php while ($rows=mysqli_fetch_array($uresult)) { ?><option><?php echo $rows['name'];?> </option><?php } ?> </select> </td>";
    
    Shyam Achuthan
    • 910
    • 5
    • 8
    0

    Try like this, embedding php in html

    while($row = mysqli_fetch_array($result))
    {
        ?>
        <tr>
        <td align='center'><?php echo $row['emp_no'] ?></td>
        <td align='center'><?php echo $row['emp_name'] ?></td>
        <td align='center'><?php echo $row['task'] ?></td>
        <td align='center'> <select name='select1'> 
        <?php while ($rows=mysqli_fetch_array($uresult)) { ?>
        <option><?php echo $rows['name'];?> </option><?php } ?> 
        </select> </td>
        </tr>";
        <?php 
    }
    
    rahul
    • 841
    • 1
    • 8
    • 18
    • $uresult=mysqli_query($conn,"SELECT name from user where role='Support'"); ---> if select is going to populate the same result, why put it inside loop? store it outside and pass it as a variable to this while. Very bad code indeed, by the author, i didn't mean you, since you are just trying to provide a quick fix. – Muhammed Mar 02 '16 at 04:42
    • Thats upto the questnr to put it wherever he wants, but one thing sure is he wants to loop SELECT * from dummy statement in the table rows and SELECT name from user where role='Support' in the select option – rahul Mar 02 '16 at 04:44