Search code examples
phpjqueryhtmljquery-pagination

JQuery Table didn't show any value from PHP


I've been struggling for weeks just to analyze the problem of my code, the JQuery Pagination table didn't show the PHP value. I thought it would be PHP that causes the problem.

<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>No.</th>
            <th>Nama Mitra</th>
            <th>Jenis Kelamin</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        //I think the problem starts here
        while ($rowmitra = mysqli_fetch_array($mitra)) {
            echo '<tr>';
            echo "<th scope='row'>" . $i . "</th>";
            echo "<td>" . $rowmitra['id_mitra'] . "</td>";
            echo "<td>" . $i . "</td>";
            echo "<td>" . $rowmitra['mitra'] . "</td>";
            echo '<td><img width="200px" class="shadow-sm" src="image/logo/' . $rowmitra["logo"] . '"></td>';
            echo '<td><a href="edit-mitra.php?id=' . $rowmitra["id_mitra"] . '"><button>EDIT</button></a></td>';
            echo "</tr>";
            $i = $i + 1;
        }
        ?>
    </tbody>
</table>

And here's what I do to fetch Mitra table

$mitra = $koneksi->query("SELECT * FROM `mitra`");
$rowmitra = mysqli_fetch_array($mitra);

I put it in a different file that I use include 'head.php'; command to merge both. When I fetch it in outside while command it works.

It didn't fetch an array of my PHP when I put it in while. Am I clumsy enough not to know where is the problem exactly? I've tried to match the variable and also search for alternative ways to fetch array. But it didn't work.

Thank you for your help.


Solution

  • You should not declare the result fetch array outside while loop. Remove this $rowmitra = mysqli_fetch_array($mitra); and try with mysqli_fetch_assoc to get database column name.

                    <?php
                        $mitra = $koneksi->query("SELECT * FROM `mitra`");
                        $i = 1;
                        //I think the problem starts here
                        if (mysqli_num_rows($mitra) > 0) {
                          while ($rowmitra = mysqli_fetch_assoc($mitra)) {
                            echo '<tr>';
                            echo "<th scope='row'>".$i."</th>";
                            echo "<td>".$rowmitra['id_mitra']."</td>";
                            echo "<td>".$i."</td>";
                            echo "<td>".$rowmitra['mitra']."</td>";
                            echo '<td><img width="200px" class="shadow-sm" src="image/logo/'.$rowmitra["logo"].'"></td>';
                            echo '<td><a href="edit-mitra.php?id='.$rowmitra["id_mitra"].'"><button>EDIT</button></a></td>';
                            echo "</tr>";
                            $i = $i + 1;
                          }
                        }
                   ?>