Created
July 18, 2013 17:55
-
-
Save phpfiddle/6031446 to your computer and use it in GitHub Desktop.
[ Posted by Rahul ] ajax page load_data for live edit pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL^E_NOTICE); | |
include "db.php"; | |
if($_POST['page']) | |
{ | |
$page = $_POST['page']; | |
$cur_page = $page; | |
$page -= 1; | |
$per_page = 5; // Per page | |
$previous_btn = true; | |
$next_btn = true; | |
$first_btn = true; | |
$last_btn = true; | |
$start = $page * $per_page; | |
$default_sort_class = "sort_col"; | |
if(isset($_POST['sortColumn']) && $_POST['sortColumn']=="sortColumn" && isset($_POST['sort_col_name']) && $_POST['sort_col_name'] != "") | |
{ | |
$sort_col_name = $_POST['sort_col_name']; | |
$sort_col_order = $_POST['sort_col_order']; | |
$sort_class="sort_col_".$sort_col_order; | |
$query_pag_data = "SELECT pid,name,category,price,discount from products_new order by ".$sort_col_name." ".$sort_col_order." LIMIT $start, $per_page"; | |
} | |
else | |
{ | |
$query_pag_data = "SELECT pid,name,category,price,discount from products_new LIMIT $start, $per_page"; | |
$sort_class=$default_sort_class; | |
} | |
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error()); | |
$finaldata = ""; | |
$tablehead='<tr> | |
<th> | |
Product Name'; | |
if($sort_col_name=="name") | |
{ | |
$tablehead .='<a href="javascript:void(0)" id="name" class="'.$sort_class.'">'; | |
} | |
else | |
{ | |
$tablehead .='<a href="javascript:void(0)" id="name" class="'.$default_sort_class.'">'; | |
} | |
$tablehead .=' | |
</a> | |
</th> | |
<th> | |
Category'; | |
if($sort_col_name=="category") | |
{ | |
$tablehead .='<a href="javascript:void(0)" id="category" class="'.$sort_class.'">'; | |
} | |
else | |
{ | |
$tablehead .='<a href="javascript:void(0)" id="category" class="'.$default_sort_class.'">'; | |
} | |
$tablehead .=' | |
</a> | |
</th> | |
<th> | |
Price'; | |
if($sort_col_name=="price") | |
{ | |
$tablehead .='<a href="javascript:void(0)" id="price" class="'.$sort_class.'">'; | |
} | |
else | |
{ | |
$tablehead .='<a href="javascript:void(0)" id="price" class="'.$default_sort_class.'">'; | |
} | |
$tablehead .=' | |
</a> | |
</th> | |
<br /> | |
<th> | |
Discount'; | |
if($sort_col_name=="discount") | |
{ | |
$tablehead .='<a href="javascript:void(0)" id="discount" class="'.$sort_class.'">'; | |
} | |
else | |
{ | |
$tablehead .='<a href="javascript:void(0)" id="discount" class="'.$default_sort_class.'">'; | |
} | |
$tablehead .=' | |
</a> | |
</th> | |
<th>Edit</th> | |
</tr>'; | |
while($row = mysql_fetch_array($result_pag_data)) | |
{ | |
$id=$row['pid']; | |
$name=htmlentities($row['name']); | |
$category=htmlentities($row['category']); | |
$price=htmlentities($row['price']); | |
$discount=htmlentities($row['discount']); | |
$tabledata.="<tr id='$id' class='edit_tr'> | |
<td class='edit_td' > | |
<span id='mybox_$id' class='myspan_for_edit'>$name</span> | |
<input type='text' id='pid_UniqueIdValIs_".$id."_seprtr_ColNameIs_name' value='$name' class='mytext_for_edit' /> | |
</td> | |
<td class='edit_td' > | |
<span id='mybox_$id' class='myspan_for_edit'>$category</span> | |
<input type='text' id='pid_UniqueIdValIs_".$id."_seprtr_ColNameIs_category' value='$category' class='mytext_for_edit' /> | |
</td> | |
<td class='edit_td' > | |
<span id='mybox_$id' class='myspan_for_edit'>$price $</span> | |
<input type='text' id='pid_UniqueIdValIs_".$id."_seprtr_ColNameIs_price' value='$price' class='mytext_for_edit' /> | |
</td> | |
<td class='edit_td' > | |
<span id='mybox_$id' class='myspan_for_edit'>$discount $</span> | |
<input type='text' id='pid_UniqueIdValIs_".$id."_seprtr_ColNameIs_discount' value='$discount' class='mytext_for_edit' /> | |
</td> | |
<td><a href='#' class='delete' id='$id'> X </a></td> | |
</tr>"; | |
} | |
$finaldata = "<table width='100%'>". $tablehead . $tabledata . "</table>"; // Content for Data | |
/* Total Count */ | |
$query_pag_num = "SELECT COUNT(*) AS count FROM products_new"; | |
$result_pag_num = mysql_query($query_pag_num); | |
$row = mysql_fetch_array($result_pag_num); | |
$count = $row['count']; | |
$no_of_paginations = ceil($count / $per_page); | |
/* ---------------Calculating the starting and endign values for the loop----------------------------------- */ | |
if ($cur_page >= 7) { | |
$start_loop = $cur_page - 3; | |
if ($no_of_paginations > $cur_page + 3) | |
$end_loop = $cur_page + 3; | |
else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) { | |
$start_loop = $no_of_paginations - 6; | |
$end_loop = $no_of_paginations; | |
} else { | |
$end_loop = $no_of_paginations; | |
} | |
} else { | |
$start_loop = 1; | |
if ($no_of_paginations > 7) | |
$end_loop = 7; | |
else | |
$end_loop = $no_of_paginations; | |
} | |
/* ----------------------------------------------------------------------------------------------------------- */ | |
$finaldata .= "<div class='pagination'><ul>"; | |
// FOR ENABLING THE FIRST BUTTON | |
if ($first_btn && $cur_page > 1) { | |
$finaldata .= "<li p='1' class='active'>First</li>"; | |
} else if ($first_btn) { | |
$finaldata .= "<li p='1' class='inactive'>First</li>"; | |
} | |
// FOR ENABLING THE PREVIOUS BUTTON | |
if ($previous_btn && $cur_page > 1) { | |
$pre = $cur_page - 1; | |
$finaldata .= "<li p='$pre' class='active'>Previous</li>"; | |
} else if ($previous_btn) { | |
$finaldata .= "<li class='inactive'>Previous</li>"; | |
} | |
for ($i = $start_loop; $i <= $end_loop; $i++) { | |
if ($cur_page == $i) | |
$finaldata .= "<li p='$i' style='color:#fff;background-color:#006699;' class='active'>{$i}</li>"; | |
else | |
$finaldata .= "<li p='$i' class='active'>{$i}</li>"; | |
} | |
// TO ENABLE THE NEXT BUTTON | |
if ($next_btn && $cur_page < $no_of_paginations) { | |
$nex = $cur_page + 1; | |
$finaldata .= "<li p='$nex' class='active'>Next</li>"; | |
} else if ($next_btn) { | |
$finaldata .= "<li class='inactive'>Next</li>"; | |
} | |
// TO ENABLE THE END BUTTON | |
if ($last_btn && $cur_page < $no_of_paginations) { | |
$finaldata .= "<li p='$no_of_paginations' class='active'>Last</li>"; | |
} else if ($last_btn) { | |
$finaldata .= "<li p='$no_of_paginations' class='inactive'>Last</li>"; | |
} | |
$goto = "<input type='text' class='goto' size='1' style='margin-top:-1px;margin-left:60px;'/><input type='button' id='go_btn' class='go_button' value='Go'/>"; | |
$total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>"; | |
$finaldata = $finaldata . "</ul>" . $goto . $total_string . "</div>"; // Content for pagination | |
echo $finaldata; | |
} | |
if(isset($_POST['updateData']) && $_POST['updateData']=='updateData' && isset($_POST['unique_col_name']) && isset($_POST['unique_col_value']) && isset($_POST['changed_col_name']) && isset($_POST['changed_col_new_val'])) | |
{ | |
$unique_col_name = $_POST['unique_col_name']; | |
$unique_col_value = $_POST['unique_col_value']; | |
$changed_col_name = $_POST['changed_col_name']; | |
$changed_col_new_val = $_POST['changed_col_new_val']; | |
$query="update products_new set ".$changed_col_name." = '".$changed_col_new_val."' where ".$unique_col_name." = '".$unique_col_value."'"; | |
mysql_query($query); | |
} | |
?> | |
<script> | |
$(document).ready(function() | |
{ | |
$("a.sort_col").addClass('sort_col_none'); | |
$("a.sort_col").removeClass('sort_col'); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment