Php pagination

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

PoorDoggie

Soon to be RICHdoggie!VIP Member
Impact
18
I am looking for some help to write a php pagination script.

Here is what I have at the moment. A mysql query that gets a list of results (dynamic, I can't tell how many results will be displayed). The script gets the "$p" variable from the query string, multiplies it by "10" and then that is used for the limit.

So, for instance:

Code:
SELECT * FROM pd_shops WHERE MATCH (name, description, keywords) AGAINS ('$q'  IN BOOLEAN MODE) LIMIT $a, 10
($a is $p*10)

So, I know that every page will display 10 results, until, obviouslly, there are no results left.

$i is the number of rows on a the same query, but without the "LIMIT" clause.

I want to transfer this to a pagination thing, but only want a maximum of 10 links displayed (and next/previous).

(IE: <-- PREV - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - NEXT -->)

When you click next, if there are more than 10 pages, the 1 will disappear, and 11 will be displayed, and on and on, so no more than 10 number hyperlinks are displayed.

If anyone can help, I will be very grateful.

Thanks a lot! :)

Tom
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
i can give u a fully working script for 10NP$
 
0
Close
LOL, thought that said "PHP Paignton", the town which I live in.........
 
0
Close
I've come up with something, but please note, that it is not tested. I am so tired, there are prolly errors, lol. >.<

PHP:
<?php

//Database Details
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "password";
$dbname = "database";

//Connect
$db = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
//Select Db
mysql_select_db("$dbname", $db) or die(mysql_error());

$query = mysql_query("SELECT * FROM table_name");
$total_rows = mysql_num_rows($query);
if(empty($page))
{
$page = 1;
}

//Maximum number of results
$max_results = 10;

$limit = $page * $max_results - ($max_results);

$query  = mysql_query("SELECT * FROM table_name LIMIT $limit, $max_results");
$result = mysql_query($query) or die("Error: " . mysql_error());

if($total_rows == 0)
{
echo "No data to display";
}

while($row = mysql_fetch_array($result))
{
$var = $row["filed"]);
//Etc... :)
echo $var;
//Etc... :)
}
if($page != 1)
{
$prev = $page--;
echo "<a href='yourpage.php?page=$prev'>Previous '.$limit.'</a>";
}
else
{
echo "Previous ".$limit." ");
}

$pages = $total_rows / $limit;

for($i = 1; $i <= $pages; $i++)
{
if($i == $page)
{
echo $i." ";
}
else
{
echo "<a href='yourpage.php?page=$i'>$i</a>";
}
}
if(($total_rows % $limit) != 0)
{
if($i == $page)
{
echo $i." ";
}
else
{
echo "<a href='yourpage.php?page=$i'>$i</a>";
}
}
if(($total_rows - ($limit * $page)) > 0)
{
$next = $page++;
echo "<a href='yourpage.php?page=$next'>Next '.$limit.'</a>";
}
else
{
echo "Next ".$limit;
}

?>
 
0
Close
SecondVersion said:
I've come up with something, but please note, that it is not tested. I am so tired, there are prolly errors, lol. >.<

PHP:
<?php

//Database Details
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "password";
$dbname = "database";

//Connect
$db = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
//Select Db
mysql_select_db("$dbname", $db) or die(mysql_error());

$query = mysql_query("SELECT * FROM table_name");
$total_rows = mysql_num_rows($query);
if(empty($page))
{
$page = 1;
}

//Maximum number of results
$max_results = 10;

$limit = $page * $max_results - ($max_results);

$query  = mysql_query("SELECT * FROM table_name LIMIT $limit, $max_results");
$result = mysql_query($query) or die("Error: " . mysql_error());

if($total_rows == 0)
{
echo "No data to display";
}

while($row = mysql_fetch_array($result))
{
$var = $row["filed"]);
//Etc... :)
echo $var;
//Etc... :)
}
if($page != 1)
{
$prev = $page--;
echo "<a href='yourpage.php?page=$prev'>Previous '.$limit.'</a>";
}
else
{
echo "Previous ".$limit." ");
}

$pages = $total_rows / $limit;

for($i = 1; $i <= $pages; $i++)
{
if($i == $page)
{
echo $i." ";
}
else
{
echo "<a href='yourpage.php?page=$i'>$i</a>";
}
}
if(($total_rows % $limit) != 0)
{
if($i == $page)
{
echo $i." ";
}
else
{
echo "<a href='yourpage.php?page=$i'>$i</a>";
}
}
if(($total_rows - ($limit * $page)) > 0)
{
$next = $page++;
echo "<a href='yourpage.php?page=$next'>Next '.$limit.'</a>";
}
else
{
echo "Next ".$limit;
}

?>
lol - nice one! :) Thanks... great help. I used your script as a basis for my paignation thing. Seeing someone else's script kinda helps! :) Thanks a lot.
 
0
Close
PoorDoggie said:
lol - nice one! :) Thanks... great help. I used your script as a basis for my paignation thing. Seeing someone else's script kinda helps! :) Thanks a lot.
:) No problem.


-Eric
 
0
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back