so i have this loop like this
<?php for($i = 0; $i <$size; $i++) { ?>
<li>
<a pagenumber="<?php $i+1?>" class="pagination" href="#"><?php $i+1?></a></li>
<?php } ?>
so it kinda looks like this:
1 2 3 4 5 6 7 8
but the problem here is when there is like say a 100 page there is alot of numbers how can i do somthing like this
1 2 3 4 5 .... 98 99 100
and not have numbers from 1 to 100 just keep showing up
so for example when i click 5 then it should show
6 7 8 9...98 99 100
or somthing better i am not able to figure how to go about this
You don't need to loop.
You can use array_slice and implode to get the expected output.
I added $n to give you the option to output one page previous to the selected if it's not page 1 that is selected.
$pages = range(1,100); // range(1, $size);
$page = 1; // page selected by user
if($page ==1){
$n = 0;
}else{
$n = 1;
}
echo implode(" ", array_slice($pages, $page-1-$n, 5)) . " . . . . " . Implode(" ", array_slice($pages, -3));
Output:
1 2 3 4 5 . . . . 98 99 100
With page 5 selected:
4 5 6 7 8 . . . . 98 99 100
https://3v4l.org/doZbI
If you want to stay in the loop then you can use two loops and one optional echo.
$size =100;
$page = 5;
// Echo one page prior to selected page
if($page != 1){
Echo '<a pagenumber="' . ($page-1) .'" class="pagination" href="#">' . ($page-1) . '</a></li>';
Echo ' . '; // show dot for current page
}
// Echo +1 -> +5
For($i = $page+1; $i < $page+6; $i++){
Echo '<a pagenumber="' . $i .'" class="pagination" href="#">' . $i . '</a></li> ';
}
Echo '. . . . ';
// Echo last three pages
For($i = $size-3; $i <= $size; $i++){
Echo '<a pagenumber="' . $i .'" class="pagination" href="#">' . $i . '</a></li> ';
}
https://3v4l.org/1P0W3
Related
I want to manipulate the end output of a while loop.
In this example, i am using number 5 in place of a variable.
<?php $i=1; while($i<=5) {
echo "col-md-" . round(12/5) . "<br>";
$i++;
} ?>
giving output of
col-md-2
col-md-2
col-md-2
col-md-2
col-md-2
---> I want to show 4 here by applying the below math >>
12-(5-1)*round(12/5)
Can you please help me with this. is it possible?
A help will be very much appreciated.
An alternative is to use a for() loop and end it one earlier and then output an extra column after...
$count = 5; // Number of columns
for ( $i = 1; $i < $count; $i++ ) {
echo "col-md-" . round(12/$count) . "<br>";
}
echo "col-md-" . (12-($count-1)*round(12/$count)) . "<br>";
Which gives...
col-md-2<br>col-md-2<br>col-md-2<br>col-md-2<br>col-md-4<br>
One option could be to check if the variable $i equals 5 before it gets incremented:
$i=1;
while($i<=5) {
echo "col-md-" . round(12/5) . "<br>";
if ($i === 5) {
echo "col-md-" . (12-(5-1)*round(12/5));
}
$i++;
}
If it is always at the end, you can just add it after the while loop:
echo "col-md-" . (12-(5-1)*round(12/5));
This is the final code, which solved my issue.
$count = 1; // Number of columns
for ( $i = 1; $i < $count; $i++ ) {
echo "col-md-" . floor(12/$count) . "<br>";
}
if (($count == 5 || 7 || 8 || 9 || 10 || 11)) {
echo "col-md-" . (12-($count-1)*floor(12/$count)) . "<br>";
}
Thanks to #nigel and #thefourthbird.
Here is how the index will show the links for pagination:
total record = 40
per_page = 2
and now for the link generating:
<?php
if ($pagination->total_pages() > 1) {
if ($pagination->has_previous_page()) {
echo "<a href='index.php?page=";
echo $pagination->previous_page();
echo "&refone=" . $refone ."'>« PREVEOUS</a> ";
}
for ($i = 1; $i <= $pagination->total_pages(); $i++) {
if ($i == $page) {
echo " <span class=\"selected\">{$i}</span> ";
} else {
echo " <a href='index.php?page=" . $i . "&refone=" . $refone ."'>" . $i . "</a> ";
}
}
if ($pagination->has_next_page()) {
echo " <a href='index.php?page=";
echo $pagination->next_page();
echo "&refone=" . $refone."'>NEXT »</a> ";
}
}
?>
metion code will generate the links for pagination but the problem is it is showing many links
for example:
we have 40 record in each page we need to show 2 records so it will generate 20 links( for ($i = 1; $i <= $pagination->total_pages(); $i++) {) here is the code which will calculate for the links but I want to echo only 8 links the rest should be hid like
1-2-3-4-5-6-7-8-Next
prev-2-3-4-5-6-7-8-9-next
but its showing all
I found my answer::
here I need to change the code for the for statement:
for ($i = $page - $per_page; $i <= $page + $per_page; $i++){
this will work for me.
hey guys my English is not very good 'but i try to explain my self clear.
i'm creating pagination and all of my code is working perfect.
The problem is, that i want to display only five numbers of pages and on click on next button i hide one and showing new one. it's looks like that.
next 12345 prev
next 23456 prev
Thank's for advice guys.
Here is my code :
<?php
$dbh = new PDO("mysql:host=localhost;dbname=northwind", "root", "123");
$query = $dbh->prepare("SELECT ContactName FROM Customers");
$query->execute();
$numRows = $query->rowCount();
if (isset($_GET['pn'])) {
$pn = $_GET['pn'];
} else {
$pn = 1;
}
$startPage = 1;
$perPage = 9;
$lastPage = ceil($numRows / $perPage);
if ($pn < 1) {
$pn = 1;
} else if ($pn > $lastPage) {
$pn = $lastPage;
}
$controls = '';
if ($pn != $lastPage) {
$controls .= '<a id="next" href="' . $_SERVER['PHP_SELF'] . '?pn=' . ($pn + 1) . '"> next </a>';
}
for ($i=1; $i <= $lastPage; $i++) {
if ($i == $pn) {
$background = ' red;';
} else {
$background = ' green;';
}
$controls .= '<a id="page_' . $i . '" data-page="' . $i . '" class="num" style="background:' . $background . ' " href="' . $_SERVER['PHP_SELF'] . '?pn=' . $i . '"> ' . $i . '</a>';
}
if ($pn != $startPage) {
$controls .= ' prev ';
}
$controls .= "PAGE " . $pn . " of " . $lastPage ;
$limit = "LIMIT " . ($pn-1) * $perPage . ', ' . $perPage;
$query2 = $dbh->prepare("SELECT ContactName FROM Customers " . $limit . "");
$query2->execute();
$outputList = '';
while($row = $query2->fetch(PDO::FETCH_OBJ)){
$outputList .= '<h1>' . $row->ContactName . '</h1><hr />';
}
I would suggest you to use more easy approach.
Lets consider a page contains 25 results.
That means:
1st Page range is: 1-25
2nd Page range is: 26-50
and so on..
Now when a user request page 2, we should display him the results of 25-49. this means the results of (page-1)*25 till page*25-1 = results in 25-49.
Now all we need is how to tell the SQL to take those into consideration.
SELECT ContactName FROM Customers LIMIT 25 OFFSET 25;
Will produce use the desired result.
Now all you need is just pass the requested page with pn as you already did. And print the next 1-3(whatever) next\prev pages.
To know how many pages there are, just do another count sql query that will give you the result and divide it by 25.
I want to limit the pages out of the total number of pages.
Example: 1 2 3 4 5 6 >> out of 30
Example2: << 5 6 7 8 9 10 >> out of 30
Here is my code:
$page = !empty($_GET['page']) ? (int) $_GET['page'] : 1;
// records per page
$per_page = 5;
// total records in database
$total_count = Mp3_Model::count_all();
// instantating the $pagination
$pagination = new Pagination($page, $per_page, $total_count);
// find the records for this page
$sql = "SELECT * FROM mp3 ";
$sql .= " LIMIT {$per_page} ";
$sql .= " OFFSET {$pagination->offset()}";
$mp3s = Mp3_Model::find_by_sql($sql);
foreach ($mp3s as $mp3) {
echo $mp3->titlu;
}
and this is the pagination:
<?php
if ($pagination->total_pages() > 1) {
if ($pagination->has_previous_page()) {
echo '<li>«</li>';
}
for ($i = 1; $i <= $pagination->total_pages(); $i++) {
echo '<li';
if ($_GET['page'] == $i) {
echo ' class="active"';
}
echo '>' . $i . '</li>';
}
if ($pagination->has_next_page()) {
echo '<li>» </li>';
}
}
?>
You can use jQuery pagination, it is a much better solution.
I use this code for my custom paging :
global $wpdb, $table_prefix, $current_user;
get_currentuserinfo();
$umail = $current_user->user_email;
$paged = $wpdb->get_results("SELECT * FROM {$table_prefix}comments WHERE comment_author_email = '$umail'");
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$pages = COUNT($paged);
$pages = ceil($pages / 2);
$querystring = "";
foreach ($_GET as $key => $value) {
if ($key != "page") $querystring .= "$key=$value&";
}
// Pagination
for ($i = 1; $i <= $pages; $i++) {
echo "<a " . ($i == $page ? "class=\"selected\" " : "");
echo "href=\"?{$querystring}page=$i";
echo "\">$i</a> ";
}
This code paginate my comments look like this : 1 2 3 4 5 6 7 8 9 10 11
How can change code to get paginate look like this: 1 2 3 ... 11
Thanks for any help.
Try the following loop:
$page = 8;//current page
$pages = 15;//count of all pages
$batch_middle = 5;//the approximate number of pages in the middle links to display. Current page will be in the middle
$batch_lr = 1;//number of pages in the left and right links
for($i = 1; $i <= $pages;$i++)
{
//display first links
if ($i <= $batch_lr)
{
echo $i . ' ';
continue;
}
//display last links
if ($i > $pages-$batch_lr)
{
echo $i . ' ';
continue;
}
//display middle links
if ($i>=($page-floor($batch_middle/2)) && $i<=($page+floor($batch_middle/2)))
{
echo $i . ' ';
continue;
}
//placeholder
echo ' ... ';
//move the pointer
$i = ($i < $page) ? ($page-floor($batch_middle/2)-1) : ($pages-$batch_lr) ;
}
//output example 1: 1 2 ... 14 15
//output example 2: 1 2 ... 7 8 9 ... 14 15
//output example 3: 1 2 3 4 5 ... 14 15
//output example 4: 1 ... 6 7 8 9 10 ... 15
Replace $page and $pages with your logic with getting a current page and count of total pages.
Replace the $batch_middle and $batch_lr to configure a number of links in the first, second and third batches of the links