Im actually working on a ticket system. And for the admins i have an page where i list the first 15 rows in database.
I know i can do it manual with creating for every 15 row an new Page like:
SELECT * FROM support ORDER BY `date` DESC LIMIT 15
Next 15 rows:
SELECT * FROM support ORDER BY `date` DESC LIMIT 15, 15
and so forth.
But i know there is an dynamic way.. Can anybody explain me how ? At other sites i got locations like this:
?pageNo=1578
So i mean, how it is possible, if my rows go over the 15 on the first page, that it created individual a next page? Hope you know what i mean.
Greets Kevin
I have this code in one of my old files. Hope it can help:
<div class='div-table-responsive'>
<table width='100%' class='tagtable liste listwithfilterbefore sortablesimstats'>
<thead>
<th class='left nowrap'><?php print $langs->trans('Customer'); ?></th>
<th class='center nowrap'><?php print $langs->trans('Date'); ?></th>
<th class='center nowrap'><?php print $langs->trans('Product'); ?></th>
<th class='center nowrap'><?php print $langs->trans('Qty2'); ?></th>
<th class='center nowrap'><?php print $langs->trans('Reason'); ?></th>
<th class='right nowrap'><?php print $langs->trans('User'); ?></th>
<th></th>
</thead>
<tbody>
<?php
$results_per_page = 15;
$resql = $db->query($object->getWaste()) or die($db->error);
if (!$resq) {
dol_print_error($db);
}
$number_of_result = mysqli_num_rows($resq);
$number_of_page = ceil($number_of_result / $results_per_page);
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
$page_first_result = ($page - 1) * $results_per_page;
$resql2 = $db->query($object->getWaste2($page_first_result, $results_per_page));
if (!$resql2) {
dol_print_error($db);
}
while ($row2 = $resql2->fetch_assoc()) {
//output result as table row
}
?>
</tbody>
</table>
</div>
<div class="pagination">
<ul>
<li class="pagination paginationpage paginationpageleft">
<?php
$previous_page = $page - 1;
if ($previous_page == 0) {
print '<i class="fa fa-chevron-left" title=""></i>';
} else {
print '<i class="fa fa-chevron-left" title=""></i>';
}
?>
</li>
<li class="pagination">
<?php print $page; ?>
</li>
/
<li class="pagination">
<?php print $number_of_page; ?>
</li>
<li class="pagination paginationpage paginationpageleft">
<?php $next_page = $page + 1;
if ($next_page > $number_of_page) {
print '<i class="fa fa-chevron-right" title=""></i>';
} else {
print '<i class="fa fa-chevron-right" title=""></i>';
}
?>
</li>
</ul>
</div>
Related
I've been creating a CMS blog and this is one of the pages that accessible by the admins. I'm trying to implement pagination in it.
As you can see, it shows the latest six posts in the first page with the IDs from 1 to 6, but when I click on the forward button, it shows the IDs 1 and 2 again for the other posts in which it should be 7 and 8. Could someone tell me what's causing this bug?
First page
Second page
<!-- Right Side Area Start -->
<div class="col-lg-10">
<div class="card-body bg-info">
<h2 class="large-georgia-white-bold">Top Posts</h2>
</div>
<table class="table table-striped table-hover">
<thead class="thead-dark small-times-white">
<tr>
<th>No.</th>
<th>Title</th>
<th>Date&Time</th>
<th>Author</th>
<th>Comments</th>
<th>Details</th>
</tr>
</thead>
<?php
$SrNo = 0;
global $ConnectingDB;
// Query When Pagination is Active i.e Dashboard.php?page=1
if (isset($_GET["page"])) {
$Page = $_GET["page"];
if ($Page==0||$Page<0) {
$ShowPostFrom=0;
}else{
$ShowPostFrom=($Page*6)-6;
}
$sql ="SELECT * FROM posts ORDER BY id desc LIMIT $ShowPostFrom,6";
$stmt=$ConnectingDB->query($sql);
}
// The default SQL query
else{
$sql = "SELECT * FROM posts ORDER BY id desc LIMIT 0,6";
$stmt=$ConnectingDB->query($sql);
}
while ($DataRows=$stmt->fetch()) {
$PostId = $DataRows["id"];
$DateTime = $DataRows["datetime"];
$Author = $DataRows["author"];
$Title = $DataRows["title"];
$SrNo++;
?>
<tbody class="small-times-black">
<tr>
<td><?php echo $SrNo; ?></td>
<td><?php echo $Title; ?></td>
<td><?php echo $DateTime; ?></td>
<td><?php echo $Author; ?></td>
<td>
<?php $Total = ApproveCommentsAccordingtoPost($PostId);
if ($Total>0) {
?>
<span class="badge badge-success">
<?php
echo $Total; ?>
</span>
<?php } ?>
<?php $Total = DisApproveCommentsAccordingtoPost($PostId);
if ($Total>0) { ?>
<span class="badge badge-danger">
<?php
echo $Total; ?>
</span>
<?php } ?>
</td>
<td> <a target="_blank" href="FullPost.php?id=<?php echo $PostId; ?>">
<span class="btn btn-info">Preview</span>
</a>
</td>
</tr>
</tbody>
<?php } ?>
</table>
<!-- Right Side Area End -->
<!-- Pagination -->
<nav>
<ul class="pagination pagination-lg">
<!-- Creating Backward Button -->
<?php if( isset($Page) ) {
if ( $Page>1 ) {?>
<li class="page-item">
«
</li>
<?php } }?>
<?php
global $ConnectingDB;
$sql = "SELECT COUNT(*) FROM posts";
$stmt = $ConnectingDB->query($sql);
$RowPagination = $stmt->fetch();
$TotalPosts = array_shift($RowPagination);
//echo $TotalPosts."<br>";
$PostPagination=$TotalPosts/6;
$PostPagination=ceil($PostPagination);
//echo $PostPagination;
for ($i=1; $i <= $PostPagination ; $i++) {
?>
<li class="page-item">
<?php echo $i; ?>
</li>
<?php } ?>
<!-- Creating Forward Button -->
<?php if ( isset($Page) && !empty($Page) ) {
if ($Page+1 <= $PostPagination) {?>
<li class="page-item">
»
</li>
<?php } }?>
</ul>
</nav>
</div>
</div>
</section>
<!-- Main area end -->
Your script always reassigns $SrNo = 0 when the page is loaded and starts over. You should add the page * 6 value to it so it becomes aware of the offset. In fact, you're already using that logic for $ShowPostFrom, so you can simply assign the same value to $SrNo and it should work:
if ($Page==0||$Page<0) {
$ShowPostFrom=0;
}else{
$ShowPostFrom=($Page*6)-6;
}
$SrNo = $ShowPostFrom; // <- this is what you should add
If you don't mind modifying $ShowPostFrom, you can drop $SrNo completely and just use $ShowPostFrom to show the number.
I have listing in my HTML which is displayed from database. My listing code is like below:
<?php
require('admin/db_config.php');
$sql = "SELECT * FROM image_gallery";
$images = $mysqli->query($sql);
while($image = $images->fetch_assoc()){
?>
<div class="aamir"><span><img style="height:40px; width:55px; " class="img-responsive" alt="" src="admin/uploads/<?php echo $image['image'] ?>" /></span><small><?php echo $image['title']; ?></small><strong style="width:40%; height: 35%;"><em class="icon icon-chevron-down"></em><p style="margin-top: -5%;"> <?php echo $image['description']; ?> </p></strong>
<a
class="zayan" target="_blank" href="<?php echo $image['url']; ?>">VISIT</a>
</div>
<?php } ?>
Now I have given pagination for the same because the data is too much and I want it to go to next page. So I have given pagination. Pagination code is below:
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$conn=mysqli_connect("localhost","root","","sample");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$total_pages_sql = "SELECT COUNT(*) FROM image_gallery";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
$sql = "SELECT * FROM image_gallery LIMIT $offset, $no_of_records_per_page";
$res_data = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($res_data)){
//here goes the data
}
mysqli_close($conn);
?>
<ul class="pagination">
<li>First</li>
<li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
Prev
</li>
<li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
Next
</li>
<li>Last</li>
</ul>
But still the pagination is not working, no error is shown, the whole data is being displayed in one page and the pagination is like static.
I wrote this code to search in rows of the table:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$page_max = 8;
if (isset($_POST['search'])){
$search = $_POST['searchValue'];
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers WHERE name LIKE '%$search%'");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
$numberOfRecords = $page_max;
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = $page * $page_max;
}
else{
$page = 0;
$start = $page * $page_max;
}
$customers = $database->getUsers("SELECT * FROM customers WHERE name LIKE '%$search%' LIMIT $start, $numberOfRecords");
}
else{
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
$numberOfRecords = $page_max;
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = $page * $page_max;
}
else{
$page = 0;
$start = $page * $page_max;
}
$customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
if(isset($_GET['page']) && $_GET['page'] > 0){
$previous = $_GET['page'] - 1;
echo '<li class="page-item"><a class="page-link" href="?page='. $previous.'">Previous</a></li>';
}
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
if(isset($_GET['page']) && $_GET['page'] < $numberOfPages - 1){
$page = $_GET['page'];
$next = $page + 1;
echo '<li class="page-item"><a class="page-link" href="?page='.$next.'">Next</a></li> ';
}
elseif(!isset($_GET['page'])){
echo '<li class="page-item"><a class="page-link" href="?page=1">Next</a></li> ';
}
?>
</ul>
</div>
Searching works fine when I'm on page 0, but when I'm at page 2 I can only search for records on that page (I think this is because of the limit in my query) besides that when I search for 'A' it returns 7 pages but if I click on the seconde page it won't load the search results but the main list with customers on page 2.
Could anyone help me fix these issues?
First of all: sanitize your input. Using POST or GET in database queries can be very, very dangerous.
Second: DRY, you probably can do this with half the code you are using.
The reason why you are "losing" your search is because your pagination doesn't carry over the search term.
Add it to the links in the pagination and switch from POST to GET:
<?php
$searchString = '';
if($_GET['searchValue']){
$searchString = '&searchValue=' . $_GET['searchValue'];
}
if(isset($_GET['page']) && $_GET['page'] > 0){
$previous = $_GET['page'] - 1;
echo '<li class="page-item"><a class="page-link" href="?page='. $previous.$searchString.'">Previous</a></li>';
}
// more code
'... href="?page='.$i.$searchString'" ...'
'... href="?page='.$next.$searchString'" ...'
Note that I use $_GET['searchValue'] instead of POST. in most cases there is no advantage in using POST for searches (quite the opposite, if you use GET you can track it in Google Analytics f.ex.). So make sure you change your form method to get and if (isset($_POST['search'])){ to $_GET['searchValue']. If you insist on using POST, you can use $_REQUEST, which holds values from GET and POST.
I am doing ajax pagination in cakephp 3.2
I have done code for forward move of pagination, by getting the last id .
If i want to go backward ,the pagination will not work ,i know .
How can i do it in a proper way so that it will work for both direction as well as direct click on any pagination index.
Below i have attached some of my codes ,which is working properly only for forward move of pagination.
I know the code won't work for backward move.
How can i do it?
///////////////////////////////////PAGINATION STARTS HERE/////////////////////////////////////////////////
if(isset($_POST["page"])){
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
}else{
$page_number = 1; //if there's no page number, set it to 1
}
$item_per_page=5;
$get_total_rows = $this->Orders->find('all')->where($condition)->count(); //hold total records in variable
$total_pages = ceil($get_total_rows/$item_per_page);
$page_position = (($page_number-1) * $item_per_page);
if($page_number>1)
{
$condition[] = ['Orders.id >' => $_POST["lastId"]];
}//this one fetch all list greater than last id
$Lists = $this->Orders->find('all')->where($condition)->order(['Orders.id' => 'ASC'])->limit($item_per_page)->toArray();
Thank you
Your Controller Action code should be
$this->paginate = [
'order'=>[
'field_name'=>'desc'
]
];
$condition = [];
$query = $this->YourModel->find()->where($conditions);
$this->set('records', $this->paginate($query));
In view your code should be for listing part only, I dont know whta is your HTML structure but you can follow this, and dont forget about id=pagination_list_container in parent of your table and pagination link code.
<div class="panel-body" id="pagination_list_container">
<div class="inner-spacer">
<table class="table table-striped table-hover margin-0px">
<thead>
<tr>
<th><?php echo $this->Paginator->sort('field_1', 'Column 1') ?></th>
<th><?php echo $this->Paginator->sort('field_2', 'Column_2') ?></th>
<th><?php echo $this->Paginator->sort('field_3', 'Column_3') ?></th>
</tr>
</thead>
<tbody>
<?php
if (empty($records->toArray())) {
?>
<tr><td colspan="100%" class="text-danger text-center">No record found</td></tr>
<?php
} else {
foreach ($records as $record):
?>
<tr>
<td><?php echo $record->field_1 ?></td>
<td><?php echo $record->field_2; ?></td>
<td><?php echo $record->field_3; ?></td>
</tr>
<?php endforeach; ?>
<?php } ?>
</tbody>
</table>
</div>
<div class="row">
<div class="col-md-6">
<ul class="pagination">
<?php
$this->Paginator->templates([
'current' => '<li class="active"><a>{{text}}</a></li>',
'number' => '<li>{{text}}</li>'
]);
echo $this->Paginator->prev('«');
echo $this->Paginator->numbers();
echo $this->Paginator->next('»');
?>
</ul>
</div>
<div class="col-md-6 text-right">
<div class="mt30">
<?php
echo $this->Paginator->counter(
'Page {{page}} of {{pages}}, showing {{start}} to {{end}} of {{count}}'
);
?>
</div>
</div>
</div>
</div>
Make AJAX behaviour in your view
You have to Apply Some Javascript event for ajax behaviour into #pagination_list_container
$(document).ready(function(){
$("document").on('click','#pagination_list_container .pagination li a, #pagination_list_container table th > a', function(e){
e.preventDefault();
var link= $(this).attr('href');
if(link!="")
{
$("#pagination_list_container").load(link+ "#pagination_list_container", function(){
console.log("data loaded");
})
}
return false;
});
});
I'm about to display comments on a page, everything works fine but I want to display the comments from the user first. So, how do I display the user comments from the user_id first? I'm using a while loop to display the comments.
I would be very grateful if anyone could help me. :)
<?php
$sql = "SELECT * FROM `comments` WHERE `post_id`=$pid AND `active`=1 ORDER BY ups-downs DESC";
$result = $mysqli->query($sql);
$count = $result->num_rows;
if($count == 1){
$counttext = "1 Comment";
}else{
$counttext = $count ." Comments";
}
?>
<div class="media-area media-area-small">
<h3 class="text-center"><?php echo $counttext; ?></h3>
<?php
while($row = $result->fetch_assoc()){
$uid = (int)$row["user_id"];
$user = get_user_info($mysqli,$uid);
?>
<div class="media">
<a class="pull-left" href="#">
<div class="avatar">
<img class="media-object" src="<?php echo $user["picture"]; ?>" alt="...">
</div>
</a>
<div class="media-body">
<table width="100%">
<tr valign="middle">
<td align="left"><h4 class="media-heading text-left"><?php echo fb_clear_name($mysqli, $user["id"]); ?></h4></td>
<td align="right"><h6 class="pull-right text-muted"><?php echo $row["ups"] - $row["downs"]; ?> bits</h6></td>
</tr>
</table>
<p><?php echo htmlentities($row["content"]); ?></p>
<div class="media-footer">
<i class="fa fa-reply"></i> Reply
<a href="#" class="pull-right text-muted">
<?php echo $row["timestamp"]; ?>
</a>
</div>
</div>
</div>
<?php
}
?>
Greetings
You can prefilter the data. For example use
$user_comments=array();
$other_comments=array();
while($row = mysql_fetch_assoc($result))
{
if($row['user']==$current_user)
{
$user_comments[]=$row;
}
else
{
$other_comments[]=$row;
}
}
$comments=array_merge($user_comments,$other_comments);
Then you can display the information the way you want to.