I am sorry if the question is very basic or someone might post this question before but i can't figure out this code. Any help appreciated
Pagination works good but it shows 2 more blank pages even there are no posts on that page.
php:
<?php
$page = (isset($_GET['page']) && $_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$perpage = 10;
//$limit = ($page > 1) ? (((($page *2)+1)-3)* $perpage) - $perpage : 0;
$limit = ((($page*2)+1)-3)*$perpage;
$query = mysqli_query($dbc, "SELECT SQL_CALC_FOUND_ROWS * FROM test WHERE Category='Pharma' LIMIT {$limit}, {$perpage}");
$records = mysqli_fetch_all($query);
$total = mysqli_query($dbc, "SELECT FOUND_ROWS() as total");
$total = mysqli_fetch_assoc($total)['total'];
$pages = ceil($total/$perpage);
?>
//prev
<?php
if($page>1){
?>
<a class="page-link" href="?page=<?php $pagep = $page -1; echo $pagep; ?>" tabindex="-1">Previous</a>
<?php
}
?>
//next
<?php
if($page<$pages){
?>
<a class="page-link" href="?page=<?php $pagen = $page +1; echo $pagen; ?>">Next</a>
<?php
}
?>
I tried to change the query as i know i fetch $total up here not the $query but when i fetch $query pagination not working. Thanks in advance.
Related
sometimes last year i went through a php mvc tutorial done by jream the platform looks so simple and convenient. Now am building a web application with this mvc platform and now am stoked in the aspect of pagination, while trying to paginate records from the PDO database.
source codes below.
model:
function paginate() {
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 50 ? (int) $_GET['per-page'] : 5;
$start=($page > 1) ? ($page * $perPage) - $perPage :0;
$articles = $this->db->prepare("Select SQL_CALC_FOUND_ROWS id,title from tblpost limit {$start},{$perPage}");
$articles->execute();
$articles = $articles->fetchAll(PDO::FETCH_ASSOC);
$total = $this->db->query("SELECT FOUND_ROWS() as total")->fetch() ['total'];
return $pages = ceil($total / $perPage);
}
controller:
function index() {
$this->view->title = "Home";
$this->view->home = $this->model->loadpost();
$this->view->pgcount= $this->model->paginate();
$this->view->render("index/index");
}
view:
<div>
<?php for($x=1;$x <= $pages; $x++): ?>
<?php echo $x;?>
<?php endfor; ?>
</div>
i need to make the post paginate using this particular php mvc
I am having difficulties understanding how to limit the number of pages with pagination. I am trying to list my articles in my DB but I have over 500 pages! I would only like to show a max of about 15 page numbers at a time. I can't seem to understand the answers given in the other questions.
Here is my code for the Pagination:
<?php
require 'core/init.php';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if(!isset($_GET['page'])){
$_GET['page'] = 1;
}
if(! (int)$_GET['page']){
$_GET['page'] = 1;
}
if($_GET['page'] < 1){
$_GET['page'] = 1;
}
$perPage = 18;
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$articles = DB::getInstance()->query("SELECT SQL_CALC_FOUND_ROWS * FROM articles ORDER BY added DESC LIMIT {$start}, {$perPage}");
foreach($articles->results() as $article){ ?>
<div class="article-container">
<h2><?php echo $article->title; ?></h2>
</div>
<?php
}
// pagination
$total = DB::getInstance()->query("SELECT FOUND_ROWS() as total");
$total = $total->results()[0];
foreach ($total as $total => $value) {
}
$pages = ceil($value / $perPage);
$maxpage = 15;
?>
<div class="pagination">
<?php if($_GET['page'] >= 2){
echo 'Prev';
}
for ($i=1; $i <= $pages; $i++) : ?>
<a href="?page=<?php echo $i; ?>" <?php if($page === $i) { echo 'class="pageSelected"'; } ?>><?php echo $i; ?></a>
<?php endfor;
if((int)$_GET['page'] != $i - 1 ){
echo 'Next';
}
?>
</div>
This works as expected but it only spits out the number of pages from 1 to 500. What is the best/easiest way to show something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 ... >
?
You need to change your for-cycle, the starting point and ending point
$count = 9;
$startPage = max(1, $page - $count);
$endPage = min( $pages, $page + $count);
for($i = $startPage; $i < $endPage; $i++) {
// write out the link to the page
}
then if the page is 250 it will show only 241, 242, ..., 250, ..., 257, 258
I'm trying to make a website with movies in it, everything is fine but i have just 1 little problem,when ever i make a website, i do all work in my local computer test it then I upload the web, the code below is for paging with query it works fine in WAMP (locally). But when I upload the paging code to my web server it says NOT EXIST.
it shows the else part,whats the problem?
<?php
$per_page = 35;
$page = 1;
if (isset($_GET['page']))
{
$page = intval($_GET['page']);
if($page < 1) $page = 1;
}
$start_from = ($page - 1) * $per_page;
$con= mysql_connect("localhost","sarya_asad","Thisisfor123");
mysql_select_db('saryaal_com_movies',$con);
$current_items = mysql_query( "SELECT * FROM `english` LIMIT $start_from, $per_page");
if( mysql_num_rows($current_items) > 0)
{
while($item = mysql_fetch_assoc($current_items))
{
?>
<tr>
<td> <strong><a href="english/english-preview.php?id=<?php echo$item['id']?>" ><?php echo $item['title'] ;?></a> </strong></td>
<td> <strong> <?php echo $item['year'] ;?> </strong></td>
<td> <strong> <?php echo $item['quality'] ;?> </strong> </td>
</tr>
<tr><td>
<?php
}
}
else
{
echo 'this page does not exists';
}
$total_rows = mysql_query("SELECT COUNT(*) FROM `english`");
$total_rows = mysql_fetch_row($total_rows);
$total_rows = $total_rows[0];
$total_pages = $total_rows / $per_page;
$total_pages = ceil($total_pages); # 19/5 = 3.8 ~=~ 4
for($i = 1; $i <= $total_pages; ++$i)
{
echo "<a href='temp2.php?page=$i' class='pagNumActive'>$i</a> ";
}
?>
<?php if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="") {
$sql=mysql_query("select * from job where AND status='Active'");
}
$per_page = 5;
$page = 1;
if (isset($_GET['page'])) {
$page = intval($_GET['page']);
if($page < 1) $page = 1;
}
$start_from = ($page - 1) * $per_page;
if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="")
{
$current_items=mysql_query("select * from job LIMIT $start_from, $per_page"); } $start_from, $per_page");
if( mysql_num_rows($current_items)>0) { while($arr=mysql_fetch_array($current_items)) {
?>
<?php include("include/result.php") ?>// result u want to display
<?php
}
} else {
echo 'Data does not exists'; }
if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="") {
$total_rows=mysql_query("select COUNT(*) from job where AND status='Active'");
}
$total_rows = mysql_query("SELECT COUNT(*) FROM job");
$total_rows = mysql_fetch_row($total_rows);
$total_rows = $total_rows[0];
$total_pages = $total_rows / $per_page;
$total_pages = ceil($total_pages);
# 19/5 = 3.8 ~=~ 4
echo "<div style='margin-left:280px;'>";
echo "Page : ";
for($i = 1; $i <= $total_pages; $i++) {
echo "[<a style='text-decoration:none' href='search_result.php?page=$i' class='pagNumActive'>$i</a> ]";
}
echo "</div>";
?>
This is your problem:
$con= mysql_connect("localhost","sarya_asad","Thisisfor123");
mysql_select_db('saryaal_com_movies',$con);
You need to change the host details. localhost is the local server on your machine.
change this
$start_from = ($page - 1) * $per_page;
to
$start_from = ($page) * $per_page;
you might want to use something like:
$per_page = 35;
$start_from = $page * $per_page - $per_page;
Try this logic for pagination:
Your goal is to offset by the number of images you want to display on each page.
So let's say you want to display 6 movie thumbnails on a page... You'd have:
$videos_per_page = 6
$pageNumber = (isset($_GET['page']) ? ($_GET['page']) : 1);
And your offset would be:
$offset = $videos_per_page * $pageNumber
(6 videos * page 0... so you're offsetting 0 videos. That's good because you want to display the first 6 videos in your data structure on page 0).
So now that you have your offset value... You need to set your array pointer to the correct spot... Loop through your database rows storing your movies and move your pointer by your offset value... Store that in $videos_to_offset...
while ($videos_per_page < $offset && ($row = $Query_Result->fetch_assoc())) {
$videos_to_offset++;
}
Now you can loop through your database rows, outputting your videos from wherever your offset array pointer left off:
$video_counter = 0;
while ($video_counter < $videos_per_page && ($row = $Query_Result->fetch_assoc())) {
echo $row['videopath'];
$video_counter++;
}
Something like that.
The pagination here works fine, but addition to pagination, as the limit per page is 3 suppose total 8 items in database so i want to display "
showing 1 t0 3 item of 8" on page one, "showing 4 to 6 items of 8" on page two and so on. please help
$recordsLimit = 3;
$page = isset($_GET['page']) ? intval($_GET['page']): 1;
$totalProducts = countProducts($selectedCategoryId);
$totalPages = ceil($totalProducts / $recordsLimit);
$pageNumber = $recordsLimit * ($page - 1);
$products = getProductsByCatId($selectedCategoryId, $pageNumber, $recordsLimit);
<?php if($totalProducts > $recordsLimit) : ?>
<div class="pagination">
<span>Page <?php echo $page.' of '.$totalPages; ?></span>
<?php for($i=1; $i <= $totalPages; $i++) :
if($i == $page) { ?>
<strong><?php echo $i; ?></strong>
<?php } else { ?>
<?php echo $i; ?>
<?php }
endfor; ?>
<?php endif; ?>
Try:
echo "Showing ".( $page == 1 ? 1 : ($page -1) * $recordsLimit +1 )." to ".($page * $recordsLimit)." item of ".$totalProducts;
Simply use this:
<?php $firstNum = (($page-1)*$recordsLimit+1) ?>
showing <?php echo $firstNum; ?> to <?php echo $firstNum+2;?> items of <?php echo $totalProducts;?>
To fix the problem with Tom's code where the last page has an incorrect "to" number I added an offset variable. For this to work the $recordsLimit variable has to match the "showposts" argument in the query. The OP did not show his query so I'm showing the one I used.
$total_results = $wp_query->found_posts;
$recordsLimit = 10;
$args['post_type'] = 'listings';
$args['showposts'] = $recordsLimit;
$args['paged'] = $paged;
$wp_query = new WP_Query($args);
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$countoffset = ($page * $recordsLimit)-($total_results);
$countfrom = ( $page == 1 ? 1 : ($page -1) * $recordsLimit +1 );
$countto = ($page * $recordsLimit);
if(($total_results - $countfrom) < $recordsLimit) {$countto = ($countto - $countoffset);}
echo 'Showing '.$countfrom.' to '.$countto.' of '.$total_results.' total results';
I am doing my first pagination implementation to my site. I feel like I am very close and just confusing my logic or mistaking the placement or actual value of some variables.
Here is the code:
$pgsize_wave = 40;
$pg_wave = (is_numeric($_GET["p"]) ? $_GET["p"] : 1);
$start = ($pg_wave-1)*$pgsize_wave;
$waves = mysql_query("SELECT * FROM `CysticAirwaves` LIMIT $start, $pgsize_wave");
$waves_total = mysql_query("SELECT COUNT(1) FROM `CysticAirwaves`");
$waves_total = mysql_fetch_row($waves_total);
$waves_total = $waves_total[0];
$max_pages = $waves_total / $pgsize_wave;
$max_pages = ceil($waves_total/$pgsize_waves);
?>
<div id="all_page_turn">
<ul>
<?php if($waves_total > 40 && $pg_wave >= 40) { ?>
<li class="PreviousPageBlog round_10px">
Previous Page
</li>
<?php } ?>
<?php if($waves_total > 40 && $pg_wave < ($waves_total-40)) { ?>
<li class="NextPageBlog round_10px">
Next Page
</li>
<?php } ?>
</ul>
</div>
To clarify any confusion, an airwave is a post from a user and I want to limit 40 per page. If it helps here is the query that pulls an airwave on the same page:
$query = "SELECT * FROM `CysticAirwaves` WHERE `FromUserID` = `ToUserID` AND `status` = 'active' ORDER BY `date` DESC, `time` DESC";
$request = mysql_query($query,$connection);
$counter = 0;
while($result = mysql_fetch_array($request)) {
$replies_q = "SELECT COUNT(`id`) FROM `CysticAirwaves_replies` WHERE `AirwaveID` = '" . $result['id'] . "' && `status` = 'active'";
$request2 = mysql_query($replies_q,$connection);
$replies = mysql_fetch_array($request2);
$replies_num = $replies['COUNT(`id`)'];
$counter++;
$waver = new User($result['FromUserID']);
Thanks so much in advance.
This is pseudo code but hopefully explains the logic.
$total = SELECT COUNT(*) FROM `waves`
$currentPage = 1 // changes through page parameter in URL
$wavesPerPage = 40
$totalPages = ceil($total / $wavesPerPage)
$offset = ($wavesPerPage * ($currentPage - 1)) + 1
$waves = mysql_query("SELECT * FROM `waves` LIMIT $offset, $wavesPerPage");
while ($row = mysql_fetch_assoc($waves)) {
echo $row['wave']
…
}
To output the pagination links:
if ($totalPages > 1) {
if ($currentPage > 1) {
printf('Previous', $currentPage - 1);
}
for ($i = 1; $i <= $totalPages; $i++) {
$class = ($i == $currentPage) ? 'selected' : null;
printf('Page %1$u', $i, $class);
}
if ($currentPage < $totalPages) {
printf('Next', $currentPage + 1);
}
}