pagination incrementing problem
<p>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.</p>

<p>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.</p>

<pre><code>&lt;?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 &lt; 1) {
    $pg = 1;
} elseif ($pg &gt; $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 &lt; 1) {
    $pg = 1;
} elseif ($pg &gt; $last) {
    $pg = $last;
}
$self = htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'utf-8');
echo " &lt;a href='$self?pg=1'&gt; &lt;&lt;-First&lt;/a&gt; ";
$previous = $pg-1;
echo " &lt;a href='$self?pg=$previous'&gt; &lt;-Previous&lt;/a&gt; ";
echo "---------------------------";
$next = $pg+1;
echo " &lt;a href='$self?pg=$next'&gt;Next -&gt;&lt;/a&gt; ";
echo " &lt;a href='$self?pg=$last'&gt;Last -&gt;&gt;&lt;/a&gt; ";
</code></pre>
