tags:

views:

67

answers:

3

I have the following code, which is the basis for pagination in a larger application. However, it does not work, as although I should be obtaining the value of pg from the url, pg never goes higher than 2. For some reason, next = pg+1; seems to always see pg as 1, regardless of what is passed on the url. It is a similar problem with last. I assume I am overriding the value obtained from GET, but I am unsure where.

The problem seems to be in how I am working out $max and the limit, as instead of 0, 10, -10, 10 gets passed. Also the ifcode before $max does not seem to succeed in stopping pg from being 0.

<?php
if (isset($_GET["pg"])) {
    $pg = $_GET["pg"];
} else $pg = 1;
$con = mysql_connect("localhost","","");
if(!$con) {
    die('Connection failed because of' .mysql_error());
}
mysql_select_db("ebay",$con);

if ($pg < 1) {
    $pg = 1;
} elseif ($pg > $last) {
    $pg = $last;
}
$table = 'AUCTIONS';
$page_rows = 10;
$max = ' limit ' .($pg - 1) * $page_rows .', ' .$page_rows;
$rows = getRowsByArticleSearch($query, $table, $max);
$rowcount = count($rows);
echo $rowcount;
$last = ceil($rowcount/$page_rows);
$page_rows = 10;
$rowcount = 2;
// Would normally obtain the number of rows returned, but database stuff is snipped for brevity
$last = ceil($rowcount/$page_rows);
if ($pg < 1) {
    $pg = 1;
} elseif ($pg > $last) {
    $pg = $last;
}
$self = htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'utf-8');
echo " <a href='$self?pg=1'> <<-First</a> ";
$previous = $pg-1;
echo " <a href='$self?pg=$previous'> <-Previous</a> ";
echo "---------------------------";
$next = $pg+1;
echo " <a href='$self?pg=$next'>Next -></a> ";
echo " <a href='$self?pg=$last'>Last ->></a> ";
+1  A: 

Here's your problem:

$last = ceil($rowcount/$page_rows);

That's setting $last to be 1, and then you have:

if ($pg > $last) {
  $pg = $last;
}

Edit: I wonder if you meant this, instead:

$last = ceil($page_rows/$rowcount);

Edit again: as per Cassy's answer, you probably really just need to set the right values for these variables:

$page_rows = 10;
$rowcount = 2;
Ben
Hi Ben, Thanks for your reply. The problem seems to be that I am getting a rowcount, but because I am limiting the results it is always 10.
Joshxtothe4
+1  A: 

The code is correct, however, the data is wrong.

When you have

$page_rows = 10;
$rowcount = 2;

it actually means, that you have 2 rows displayed on pages which will display 10 rows each. This makes it 1 page in total.

Change it to

$page_rows = 10;
$rowcount = 200;

and voila, you will get your pagination.

Cassy
A: 

based on the now revised question:

$rowcount = count($rows);

will naturally always return 10, as you're limiting the result

... LIMIT 10;

But this might help:

SQL CALC FOUND ROWS Example:

mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
    -> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();

The first query will still return your articles, while the second will return the number of rows the previous select would have had if not using the LIMIT clause.

Hope that helps.

Cassy
Thankyou for your answer, I will look into it. I based my code on the tutorial here, do you have any idea why theres works and mine would not? http://php.about.com/od/phpwithmysql/ss/php_pagination_3.htm
Joshxtothe4
Yes. //Here we count the number of results$data = mysql_query("SELECT * FROM topsites");$rows = mysql_num_rows($data); see http://php.about.com/od/phpwithmysql/ss/php_pagination.htmThey're doing what I suggested: counting the number of total rows before calculating the number of pages.
Cassy
ok, I tried that, hwoevernow nothing gets put out. I wonder if you could check my entire code(still short) as now nothing is output, and I do not want to make a new question just for this. http://pastebin.com/d65cc6b30
Joshxtothe4
See the changes at http://pastebin.com/pastebin.php?diff=m65a6d0d7
Cassy
Hi Casey: Check here: http://pastebin.com/d3e79061e I made those changes, but something is still not right. If I move the ifblock to make sure pg is not less than 0 before $max, the sql completely breaks with it becomming limit -10, 10, any idea why?
Joshxtothe4
huh...nevermind. Thanks for your help.
Joshxtothe4