how to load mysql data faster into jquery datatable - php

hi i am using jquery datatable plugin to load mysql data into it so far its goin good but when the records are more the data table gets relatively slow to load so now i am stuck here and have no idea of what has to be done so can anyone help me in this
here is my code
<?php
$result = mysql_query("SELECT * FROM `mdb` ORDER BY grno");
?>
$(document).ready(function(){
$('#datatables').dataTable({
"sPaginationType":"full_numbers",
"aaSorting":[[2, "desc"]],
"bJQueryUI":true
});
})
<table id="datatables" class="display">
<thead>
<tr>
<th>Srno.</th>
<th>Brno.</th>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Pin</th>
<th>Mobile</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td align='center'>$row[grno]</td>";
echo "<td align='center'>$row[brno]</td>";
echo "<td align='center'>$row[name]</td>";
echo "<td align='center'>$row[address]</td>";
echo "<td align='center'>$row[city]</td>";
echo "<td align='center'>$row[pin]</td>";
echo "<td align='center'>$row[mobile]</td>";
echo "<td><img src='images/edit.png'> <img src='images/delete.png'></td>";
echo "</tr>";
}
?>
</tbody>
</table>

Please have a look at here.
If you want to have server-side processing click here.

Related

PHP - If database record is a specific value, only show these buttons

I have a multi-step form that allows the user to save for later or submit. If they save for later, i want them to have an option to edit or delete but if they submit, i dont want them to have any option to edit or delete. Although the following code makes sense, its not working; i'm not getting any buttons and it breaks the jquery table plugin and i dont have the search or pagination option:
<table id="myTable" class="table table-bordered table-hover table-striped tablesorter table-responsive">
<thead>
<tr>
<td>Id</td>
<td>Status</td>
<td>Date/Time</td>
<td>Tag</td>
<td>Serial Number</td>
<td>Equipment</td>
<td>Company</td>
<td>Grand Total</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<?php
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['id']."</td>";
echo "<td><span class='label ".$res['labelwarning']."'>".$res['status']."</span></td>";
echo "<td>".$res['todaysdatetime']."</td>";
echo "<td>".$res['tag']."</td>";
echo "<td>".$res['serialnumber']."</td>";
echo "<td>".$res['currentequipment']."</td>";
echo "<td>".$res['company']."</td>";
echo "<td>".$res['total_grandtotal']."</td>";
$Submitted = "Submitted";
$Saved = "Saved";
if ($res['labelwarning'] == $Submitted) {
echo "<td></td>";
} elseif ($res['labelwarning'] == $Saved) {
echo "<td></td>";
}
echo "</tr>";
}
?>
<Script>
$(document).ready(function() {
$("#myTable").dataTable({
"aaSorting": [ [0,'desc'] ]
});
});
</Script>
Oke I will try to explain this process here:
1 - First you will create a php file where you will fetch the results you need.
when you are fetching the records you will put your results in a variable like so:
$result_html = '';
while($res = mysqli_fetch_array($result)) {
$result_html .= "<tr>";
$result_html .= "<td>".$res['id']."</td>";
...en so on
$result_html .= "</tr>";
}
To return the response to your ajax call you need to add also this:
echo json_encode($result_html);
2 - Before the end </body> tag
<script>
$(document).ready(function() {
$.ajax({
url: '/path/to/the_php_file',
type: 'POST',
dataType: 'json',
data : { //only needed if you have to pass parameters to your query },
})
.done(function(data) {
//data is your html response
// and #returnedResponse is the id given to your body tag
$('#returnedResponse').html(data);
$("#myTable").dataTable();
})
.fail(function() {
console.log("error");
});
});
</script>
3 - Your table
<table id="myTable" class="table table-bordered table-hover table-striped tablesorter table-responsive">
<thead>
<tr>
<td>Id</td>
<td>Status</td>
<td>Date/Time</td>
<td>Tag</td>
<td>Serial Number</td>
<td>Equipment</td>
<td>Company</td>
<td>Grand Total</td>
<td>Action</td>
</tr>
</thead>
<tbody id="returnedResponse">
</tbody>
</table>
NOTE:
You need to have the link to the jquery.js before your ajax call.
This is not a copy and paste solution. You asked for an example on how to do it and here you have a good start point to learn it.
Try to change your if/else part to this:
if ($res['labelwarning'] == $Submitted) {
echo "<td></td>";
} else{
echo "<td></td>";
}

Formatting php into html table correctly

I have a php array that contains information from my database $name[] = $row['name']. There are also about 3 other rows that container email, age, and screen-resolution.
I am trying to neatly assemle this into a table that looks like:
name----------email----------age----------screen-res
name1---------email1--------age1---------res1
name2---------email2--------age2---------res2
However mine currently looks like this:
name----------email----------age----------screen-res
name1---------name2----------name3------name4---------name5------name6-------name7------name8
email1---------email2----------email3------email---------email5------email6-------email7
My Code
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Screen-Res</th>
</tr>
<?php
echo "<tr>";
foreach ($name as $nameVal) {
echo "<td>$nameVal</td>";
}
echo "</tr>";
echo "<tr>";
foreach ($email as $emailVal) {
echo "<td>$emailVal</td>";
}
echo "</tr>";
echo "<tr>";
foreach ($age as $ageVal) {
echo "<td>$ageVal</td>";
}
echo "</tr>";
echo "<tr>";
foreach ($screen-res as $screen-resVal) {
echo "<td>$screen-resVal</td>";
}
echo "</tr>";
?>
</table>
You are forming your tables incorrectly. You your table to look something like this. The good thing is you only need one array for all your data
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Screen-Res</th>
</tr>
<?php
foreach ($rows as $row) {
echo "<tr>";
echo "<td>".$row['nameVal']."</td>";
echo "<td>".$row['emailVal']."</td>";
echo "<td>".$row['ageVal']."</td>";
echo "<td>".$row['screen-resVal']."</td>";
echo "</tr>";
}
?>
</table>

How to do pagination in wordpress page that contain mysql query?

I have one wordpress page in that I want to add pagination code. Because of mysql query the data in page is dynamic so I searched on google this but as I'm new in wordpress don't know what is WP-PageNavi plugin and how to use it. Please suggest me how to use it or any simpler way to do this.
My code is:
<table id="myTable" class="table table-striped" style="width:200% !important" name="myTab">
<thead>
<tr>
<th>Sr.No</th>
<th>Name</th>
<th>College Name </th>
<th>Passout Year</th>
<th>Branch</th>
<th>Email ID </th>
<th> Phone No.</th>
</tr>
</thead>
<tbody>
[hi_php]
mysql_connect("localhost","root","");
mysql_select_db("wp");
$query = mysql_query("select id,value from wp_crf_entries where value like '%Alumini%' ")or die(mysql_error());
while ($row=mysql_fetch_array($query))
{
$users = maybe_unserialize($row['value']);
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $users['firstname_1'];
echo " ".$users['lastname_2'] ."</br>";
echo "</td><td>";
echo $users['collegename_4'] ;
echo "</td><td>";
echo $users['passoutyear_7'] ;
echo "</td><td>";
echo $users['branch_8'] ;
echo "</td><td>";
echo $users['user_email'] ;
echo "</td><td>";
echo $users['contactno_10'] ;
echo "</td></tr>";
echo "<tr><td>";
}
[/hi_php]
</tbody>
</table>

How to echo html and row from database

I have a script written to grab a row from my database based on current session user, which outputs the row correctly, however I want to insert a small image to be displayed alongside of the echo'd row, and cannot figure out the proper syntax.
if ($row['lifetime']!="")
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div> ".$row['lifetime'];
else
echo '';
?>
basically I want the image to appear right before or after the .$row appears, either or.
You can try:
<?php
if ($row['lifetime'] !== "") {
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div>";
echo $row['lifetime'];
echo "<img src='' alt='' style='width:100px'/>";
}
?>
Just put the HTML for the image into the string you're echoing:
echo "<div style ='font:12px Arial;color:#2F6054'><img src="fill in URL here"> Lifetime Member: </div> ".$row['lifetime'];
You can try as below example
HTML
<table>
<thead>
<tr>
<th>No.</th>
<th>Customer Name</th>
<th>Photo</th>
<th ></th>
</tr>
</thead>
<tbody>
<?php
$tst=0;
$result = mysql_query("select * from acc_cust");
while($row = mysql_fetch_array($result))
{
echo "<tr class='odd gradeX'>";
echo "<td width=5%'>" . $row['ent_no']. "</td>";
echo "<td>" . $row['cust_name']. "</td>";
echo "<td><img src='[path]" . $row['cust_img'] . "' /></td>";
}
?>
</tbody>
</table>

Repeating Table Headers in HTML

I have a large dataset from MySQL that I am displaying using PHP. If the user tries to print it, it has some 50 pages of data.
The entire data is displayed using tables and simple PHP echo-s that display the data in the table fields.
What I need is - when a user tries to print it OR export to PDF in Safari (Mac), the table headers should appear on all pages. How can this be done?
Thanks
Edit: I have enclosed the table headers under but it doesnt work. It's a plain HTML page with no scripts that just displays data from database.
Code
echo "<table border='1'>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th colspan=13>Item Info</th>
<th colspan=4>Lot Info</th>
</tr>
<tr>
<th bgcolor='#C8E3FF'>S.No.</th>
<th bgcolor='#C8E3FF'>Party Name</th>
<th bgcolor='#C8E3FF'>Item No.</th>
<th bgcolor='#C8E3FF'>Date</th>
<th>Flower</th>
<th bgcolor='#C8E3FF'>Lot No.</th>
<th bgcolor='#C8E3FF'>Avg Wt.</th>
<th bgcolor='#C8E3FF'>Total Weight</th>
<th bgcolor='#C8E3FF'>Detail Page</th>
</tr></thead><tfoot></tfoot><tbody>";
echo "<tr>";
echo "<td bgcolor='#C8E3FF' rowspan=".$itemCounter.">".$sno."</td>";
echo "<td bgcolor='#C8E3FF' rowspan=".$itemCounter.">".$row['partyName']."</td>";
while($row2 = mysqli_fetch_array($itemquery))
{
$itemNo++;
echo "<td bgcolor='#C8E3FF'>".$itemNo."</td>";
echo "<td bgcolor='#C8E3FF'>".$row2['date']."</td>";
//$flower = $flower + $row2['flower'];
echo "<td>".$row2['flower']."</td>";
echo "<td bgcolor='#C8E3FF'>".$row2['lotno']."</td>";
echo "<td bgcolor='#C8E3FF'>".$row2['avgwt']."</td>";
if ($row2['totalWeight'] == 0)
{
echo "<td bgcolor='#C8E3FF'> </td>";
}
else{
$totalWt = $totalWt + $row2['totalWeight'];
echo "<td bgcolor='#C8E3FF'>".$row2['totalWeight']."</td>";
}
echo "<td bgcolor='#C8E3FF'>".$row2['detailPage']."</td>";
echo "</tr>";
}
echo "<td bgcolor='#C8E3FF'><b>Total</b></td>";
echo "<td><b>".$flower."</b></td>";
echo "<td bgcolor='#C8E3FF'></td>";
echo "<td bgcolor='#C8E3FF'></td>";
echo "<td bgcolor='#C8E3FF'><b>".$totalWt."</b></td>";
echo "<td bgcolor='#C8E3FF'></td>";
echo "</tr>";
echo "<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>";
}
echo "</tbody></table>";
As far as I know, if you properly use the THEAD section in the table, that header will be repeated on each page.
http://www.w3.org/TR/html4/struct/tables.html#h-11.2.3
added from http://www.w3schools.com/tags/tag_thead.asp:
Definition and Usage
The tag is used to group header content in an HTML table.
The element is used in conjunction with the and elements to specify each part of a table (header, body, footer).
Browsers can use these elements to enable scrolling of the table body independently of the header and footer. Also, when printing a large table that spans multiple pages, these elements can enable the table header and footer to be printed at the top and bottom of each page.
The tag must be used in the following context: As a child of a element, after any , and elements, and before any , , and elements.

Categories