Forum Moderators: coopster
I have the following code at the bottom of my cart script, but there seems to be a bug in it. The purpose of the code below is to display 15 items per page and then create extra pages depending on how many pages are required to display the products. So for example, if i had 30 items, it should display 2 pages,(1 and 2) and link each page number to index.php?page=1 or page=2
The problem with the code below is that it doesnt do this and it just seems to produce a list of soooo many pages although i only have 5 items which are displayed.
print("Total <b>".sizeof($array_source)."</b> Items.");
for($i=0;$i<$sotrang;$i++) {
if($page!=$i+1) print(" <a href=\"index.php?page=".($i+1)."&$special_query\">[".($i+1)."]</a> ");
else print("<font color=#DFDFDF> [".($i+1)."] </font>");
}
I want the code to check the value of ".sizeof($array_source)." and then maybe devide it by 15 to produce the number of pages required and then produce the links linking to index.php?page=1
if anyone can please help me do this i would be really grateful
many thanx in advanced.
Laura
It is:
$maxid="0";
$maxinpage="15";
$sotrang=laynguyen($maxid,$maxinpage)+1;
Thanx
[edited by: laura2k at 3:33 pm (utc) on June 2, 2004]
This code can get you started. Note that I use a page=1 as the first page; I don't start counting from 0.
$itemsperpage = 15;
$num_items = count($array);
$numpages = ceil($num_items/$itemsperpage);
$pagenum=$HTTP_GET_VARS['page'];
$startat = ($pagenum-1)*$itemsperpage;
// starting at record $startat,
// display $itemsperpage items. This can be
// done with a SQL grab using the LIMIT clause
// rather than getting the whole set and only
// using 15 of them
if ($pagenum>1){print("<a href='index.php?page="--$pagenum"'>prev</a> ")}
for ($i=1;$i<=$numpages;$i++){
if ($i==$pagenum){
print("<b>".$i."</b> ");
}else{
print("<a href='index.php?page='".$i.">".$i."</a> ");
}
if ($pagenum<$numpages){print(" <a href='index.php?page="++$pagenum"'>next</a>")}
[edited by: httpwebwitch at 3:38 pm (utc) on June 2, 2004]
function laynguyen($num1,$num2){
$res=ceil($num1/$num2)-1;
return $res;
}
Putting the $maxid = 0 doesn't seem right. Look at the function and your dividing (0/15)-1 = -1 and with ceil it equals zero.
If you look at httpwebwitche's code he has $num_items = count($array) which is what $maxid should be like. So in your code $maxid = count($array_source); Give that a try and study httpwebwitche's code to fine tune it.