Simple Pagination in Php Mysql and fetch data with ajax
on your all_users.php
<?php include_once "include/header.php"; ?>
<?php
$orderby = "ORDER BY id DESC";
$limitval = isset($_GET['limitof'])? $_GET['limitof']:5;
$limitrow = "LIMIT 0,$limitval";
$count_total = $aClassDb->countRowAll("users");
$all_user = $aClassDb->getAllData('users',$orderby, $limitrow);
$total_rows = ceil($count_total/$limitval);
?>
<div class="row">
<div class="col-md-12">
<!-- BEGIN EXAMPLE TABLE PORTLET-->
<div class="portlet light portlet-fit bordered">
<div class="portlet-title">
<div class="caption">
<i class="icon-settings font-red"></i>
<span class="caption-subject font-red sbold uppercase">All Ads</span>
</div>
</div>
<div class="row">
<div class="col-md-8 col-sm-12">
<form method="get" action="" id="submitrecord">
<div class="dataTables_length" id="datatable_ajax_length"><label><span class="seperator"></span>View <select name="limitof" aria-controls="datatable_ajax" class="form-control input-xs input-sm input-inline slctboxval" onchange="changeoffset()">
<option value="" disabled selected>-</option><option value="5">5</option><option value="10">10</option><option value="20">20</option><option value="50">50</option><option value="100">100</option><option value="150">150</option><option value="-1">All</option></select> records</label>
</div>
</form>
</div>
</div>
<div class="portlet-body contanier_with_no_left_padding">
<div class="allmsgs_for_tbl"></div>
<table class="table table-striped table-hover table-bordered" id="sample_editable_1">
<thead>
<tr>
<th> S no. </th>
<th>ID</th>
<th> Name </th>
<th> Email </th>
<th> Contact </th>
<th>Gender</th>
<th>Address</th>
<th>Date</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody class="tbl_bodyusr">
<?php if($all_user) { $count1=0;
foreach ($all_user as $key => $userinfo) {
$usersingleid = $userinfo['id'];
$dateofregistration = date('d-m-Y', strtotime($userinfo['registerdate']));
if($userinfo['status']==0) { $status = "<div class='bgclrblue'>Pending</div>"; }elseif($userinfo['status']==1) { $status = "<div class='bgclrgrn'>Active</div>"; }elseif($userinfo['status']==2) { $status = "<div class='bgclrred'>Denied</div>"; }
?>
<tr>
<td><?php echo ++$count1; ?></td>
<td><?php echo $userinfo['id']; ?></td>
<td><?php echo $userinfo['fullname']; ?></td>
<td><?php echo $userinfo['email']; ?></td>
<td><?php echo $userinfo['contact']; ?></td>
<td><?php echo $userinfo['gender']; ?></td>
<td><?php echo $userinfo['address']; ?></td>
<td><?php echo $dateofregistration; ?> </td>
<td class="status_data_of_ad"><?php echo $status; ?></td>
<td>
<span class="actionofstatus">
<?php if($userinfo['status']==0) { //active ?>
<a onclick="activethis(this,<?php echo $usersingleid; ?>)" class="clrgrn" title="active"><i class="fa fa-check-circle"></i></a>
<a onclick="denythis(this,<?php echo $$usersingleid; ?>)" class="clrred" title="denied"><i class="fa fa-times-circle"></i></a>
<?php } elseif($userinfo['status']==1) { ?>
<a onclick="denythis(this,<?php echo $usersingleid; ?>)" class="clrred" title="denied"><i class="fa fa-times-circle"></i></a>
<?php } elseif($userinfo['status']==2) { ?>
<a onclick="activethis(this,<?php echo $usersingleid; ?>)" class="clrgrn" title="active"><i class="fa fa-check-circle"></i></a>
<?php } ?>
</span>
<a href="edit_ad.php?uid=<?php echo md5(uniqid()); ?>&aid=<?php echo base64_encode($usersingleid);?>" class="clrblue" title="edit"><i class="fa fa-pencil-square"></i></a>
<a onclick="dltthis(this,<?php echo $usersingleid; ?>)" class="clrred" title="delete"><i class="fa fa-trash"></i></a>
<a href="all_ads.php?&uid=<?php echo md5(uniqid());?>&userid=<?php echo base64_encode($usersingleid); ?>" class="clrgrn" title="View Ads"><i class="fa fa-eye"></i></a>
</td>
</tr>
<?php } ?>
<?php }else { ?>
<div class="no-result-found-cl"><h3>No Result Found</h3></div>
<?php } ?>
</tbody>
</table>
</div>
<div class="pagination_container">
<span style="flaot:left;">
<?php
if($total_rows > 1)
{
for($i=1; $i <= $total_rows; $i++)
{
echo "<a onclick='showpage(this,$i)'><span class='pagibox'> $i</span></a>";
}
}
?>
</span>
<span class="loadingmore"><i class="fa fa-spinner fa-spin"></i></span>
</div>
<span style="clear:both"></span>
</div>
<!-- END EXAMPLE TABLE PORTLET-->
</div>
<!-- END CONTENT BODY -->
</div>
<style>
.bgclrblue { background-color: #0095fd; color: #fff; padding:5px; margin:5px; }
.bgclrgrn { background-color: green; color:#fff; padding:5px; margin:5px; }
.clrgrn { color:green;}
.clrblue { color:#0095fd;}
.clrred { color:red; }
.bgclrred{ background-color: red; color: #fff; padding:5px; margin:5px; }
.popupcontainer{ width:100%; height:100%; background-color: rgba(255,255,255,0.66); }
.userdatacontainer td { padding:5px; }
#modelbutton{ display:none;}
.post_ad_featured { width: 120px; height:60px; }
.contanier_with_no_left_padding { padding-left:0px!important; padding-right:0px!important; }
.pagination_container { margin-bottom: 10px;}
.pagibox { padding: 10px; border: 1px solid #dedede;}
.loadingmore { color: #0095fd; font-size: 25px; vertical-align: middle; display: none;}
</style>
<script>
var opc = jQuery.noConflict();
function activethis(obj,id)
{
var active_user_id = id;
opc(".loadingmore").show();
opc.ajax({
type: "POST",
url: "admin_ajax_request.php",
data:{userforactive:active_user_id},
}).done(function(data){
opc(".loadingmore").hide();
opc(".allmsgs_for_tbl").html(data);
opc(".allmsgs_for_tbl").fadeIn();
opc(".allmsgs_for_tbl").delay(5000).fadeOut();
if(data)
{
opc(obj).parent("span").parent("td").parent("tr").find("td.status_data_of_ad").html("<div class='bgclrgrn'>Active</div>");
opc(obj).parent("span").html("<a onclick='denythis(this,"+active_user_id+")' class='clrred' title='denied'><i class='fa fa-times-circle'></i></a>");
}
});
}
function denythis(obj,id)
{
var deny_user_id = id;
opc(".loadingmore").show();
opc.ajax({
type: "POST",
url: "admin_ajax_request.php",
data:{userfordeny:deny_user_id},
}).done(function(data){
opc(".loadingmore").hide();
opc(".allmsgs_for_tbl").html(data);
opc(".allmsgs_for_tbl").fadeIn();
opc(".allmsgs_for_tbl").delay(3000).fadeOut();
if(data)
{
opc(obj).parent("span").parent("td").parent("tr").find("td.status_data_of_ad").html("<div class='bgclrred'>Denied</div>");
opc(obj).parent("span").html("<a onclick='activethis(this,"+deny_user_id+")' class='clrgrn' title='active'><i class='fa fa-check-circle'></i></a>");
}
});
}
function dltthis(obj,id)
{
var delete_user_id = id;
if (confirm("Are you sure to delete this Ad?")) {
opc(".loadingmore").show();
opc.ajax({
type: "POST",
url: "admin_ajax_request.php",
data:{useridfordelete:delete_user_id},
}).done(function(data){
opc(".loadingmore").hide();
opc(".allmsgs_for_tbl").html(data);
opc(".allmsgs_for_tbl").fadeIn();
opc(".allmsgs_for_tbl").delay(3000).fadeOut();
if(data.indexOf("success") > -1)
{
opc(obj).parent().parent().remove();
//opc(obj).parent("tr").remove();
}
});
}
return false;
}
function viewuser(obj,userid)
{
var user_id = userid;
var limitval = <?php echo $limitval; ?>;
opc(".loadingmore").show();
opc.ajax({
type: "POST",
url: "admin_ajax_request.php",
data:{prouserid:user_id,limitvalue:limitval},
}).done(function(data){
opc(".loadingmore").hide();
if(data)
{
opc("#modelajaxdata").html(data);
opc('#modelbutton').click();
}
});
}
function showpage(obj,val)
{
var pagenumber = val;
opc('.pagibox').css('font-weight','');
opc(obj).children(".pagibox").css('font-weight','bold');
opc.ajax({
type:"POST",
url:"admin_ajax_request.php",
data:{pageid:pagenumber}
}).done(function(data){
opc(".tbl_bodyusr").html(data);
});
}
function changeoffset()
{
//var slctboxval= opc(".slctboxval").val();
opc("#submitrecord").submit();
}
</script>
<?php include_once "include/footer.php"; ?>
STEP 2. CREATE A NEW FILE function.php
<?php
class ClassDb
{
function __construct()
{
$this->database = AN_DATABASE_NAME;
$this->username = AN_DATABASE_USER;
$this->password = AN_DATABASE_PASSWORD;
$this->hostname = AN_DATABASE_HOST;
$this->Connect();
}
public function getAllData($aTable,$aCon = '',$aOrder = '', $limit='')
{
$aRows = array();
$aSql = "SELECT * FROM {$aTable} WHERE 1 {$aCon} {$aOrder} {$limit}";
//echo $aSql;
$aRes = mysqli_query($this->databaseLink,$aSql);
while($aRow = mysqli_fetch_assoc($aRes))
{
$aRows[] = $aRow;
}
return $aRows;
}
public function countRowAll($aTable)
{
$aSql = mysqli_query($this->databaseLink,"SELECT COUNT(*) as count FROM {$aTable} ");
$aCount = mysqli_fetch_assoc($aSql);
return $aCount['count'];
}
}
IN YOUR AJAX FILE admin_ajax_request.php
//pagination starts here
if(isset($_POST['pageid']))
{
$pageid = $_POST['pageid'];
$limit = isset($_POST['limitvalue'])? $_POST['limitvalue']:5;
if($pageid > 0)
{
$offset = ($pageid-1)*$limit;
}else {
$offset = 0;
}
$orderby = "ORDER BY id DESC";
$limitval = isset($_POST['limitvalue'])? $_POST['limitvalue']:5;
$limitrow = "LIMIT $offset,$limitval";
$queryGetReaults = $aClassDb->getAllData("users",$orderby,$limitrow);
if(count($queryGetReaults) <1)
{
echo "<div class='alert alert-danger productstatus'><h3>Sorry!!! No result found.</h3></div>";
} else {
$count1 = $offset++;
foreach ($queryGetReaults as $key => $userinfo) {
$usersingleid = $userinfo['id'];
$dateofregistration = date('d-m-Y', strtotime($userinfo['registerdate']));
if($userinfo['status']==0) { $status = "<div class='bgclrblue'>Pending</div>"; }elseif($userinfo['status']==1) { $status = "<div class='bgclrgrn'>Active</div>"; }elseif($userinfo['status']==2) { $status = "<div class='bgclrred'>Denied</div>"; }
?>
<tr>
<td><?php echo ++$count1; ?></td>
<td><?php echo $userinfo['id']; ?></td>
<td><?php echo $userinfo['fullname']; ?></td>
<td><?php echo $userinfo['email']; ?></td>
<td><?php echo $userinfo['contact']; ?></td>
<td><?php echo $userinfo['gender']; ?></td>
<td><?php echo $userinfo['address']; ?></td>
<td><?php echo $dateofregistration; ?> </td>
<td class="status_data_of_ad"><?php echo $status; ?></td>
<td>
<span class="actionofstatus">
<?php if($userinfo['status']==0) { //active ?>
<a onclick="activethis(this,<?php echo $usersingleid; ?>)" class="clrgrn" title="active"><i class="fa fa-check-circle"></i></a>
<a onclick="denythis(this,<?php echo $$usersingleid; ?>)" class="clrred" title="denied"><i class="fa fa-times-circle"></i></a>
<?php } elseif($userinfo['status']==1) { ?>
<a onclick="denythis(this,<?php echo $usersingleid; ?>)" class="clrred" title="denied"><i class="fa fa-times-circle"></i></a>
<?php } elseif($userinfo['status']==2) { ?>
<a onclick="activethis(this,<?php echo $usersingleid; ?>)" class="clrgrn" title="active"><i class="fa fa-check-circle"></i></a>
<?php } ?>
</span>
<a href="edit_ad.php?uid=<?php echo md5(uniqid()); ?>&aid=<?php echo base64_encode($usersingleid);?>" class="clrblue" title="edit"><i class="fa fa-pencil-square"></i></a>
<a onclick="dltthis(this,<?php echo $usersingleid; ?>)" class="clrred" title="delete"><i class="fa fa-trash"></i></a>
<a href="all_ads.php?&uid=<?php echo md5(uniqid());?>&userid=<?php echo base64_encode($usersingleid); ?>" class="clrgrn" title="View Ads"><i class="fa fa-eye"></i></a>
</td>
</tr>
<?php }
}
// echo "<pre>"; print_r($queryGetReaults);
}
//PAGINATION ENDS HERE
Comments
Post a Comment