...

View Full Version : Php Pagination



bucket
11-02-2009, 06:08 PM
I am getting this error with a script:


Parse error: syntax error, unexpected T_IF in /home/brian/public_html/pages.php on line 5


<?php
// Connects to your Database
include("inc/config.php");(
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM accounts") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM accounts $max") or die(mysql_error());

//This is where you display your query results
while($info = mysql_fetch_array( $data_p ))
{
Print $info['Name'];
echo "<br>";
}
echo "<p>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>

Anyone know why?

This is the structure of my table:


+---------+------------+-----------+
| Table Name: accounts |
+---------+------------+-----------+
| id | username | password |
+---------+------------+-----------+
| 1 | john12 | thepass |
| 2 | catman17 | cat |
+---------+------------+-----------+

bucket
11-02-2009, 06:27 PM
I have fixed it:


<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include("inc/config.php");
if (!(isset($pagenum)));
{
$pagenum = 1;
}
$data = mysql_query("SELECT * FROM accounts") or die(mysql_error());
$rows = mysql_num_rows($data);
$page_rows = 4;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$data_p = mysql_query("SELECT * FROM accounts $max") or die(mysql_error());
while($info = mysql_fetch_array( $data_p ))
{
Print $info['username'];
Print $info['password'];
echo "<br>";
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
echo " ---- ";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>

Now for some reason it is not going to the next page.

bucket
11-02-2009, 06:30 PM
It is only showing the first 4 rows and it wont show the rest when I click the next or last button.

Phil Jackson
11-02-2009, 07:35 PM
If you can wait, I think ill create a plug in Pagination script tomoz mornin that anyone can use.

bucket
11-02-2009, 07:48 PM
Well I have fixed it, but I was wondering if you can just help me.

I really need this today and if its something simple to fix.

Another person said do this:


do a requery... add "LIMIT" on your sql statement.

I have no idea what that means.

Phil Jackson
11-02-2009, 08:17 PM
You have the limit set. from first glance it seems fishy as i cant quite understand the crack



$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;


so if pagenum = 3
and page rows = 10

result would be
LIMIT 20, 10



$fst = $pagenum * $page_rows;
$max = 'LIMIT ' .$fst .',' .$fst+$page_rows;


i think...

So then be LIMIT 30, 40

bucket
11-02-2009, 08:28 PM
So the updated code is:


<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include("inc/config.php");
if (!(isset($pagenum)));
{
$pagenum = 1;
}
$data = mysql_query("SELECT * FROM accounts") or die(mysql_error());
$rows = mysql_num_rows($data);
$page_rows = 4;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

$fst = $pagenum * $page_rows;
$max = 'LIMIT ' .$fst .',' .$fst+$page_rows;


$data_p = mysql_query("SELECT * FROM accounts $max") or die(mysql_error());
while($info = mysql_fetch_array( $data_p ))
{
Print $info['username'];
Print $info['password'];
echo "<br>";
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
echo " ---- ";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>

I changed

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

to


$fst = $pagenum * $page_rows;
$max = 'LIMIT ' .$fst .',' .$fst+$page_rows;

Is that right?

I now have this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '4' at line 1

bucket
11-02-2009, 08:58 PM
I have fixed it:


<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include("inc/config.php");

$pagenum = (isset($_GET['pagenum'])) ? (int) $_GET['pagenum'] : 1;

$data = mysql_query("SELECT * FROM accounts") or die(mysql_error());
$rows = mysql_num_rows($data);
$page_rows = 4;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$data_p = mysql_query("SELECT * FROM accounts $max") or die(mysql_error());
while($info = mysql_fetch_array( $data_p ))
{
echo "Username: ";
Print $info['username'];
echo "<br>";
echo "Password: ";
Print $info['password'];
echo "<br>----";
echo "<br>";
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
echo " ---- ";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>



----

Now I need help with another thing, How would I do it so instead of it posting it like this when im at the first page:

---- Next -> Last ->>

To like this:

| 1 | 2 | 3| Last ->>

Like it adds a number based on how many pages, its like instead of using the Next -> thing clicking over and over again to get to like page 5.

Same thing with the rest of the pages:

<<-First | 1 | 2 | 3| Last ->>

And the last page:

<<-First | 1 | 2 | 3|

Does anyone know how to do this? I want number pages instead of clicking to get to the next page or the page before.

met
11-02-2009, 09:25 PM
consider using LIMIT and OFFSET

LIMIT=5, OFFSET=10 (show 5 records, starting from the 10th)

increment offset by LIMIT each time.

As for the URLs, divide the total number of rows by the amount you want to show on each page (100records/20 per page==5 pages)

then do a loop



$offset=0;
for($i=1;$i<=5;$i++) { echo '<a href="?offset='.$offset . '">'.$i.</a>;
$offset=$offset+20; //20=data per page
}
// do query

bucket
11-02-2009, 09:33 PM
Where would I add that code into my script?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum