Why is my DataTable only returning me one data though it has all data displayed?

Why is my DataTable only returning me one data though it has all data displayed?

cephceph Posts: 2Questions: 1Answers: 0
edited July 2019 in Free community support

So I am working on a project as part of an internship where my Task is to code a user registration. To tackle this task I use php, phpMyAdmin and mysqli for the connection.
Everything works great so far, but the next part of the task is that the user is then getting redirected to an index.php page where he can see the user data, search it, edit it, etc.
I coded that aswell, worked out great and then tried to improve my coding, make it more object-orientated (I'm still pretty new to coding so that's not my standard yet :<).
As I searched for improvements I first found jquery which made me get some cool new features and optics for my project (made me very happy to find such a handy tool since I can only read java code at best and these scripts were so easy to implement). Later on I found out About datatables which pretty much threw all my code for the index.php into the bin since it has most of the features I implemented myself already in better looking and nicely working. But here is the problem, somehow it displays all the data I have in my SQL database BUT tells me it would show only 1 to 1 of 1 entries which is totally wrong. As a result the search in the upper right of the database is not working, neither the sorting of rows, etc.

My guess is that the code doesn't realise where my first data ends, though my table is formatted as datatable needs it. AFAIK it might be the issue that I fetch the data as an object with $var->fetch_object() rather than fetching data as an array? Is this maybe the case or am I completly wrong? Before I toss everything overboard I wonder if anyone has a solution or ideas for me?

Here is my code of my current attempt for easier review of my problem:
(Sorry for the messages and echoes being in German :<)

Thank you very much in advance for any suggestions,
ceph

```
<?php session_start(); if (!isset($_SESSION['username'])) { $_SESSION['msg'] = "Sie müssen sich erst einloggen."; header('location: login.php'); } if (isset($_GET['logout'])) { session_destroy(); unset($_SESSION['username']); header("location: login.php"); } <?php > <!DOCTYPE = HTML> ?> Test von DataTables $(document).ready( function () { $('#dataTable').DataTable(); } ); table { font-family: verdana, sans-serif; border-collapse: collapse; width: 100%; } body { background-color: #e3e5e6; }

Main Interface

<?php if (isset($_SESSION['s <div class="error success" >

<?php echo $_SESSION['success']; unset($_SESSION['success']); ?>
</h3>
</div>
<?php endif ?> <?php if (isset($_SESSION['username'])) : ?> <p><center>Willkommen <strong><?php echo $_SESSION['username']; ?>.</center></strong> <a href="index.php?logout='1'" style="color: red;"><br><center><b>Ausloggen</b></center></a> </p> <?php endif ?> <?php $db = mysqli_connect('localhost', 'XXXXXXXXXX', 'XXXXXXXXXX', 'benutzerverwaltung'); if ($db) { echo "<center>Erfolgreich zur SQL Datenbank verbunden.</center>"; } ?> </div> <?php $var = $db->query("SELECT * FROM users");
$count = $db->affected_rows; <?php >


ID Username Passwort Vorname Nachname Rolle Erstellt am Erstellt von Zuletzt bearbeitet am Zuletzt bearbeitet von <?php while ($zeile = $var->fetch_object()) { > <?php echo $zeile->id; ?> <?php echo $zeile->username; ?> <?php echo $zeile->passwort; ?> <?php echo $zeile->vorname; ?> <?php echo $zeile->nachname; ?> <?php echo $zeile->role; ?> <?php echo $zeile->created_at; ?> <?php echo $zeile->created_by; ?> <?php echo $zeile->last_edited; ?> <?php echo $zeile->edited_by; ?> <?php } > <?php if($count > 10){ > ID Username Passwort Vorname Nachname Rolle Erstellt am Erstellt von Zuletzt bearbeitet am Zuletzt bearbeitet von <?php } >

``` ?>

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @ceph ,

    If you remove the DataTable initialisation, does the table show all the expected records in a plain HTML table? If not, that's the place to start looking.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    Answer ✓

    I'm not familiar with PHP but it looks like you are adding the tbody tag within the loop which would result in an invalid HTML page.

    ```
    <?php while ($zeile = $var->fetch_object()) {

    <?php > ``` ?>

    You can look at the page using view source. You can also use this site to validate your HTML:
    https://validator.w3.org/

    Kevin

  • cephceph Posts: 2Questions: 1Answers: 0
    edited July 2019

    @colin thank you for the advice. I tried that, but @kthorngren found the solution! I thank you very much, sometimes your eyes just won't find mistakes after a long day..

    :smile:

    P.S.: Thank you very much for formatting my table whoever it was.. :smiley:

This discussion has been closed.