0

hello i created a datatabe to to load my datas sort them and using the search functionality but it dose not work i tried it been 5 days and i am stuck here i do not know what i am missing or what i done wrong this is my codes if any one can help me i would be thankful

<div class="table-responsive">
<table id="datas" class="table table-striped table-bordered" style="width:100%">

    <thead style="color:black;" >
    <th>id</th>
    <th>Product</th>
    <th>Price</th>
    <th>Weight</th>
    <th>Image</th>
    <th>Type</th>
    <th>Type 2</th>
    </thead>

    <?php

        $get = mysqli_query($conn,"SELECT * FROM products;");
    ?>
        <tbody>
<?php
        while ($row=mysqli_fetch_array($get)) {
            $id=$row['product_id'];
            $name=$row['product_name'];

            $type2=$row['product_type'];
            $weight=$row['weight'];
            $price=$row['product_price'];

            $type=$row['type'];
            $img=$row['img'];

        $get1 = mysqli_query($conn," SELECT * FROM money WHERE name='$type' ");

        while ($row1 = mysqli_fetch_assoc($get1)):
            $p=$row1['price'];
            $newprice = $p*$weight;
        ?>
        <td><?php echo $id;?></td>
        <td><?php echo $name;?></td>
        <td>$<?php echo $newprice;?></td>
        <td><?php echo $weight;?> g</td>
        <td>
            <img  src="<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;">
        </td>
        <td><?php echo $type;?></td>
        <td><?php echo $type2;?></td>

        </tbody>

        <?php
    endwhile;
  }


  ?>


                    </div>
                    </table>

these are my bootstrap and scripts that i use

  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css">

    <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
 <script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
  <script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js"></script>

also this is my data table code

 <script type="text/javascript">
      $(document).ready(function() {
    $('#datas').dataTable( { "ordering": true } );

} );
    </script>

the datatable is created but it does not work like the search or the sorting this is picture of what it likes picture i am using bootstrap 4

console

7
  • 1
    Don't just say that something isn't working. What is happening? What should be happening? What debugging have you already done? Commented Jan 29, 2020 at 18:36
  • @PatrickQ when you install datatable it should pagination , search , sort , a lot of more things but none of that works i tried to install the fonts change the php codes countless times change the table it self Commented Jan 29, 2020 at 18:42
  • Again, all you're saying is that it doesn't work. That doesn't help us. You need to do some debugging to narrow down the problem. If I bring my car to a mechanic and say "my car doesn't work", chances are I'm going to be disappointed with the result. Commented Jan 29, 2020 at 18:46
  • @PatrickQ the problem os i do not now why it is not working or where is the problem i searched a lot before i post here i did not get result that is why i posted here maybe someone now what is the problem these are all my codes i searched a lot with out any result i dont know what to say actually Commented Jan 29, 2020 at 18:48
  • If you haven't already (and you really should have) check your browser's JavasScript console for error. Without more information, there's really nothing else we can tell you. Commented Jan 29, 2020 at 19:02

2 Answers 2

2

You are missing <tr> both on your <thead> and <tbody>

<thead style="color:black;">
<th>Product</th>
</thead>

should be:

<thead style="color:black;">
  <tr>
    <th>Product</th>
  <tr>
</thead>

and inside you tbody while:

while ($row1 = mysqli_fetch_assoc($get1)):
    $p=$row1['price'];
    $newprice = $p*$weight;
?>
    <tr>
       <td>...</td>
    </tr>
<?php endwhile; ?>

You're also closing your </tbody> on every while loop:

    </tbody>    
<?php
    endwhile;
?>
    </div> //where are you opening this div? Seems it should not be here
</table>

change to:

        <?php endwhile; ?>
    </tbody> 
</table>

This changes should make your DataTable work as expected.

Sign up to request clarification or add additional context in comments.

Comments

1

Malformed html, loops wrong, missing tr tag, etc. Should be something like this.

    <div class="table-responsive">
<table id="datas" class="table table-striped table-bordered" style="width:100%">

    <thead style="color:black;" >
        <th>id</th>
        <th>Product</th>
        <th>Price</th>
        <th>Weight</th>
        <th>Image</th>
        <th>Type</th>
        <th>Type 2</th>
    </thead>

    <?php
    $get = mysqli_query($conn,"SELECT * FROM products;");
    ?>
    <tbody>

    <?php
    while ($row=mysqli_fetch_array($get)) {
        $id=$row['product_id'];
        $name=$row['product_name'];

        $type2=$row['product_type'];
        $weight=$row['weight'];
        $price=$row['product_price'];

        $type=$row['type'];
        $img=$row['img'];

        $get1 = mysqli_query($conn," SELECT * FROM money WHERE name='$type' ");

        while ($row1 = mysqli_fetch_assoc($get1)):
            $p=$row1['price'];
            $newprice = $p*$weight;
            ?>
            <tr>
            <td><?php echo $id;?></td>
            <td><?php echo $name;?></td>
            <td>$<?php echo $newprice;?></td>
            <td><?php echo $weight;?> g</td>
            <td>
                <img  src="<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;">
            </td>
            <td><?php echo $type;?></td>
            <td><?php echo $type2;?></td>
            </tr>
        ?>
        <?php endwhile; ?>
    <?php } ?>
    </tbody>
</table>
</div>

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.