1

How can i display Data from MYSQL table using DataTables I tried but Top part shown me enter image description here

Bottom Shows Records from MYSQL table.

I want DataTables to read Mysql data display as shown in the picture below

enter image description here

//bottstrap html code

<div class="panel-body">
            <div class="dataTable_wrapper">
                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                       <thead>
                          <tr>
                            <th>Brand Name</th>
                             <th>Size</th>
                          </tr>
                         </thead>

// php

       $record = mysqli_query($con,"SELECT * FROM ts127 ORDER BY Manufacturer");
    while ($row = mysqli_fetch_array($record)) {

       if()
         {
         }
       elseif ($row['field1']==$XXX) 
{   
     echo'<table style="table-layout:fixed;" class="table table-striped table-bordered table-hover" id="dataTables-example">';
     // When i remove the above table code works fine for the first record or first row but other listed as a normal text
                echo "<tr'>";                 
                  echo "<td style=' width:150px;  text-align:left; padding: 10px;vertical-align: middle;'>";echo $row['Brand_Name'];echo"</td>";
                  echo "<td style='width:110px;  text-align:left; vertical-align: middle;'>";echo $row['Size'];"</td>";
                       ....

    echo '<tr/></tbody></table>';
 }

//JS Code

  <script>
$(document).ready(function() {
    $('#dataTables-example').DataTable({
            responsive: true
    });
});
</script>
9
  • could you show me the Js code please Commented Dec 6, 2015 at 5:50
  • @FahedAlkaabi i added it to the question or you want to see the content of jquery.dataTables.min.js Commented Dec 6, 2015 at 5:51
  • If I'm not mistaken, You doing a lot of things of PHP and the code is not completed. So I can't judge you PHP code. - Js code and html nothing wrong with it - On you browser check the console on the developing tools (Google Chrome F12) and see if there is any errors Commented Dec 6, 2015 at 5:55
  • Your PHP code is not clear, what is this $XXX, or $YYY or the reset values in if condition. Also in while loop you should repeat the tr tag not table tag Commented Dec 6, 2015 at 5:57
  • @GoudaElalfy Yes When i remove this line echo'<table style="table-layout:fixed;" class="table table-striped table-bordered table-hover" id="dataTables-example">'; it works but for the first row only Commented Dec 6, 2015 at 6:06

2 Answers 2

1

You mistake was looping the table and tbody, you should be more careful about opening and closing the HTML tags

<?php
$record = mysqli_query($con,"SELECT * FROM ts127 ORDER BY Manufacturer");
echo'<table style="table-layout:fixed;" class="table table-striped table-bordered table-hover" id="dataTables-example">';
    echo '<thead>';
        echo '<tr>';
            echo '<th>Brand Name</th>';
            echo '<th>Size/th>';
        echo '</tr>';
    echo '</thead>';
    echo '<tbody>';
    while ($row = mysqli_fetch_array($record)) {
        if (($row['field1']==$XXX || $row['field1']==$YYY ) && ($row['field2']>=$field1sw && $row['field1']<=$largesw) && ($txtbrand != "" && $size1=="" && $row['field2'] != "" && $row['Brand_Name']!="") ) 
        {   
            echo "<tr'>";                 
                echo "<td style=' width:150px;  text-align:left; padding: 10px;vertical-align: middle;'>";echo $row['Brand_Name'];echo"</td>";
                echo "<td style='width:110px;  text-align:left; vertical-align: middle;'>";echo $row['Size'];"</td>";
                   ....
            echo '</tr>';
        }
    }
    echo '</tbody>';
echo '</table>';
?>

if NOT, I would recommend you to use server-side DataTable

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

5 Comments

That Works only for the first record.
So you need to double check on your if statement
I forget to add Thead, have a look please
But the Sort icon on the heading and Paginations are gone
stackoverflow.com/a/26564457/5154084 here is the answer, try to check if sorting icons is exist also if requested well
0

Update this

echo '<tr/></tbody></table>';

To be

echo '</tr>';

Then close the tbody, and table tag outside while loop

1 Comment

Do you mean echo '</tr>';

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.