2

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.

Zain Aftab
  • 703
  • 7
  • 21
Bayu Wicaksono
  • 101
  • 1
  • 1
  • 8
  • PHP executes on the Server, before it is sent to the Browser. You would want to use AJAX after rendering HTML with PHP. – StackSlave Jul 15 '19 at 04:09
  • @StackSlave well, yes. But I didn't learn well enough with AJAX tbh. Probably it just an error of my code. Thank u for the answer – Bayu Wicaksono Jul 15 '19 at 04:11

1 Answers1

1

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;
                      }
                    }
               ?>
Rajkumar R
  • 1,097
  • 8
  • 14