1

I'm trying to fetch mySQL data into html table with vertical header. My html table code is in the following code :

<table border="1" align="center">
<thead>
    <tr>
        <th>No.</th>
        <?php  
            while ($row1 = mysqli_fetch_assoc($q)) {
                echo "<td>$no</td>";
                $no++;
            }
        ?>
    </tr>
    <tr>
        <th>Term</th>
        <?php  
            while ($row2 = mysqli_fetch_assoc($q)) {
                echo "<td>".$row2['term']."</td>";
            }
        ?>
    </tr>
    <tr>
        <th>Normalisasi TF.IDF</th>
        <?php  
            while ($row3 = mysqli_fetch_assoc($q)) {
                echo "<td>".$row3['tf_idf_normalisasi']."</td>";
            }
        ?>
    </tr>
</thead>

Here's screenshot of my table : enter image description here

But mySQL data are not displayed on 2nd and 3rd <tr>. What is wrong with my code ? Can someone tell me how to make it work ? Thank you.

1 Answer 1

2

Generate td elements HTML with a single loop otherwise mysqli_fetch_assoc method returns NULL in second and third loop since you are already fetched all rows.

<?php  
    $td1 = '';
    $td2 = '';
    $td3 = '';
    while ($row = mysqli_fetch_assoc($q)) {
        $td1 .= "<td>$no</td>";
        $td2 .= "<td>" . $row['term'] . "</td>";
        $td3 .= "<td>" . $row['tf_idf_normalisasi'] . "</td>";
        $no++;
    }
?>

<table border="1" align="center">
<thead>
    <tr>
        <th>No.</th>
        <?php echo $td1; ?>
    </tr>
    <tr>
        <th>Term</th>
        <?php echo $td2; ?>
    </tr>
    <tr>
        <th>Normalisasi TF.IDF</th>
        <?php echo $td3; ?>
    </tr>
</thead>
Sign up to request clarification or add additional context in comments.

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.