How set paging in PHP? - php

I have an 300 rows(data) each page contain 10 records I need first 10 pages then click next then display next 10 page here is my code:
<?php
$eu = ($start - 0);
$limit = 10;
$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;
if ($nume > $limit)
{
echo'<div class="pagination right">';
if ($back >= 0)
{
echo"<a href='$page_name?start=$back&o" . $_SESSION['clicked'] . "&p=$desc'>«</a>";
}
$i = 0;
$l = 1;
for ($i = 0; $i < $nume; $i = $i + $limit)
{
if ($i <> $eu)
{
echo"<a href='$page_name?start=$i&o=" . $_SESSION['clicked'] . "&p=$desc'>$l</a>";
}
else
{
echo "<a class='active'>$l</a>";
}
$l = $l + 1;
}
if ($this1 < $nume)
{
echo "<a href='$page_name?start=$next&o=" . $_SESSION['clicked'] . "&p=$desc'>»</a>";
}
echo '</div>';
}
echo '</div></div>';
?>
Then I will get response like this http://app.ologie.us/app/admin/Screen.png
Can any one please guide to effective paging in PHP.

Here's a tutorial on basic pagination.
http://www.phpfreaks.com/tutorial/basic-pagination

Doing it like Tim said is, according to me, mainly a bad way to do this. Read in the data needed WHEN it's needed.
You could do it with a min - max index to your array.
Consider you want to display 10 records per page:
$min = $page * 10; //Page is the current page
$max = $min + 10;
Then you just loop through the data between the min - max indexes.
for($i = $min; $i < $max; $i++) {
//Echo your data like normal
}
If you're gathering your data from MySQL, you might want to look up LIMIT.

If $limit is your page size and you add another request variable $page - representing the search result page requested by the user - you can use
$start = ($page-1)*$limit;
for($i=$start;$i < $start + $limit;$i=$i+1) {
...
}
to iterate over a "page" of the results.
But of course it does not make sense to handle paging on the level of the result view. You want to limit the number of results you get from the query.
In addition I would really consider using a PHP framework rather than coding it in the way you present in your request. Chances are your application will end up to be difficult to maintain.

You do it all client side with the DataTables plugin for jQuery. This plugin also gives you a LOT more options like sorting and searching.
http://datatables.net/
If you have a VERY large table, this probably isn't a good idea, though.

Related

Create a custom array in PHP

I've played around with a personal project regarding a website listing PC games by year of releasing etc. (the topic it is not so important i guess) using the following structure for the links:
http://localhost/
http://localhost/?g=15
http://localhost/?g=30
http://localhost/?g=45
Like it is seen, I've display 15 games per page. What am I working right now is displaying the above links using php in a specific manner for each page (thumbnails, links, etc.):
$arr = array("","?g=15","?g=30","?g=45","?g=60","?g=75","?g=90");
foreach ($arr as $page) {
$link = 'http://localhost/' . (string)$page;
// do stuff to each page link
}
I am very satisfied with how it goes so far but I am wondering if there is a way to create the array automatically not requiring me to manually write the string, just specify the last multiple of 15 for example. I searched the web but I haven't find something concludent or maybe I don't express myself clear enough that's why any help is more than welcomed.
echo 'http://localhost';
for ($i = 15; $i < $max; $i += 15) {
echo "http://localhost/?g=$i";
}
In practice $max is calculated by how many entries there are, which is something you usually figure out from querying a database. Hope this points you in the right direction though.
You can easily generate your array:
<?php
$arr = array('');
$max = 5;
for($i = 0; $i < $max; ++$i) {
$arr[] = '?g='.($i*15);
}
?>
Thry this and tell me if it helps
$links = array();
$links[] = 'http://localhost/';
$multiplier = 5; //the mutiplier, number of links to provide
for ($i = 1; $i < $multiplier; $i++)
{
$links[$i] = 'http://localhost/?g='.(15*$i);
// do here what you want with $links[$i]
}
print_r($links);

PHP Pagination - detect current page

What is the best way to detect/print the current page number (pagination)?
I tried two different ways, and now i want to know if there exists a better way to do it.
I:
$number = 12; //current page number
$all = 40; // all pages
$range = 4; // number of pages shown (up and down)
$min = $number-$range;
$max= $number+$range+1;
for($i=$min; $i<$number;$i++) {
echo "<a href='#'>$i</a><br/>";
}
echo "$number<br/>";
for($i=($number+1); $i<$max;$i++) {
echo "<a href='#'>$i</a></br>";
}
II:
$number = 12; //current page number
$all = 40; // all pages
$range = 4; // number of pages shown (up and down)
$min = $number-$range;
$max= $number+$range+1;
for($i=$min; $i<$max;$i++) {
if($i!=$number) {
echo "<a href='#'>$i</a><br/>";
} else {
echo "$i<br/>";
}
}
I also checked the speed of both solutions (using different high-number $all values) and first one is faster in most cases.
Thanks for suggestions :)
Here's a functional alternative (for 0 to 10 pages; replace it with $min $number-1 .. )
echo implode("<br/>",
array_map(function($x){
return "<a href='..'>$x</a>";
}, range(0,10))
);
Please time it, and let me know. :)

How can I limit the page numbers shown in the pagination?

I am trying to limit the shown pagination. My site has 500+ pages, and all 500+ numbers are shown in the pagination.
I am trying to limit it like this:
Prev 1 2 3 4 5 6 Next
My code:
$skin = new skin('site/pagination'); $pagination = '';
if ($pages >= 1 && $page <= $pages) {
for ($x=1; $x<=$pages; $x++) {
$TMPL['pagination'] = ($x == $page) ? '<strong>'.$x.'</strong> ' : ''.$x.' ';
$pagination .= $skin->make();
}
}
pagination page number limit problem solve by chnage
for ($x=1; $x<=$pages; $x++)
to
for($x = max(1, $page - 5); $x <= min($page + 5, $pages); $x++)
What do you expect this to do?:
for ($x=1; $x<=$pages; $x++)
It is going to create an entry for every page. If you don't want that, limit it how it makes sense:
for ($x=1; $x <= min(5, $pages); $x++)
Even better would be to consider the current page:
for ($x=max($curpage-5, 1); $x<=max(1, min($pages,$curpage+5)); $x++)
//Let's say you want 3 pages on either side of your current page:
$skin = new skin('site/pagination'); $pagination = '';
$currentPage = get the current page number however you have it stored;
// set the lower bound as 3 from the current page
$fromPage = $currentPage - 3;
// bounds check that you're not calling for 0 or negative number pages
if($fromPage < 1) {
$fromPage = 1;
}
// set the upper bound for what you want
$toPage = $fromPage + 7; // 7 is how many pages you'd like shown
// check that it doesn't exceed the maximum number of pages you have
if($toPage > $maxPages) {
$toPage = $maxPages;
}
// iterate over your range
for ($x=$fromPage; $x<=$toPage; $x++) {
$TMPL['pagination'] = ($x == $page) ? '<strong>'.$x.'</strong> ' : ''.$x.' ';
$pagination .= $skin->make();
}
For large numbers of pages, consider displaying links using "logarithmic" pagination. See my answer here (PHP code included):
How to do page navigation for many, many pages? Logarithmic page navigation
I tried the answers provided by wallyk and Hemang but they fell short for my pagination case. Their answers would sometimes display less links than the range. I had to add some max and min statements.
Here is my take in Javascript code:
var minPage = Math.max(Math.min(currentPage - (range / 2), totalPages - range), 0);
var maxPage = Math.min(Math.max(currentPage + (range / 2), range), totalPages);
The range is the number of links always displayed.
The totalPages is the total number of pages to iterate over.
The currentPage is the currently displayed page.
Note that my pagination index is 0 based.

Not sure the loop I need to use every two variables

I have a problem where I have an array $user.
I have $_SESSION['players'] which has the total amount of $user.
I need a function where I can take the user1 and 2 and use them. Then move on to user3 and 4 and use them, and so on.. until I have used all the players. Obviously the total $user[$i] would be players-1.
Anyone have a solution for this?
Thanks
Would this suit your needs? This requires that there be an even number of players to work properly though, unless you stick in a check for odd numbers:
for ($i = 0; $i < $_SESSION['players']; $i += 2) {
$userA = $user[$i];
$userB = $user[$i + 1];
// Do things with $userA and $userB variables...
}
just because you're taught how to use for loops in one way does not mean that you're stuck continuing to use them the way you were taught:
$length = count($users);
$length = $_SESSION['players'];
for ($i = 0; $i < $length; $i += 2)
{
if (!isset($user[$i], $user[$i + 1])) break;
$userOne = $user[$i];
$userTwo = $user[$i+1];
//do stuff
}
I realized that isset wasn't necessary, the for call could be modified more:
$length = $_SESSION['players'];
for ($i = 0; ($i + 1) < $length; $i += 2)
{
$userOne = $user[$i];
$userTwo = $user[$i+1];
//do stuff
}
EDIT to change how the length was calculated:
EDIT to validate that user exists
EDIT to add consolidated version

how to show hit counter then make it back to zero?

i want after i've been submit form it can show hit counter..
but i want after it reach "20" it can back to zero..bcoz the limit of submit is 20 times so it can't over the limit.
how do i make it works?I've been try to this code...
<?
$limit="20";
$Query_counter=mysql_query("SELECT model FROM inspec");
$Show_counter=mysql_fetch_array($Query_counter);
$show_counter = $show_counter["model"]+1;
if($show_counter > $limit[0]) {
$show_counter = 0;
}elseif ($show_counter > $limit[1]) {
$show_counter = 0;
}
$Query_update=mysql_query("UPDATE inspec SET model=$Show_counter");
$Show_counter=number_format($Show_counter);
$Show_counter=str_replace(",",".",$Show_counter);
echo "Hit:</br><strong>$show_counter</strong>";
?>
To make a counter go up to a certain value then loop back to zero, you can use the modulus operator, which in a lot of languages (including PHP and MySQL) is %
$x = 0;
$limit = 4;
for ($i = 0; $i < 10; ++$i) {
$x = ++$x % $limit;
echo $x;
}
// 1, 2, 3, 0, 1, 2, 3, 0, 1, 2
I hope that makes enough sense. I can't really figure out from the question what exactly you want... Perhaps something like this?
UPDATE `mytable` SET `mycounter` = (`mycounter` + 1) % {{the limit}}
The concept here is to test to see if the variable you're incrementing exceeds some acceptable range as a result of the next increment. Simple increment the variable, then test its value.
In your case, just add a test after you increment the counter:
$show_counter = $show_counter["model"]+1;
if($show_counter > $limit){
$show_counter = 0;
}
Be sure to define $limit to whatever number you want to cycle on.
If you want to do this for multiple thresholds you can add additional tests. Note that you could hard-code $limit to any number or any variable you want, it's just the thing you're testing against.
<?
$Query_counter=mysql_query("SELECT model FROM inspec");
$Show_counter=mysql_fetch_array($Query_counter);
$show_counter = $show_counter["model"]+1;
$x = 0;
$limit = 20;
for ($i = 0; $i < 30; ++$i) {
$x = ++$x % $limit;
echo $x;
}
$Query_update=mysql_query("UPDATE inspec SET model= (model + 1) % 20");
$Show_counter=number_format($Show_counter);
$Show_counter=str_replace(",",".",$Show_counter);
echo "Hit:</br><strong>$show_counter</strong>";
?>

Categories