I have this code below and i am using a Mysql loop to get some rows of my database. The $Pages == 1
is the pages we are on like /list.php?page=1 but the problem is the else section. If we go to page 2 both $pages2
& $pages3
echo out as 40 & 80 BUT the mysql loop still get's the results for 41-120 out of the database, why? i only want row 40 to 80 in the output, nothing more.
if($pages == 1)
{
$pages3 = 40;
$pages2 = 0;
echo $pages2;
echo $pages3;
}
else
{
$pages3 = 40 * $pages;
$pages2 = 40 * $pages - 40;
echo $pages2;
echo $pages3;
}
$currentpage = 0;
$sql = "SELECT * FROM cake";
$numRows = mysql_num_rows(mysql_query($sql));
$getquery = mysql_query("$sql ORDER by ID LIMIT $pages2, $pages3");
while($rows=mysql_fetch_assoc($getquery)){
on LIMIT the first parameter is first row to start from (starting from 0) and second is amount of rows to be fetched.
So it should be like:
$getquery = mysql_query("$sql ORDER by ID LIMIT $pages2, 40");
try this
else
{
$pages3 = 40;
instead of
else
{
$pages3 = 40 * $pages;
Look at the Documentation https://dev.mysql.com/doc/refman/5.0/en/select.html
LIMIT arg1, arg2
arg1 is offset arg2 is no. of rows you want after offset
so if want result from 40-80, you have to do
LIMIT 40,40
Use this
$pages="";
if($pages == 1)
{
$pages3 = 40;
$pages2 = 0;
echo $pages2;
echo $pages3;
}
else
{
$pages3 = 40 * $pages;
$pages2 = (40 * $pages) - 40;
echo $pages2;
echo $pages3;
}
The value of $pages should be giving