简体   繁体   中英

html/mysql echo query results into table?

I am using the following MySQL query to select data from my database and echo the results in a table.

My problem is the first set of results is being echoed correctly, however the next line is showing my results as vertical down the page rather than horizontal.

Here's what's happening:

Heading1      Heading2      Heading3      Heading4     Heading5
a             b             c             d            e
a
b
c
d
e

Please can someone show me where I am going wrong. Here's my code:

    <?php 
$conn = new mysqli($host, $username, $password, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); } 
$sql = "select * from new_supplier_request where status!= 'complete' and action_taken ='actioned'";
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {
            echo '<table><tr><td><p><u>Request By</u></p></td><td><p><u>Date</u></p></td><td><p><u>Status</u></p></td><td><p><u>Supplier Name</u></p></td><td><p><u>Action</u></p></td></tr>';

            while($row = $result->fetch_assoc()) { 

        $datetime = strtotime($row['date']);
        $mysqldate = date("D, d M Y ", $datetime);

        echo '<tr>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p><a href="process/action.php?reference='.$row['reference'].'" id="action">Action</a>&nbsp;/&nbsp;<a href="process/decline.php?reference='.$row['reference'].'" id="decline">Decline</a></p></td></tr>';
}
echo '</table>';

            }else{

                echo'<div class="no_requests">No New Supplier Request&#39;s</div>';


            }  ?>

You close the table in the loop.The first set of results is echoed correctly because it's still in the first loop and it'll output how you want it, but because of the closing table, the other results are messed up. The </table> tag is in the loop but the <table> isn't so the </table> isn't even closing anything. Meaning the rest of your results aren't actually in a table. You need to put your </table> out of the loop.

while($row = $result->fetch_assoc()) { 

        $datetime = strtotime($row['date']);
        $mysqldate = date("D, d M Y ", $datetime);

        echo '<tr>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p><a href="process/action.php?reference='.$row['reference'].'" id="action">Action</a>&nbsp;/&nbsp;<a href="process/decline.php?reference='.$row['reference'].'" id="decline">Decline</a></p></td></tr>';
}
echo '</table>';

also what's with the random <?php above your while?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM