Forum Moderators: coopster

Message Too Old, No Replies

how to produce links depending on value of sizeof($array_source)

         

laura2k

1:08 pm on Jun 2, 2004 (gmt 0)



Hi there,

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

Timotheos

3:23 pm on Jun 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Laura,

We're missing a piece of the puzzle. How is the $sotrang variable set?

Tim

laura2k

3:25 pm on Jun 2, 2004 (gmt 0)



Hi Tim,

It is:

$maxid="0";
$maxinpage="15";
$sotrang=laynguyen($maxid,$maxinpage)+1;

Thanx

[edited by: laura2k at 3:33 pm (utc) on June 2, 2004]

httpwebwitch

3:33 pm on Jun 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your array has 30 items, you need 2 pages. But if your array has 31 items, you need 3 pages. Use ceil() to round the quotient up.

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]

Timotheos

3:35 pm on Jun 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So there's a function somewhere named 'laynguyen' which is supposed to return the number of page links to display. Can you find that function and post it?

laura2k

3:41 pm on Jun 2, 2004 (gmt 0)



Hi Tim,

function laynguyen($num1,$num2){
$res=ceil($num1/$num2)-1;
return $res;
}

Timotheos

4:00 pm on Jun 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Laura,

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.