Forum Moderators: coopster

Message Too Old, No Replies

Stop paging from entering negatives

         

dkin

3:55 am on Apr 25, 2005 (gmt 0)

10+ Year Member



This is my code

$query = "SELECT count(*) as count FROM user_char";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$numrows = $row['count'];
if($start > 0)
echo "<a href=\"" . $PHP_SELF . "?start=" . ($start - 50) ."\">Previous</a><BR>\n";
if($numrows > ($start + 50))
echo "<a href=\"" . $PHP_SELF . "?start=" . ($start + 50) ."\">Next</a><BR>\n";
echo '</table></center>';
/*End beginning table for inner cell*/
echo '</td></tr></table>';

but say it starts at 34, if the user presses back it will display nothing because $start = -16.

I would like it to stop at 0 but am not sure how to do this.

larryhatch

4:08 am on Apr 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know php scripting but ...

Somewhere in your code, you need the equivalent of:

if ($start < 0) start = 0; (or possibly)
if ($start < 1) start = 1;

This and similar limits should be imposed after incrementing or
decrementing, and BEFORE showing or echoing your buttons.

You may want to impose an upper limit as well so people don't
sail off to some non-existent page. - Larry

dkin

4:18 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



This is my code now,

$query = "SELECT count(*) as count FROM user_char";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$numrows = $row['count'];
if ($start < 0)
{
$start = 1;
echo "<a href=\"" . $PHP_SELF . "?start=" . ($start) ."\">Previous</a><BR>\n";
}

elseif($start > 0)
{
echo "<a href=\"" . $PHP_SELF . "?start=" . ($start - 50) ."\">Previous</a><BR>\n";
}
if($numrows > ($start + 50))
echo "<a href=\"" . $PHP_SELF . "?start=" . ($start + 50) ."\">Next</a><BR>\n";
echo '</table></center>';
/*End beginning table for inner cell*/
echo '</td></tr></table>';

But it still is not working I do not know why.

Anyone have any ideas?

mykel79

4:37 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



The link has:
$start-50
So if $start is 0 or more but anything less than 50, you'll get a negative number.

I suggest you set another variable, e.g.

$previous=$start-50;
Then check
if ($previous<=0) $previous=1;
In the links, instead of
$start-50
use
$previous