...

View Full Version : PHP Loop Calculations



BabyPinkHeart
03-30-2004, 04:21 AM
Hi!

I don't really know PHP. I'm using a code that calls text file entries and prints it out to the screen.

I tried my hands on it without success. The first page prints out really good, exactly what I want - the latest entry at the top and the number starts at the total number of entries.

But when I go to the next page (shows the next 10 recent entries), it adds up the total number of entries then the calculation starts there.

For example, if there are a total number of 50 entries, the output will be for the first page is:
50 - Tyrese
49 - Beyonce
48 - Kelly
47 - Michelle
46 - Aaliyah
45 - Usher
44 - Tupac
43 - Brian McKnight
42 - Jessica Alba
41 - Johnny Depp

Then for the second page:
60 - Twista
59 - Tamia
58 - Fabolous
57 - Me
56 - Lee
55 - Josh
54 - Dat
53 - Lis
52 - Ann
51 - Fayzie

Instead of:
(second page)
40 - Twista
39 - Tamia
38 - Fabolous
37 - Me
36 - Lee
35 - Josh
34 - Dat
33 - Lis
32 - Ann
31 - Fayzie

This is the code that I have:


<?php
$fileName = file ("guestbook.txt");
$rows = count ($fileName);
$idtotal = count ($fileName);
$u_id = 1;

if ($rows > 10)
{
if (!isset ($row) )
{
$row = 0;
}
print ("<table class=\"guestbookLinks\" width=\"100%\"><tr><td>");
if ($row > 0)
{
echo "<p class=\"nextPage\"><< <a href=\"readbook.php?row=" . ($row - 10) . "\">Previous 10 </a></p>";
}
print ("</td><td>");
if ( ($rows - $row) > 10)
{
echo "<p class=\"previousPage\"><a href=\"readbook.php?row=" . ($row + 10) . "\">Next 10 </a> >></p>";
}
print ("</td></tr></table>");
for ($i = $row; $i < ($row + 10); $i++)
{
print ("<p>");
echo $row + $idtotal + $u_id . " " . $fileName [$i] . "<br\>";
$u_id--;
print ("<img src=\"../../images/divide.gif\" width=\"36\" height=\"28\" alt=\"New\"></p>");
}
print ("<table class=\"guestbookLinks\" width=\"100%\"><tr><td>");
if ($row > 0)
{
echo "<p class=\"nextPage\"><< <a href=\"readbook.php?row=" . ($row - 10) . "\">Previous 10 </a></p>";
}
print ("</td><td>");
if ( ($rows - $row) > 10)
{
echo "<p class=\"previousPage\"><a href=\"readbook.php?row=" . ($row + 10) . "\">Next 10 </a> >></p>";
}
print ("</td></tr></table>");
}
else
{
for ($i=0; $i < $rows; $i++)
{
echo $fileName [$i];
}
}
?>


Thank you, any help is appreciated. Thanx!

sidney
03-30-2004, 07:05 PM
try

echo ($idtotal-$row) + $u_id . " " . $fileName [$i] . "<br\>";

BabyPinkHeart
03-31-2004, 01:06 PM
THANK YOU!!!

This is a great help, and I really appreciate it. I'm really happy with the results.

I have one tiny problem though, the loop doesn't stop when there are no entries left (in the last page) so there's 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5. But still, I'm really happy with it. THANK YOU!

sidney
03-31-2004, 01:27 PM
print ("</td></tr></table>");
for ($i = $row; $i < ($row + 10); $i++)
{
if $row = say 50

and the are 55 results all together

then

echo $row + $idtotal + $u_id . " " . $fileName [$i] . "<br\>";

wil count in to minus figures

you will need to check to see if

somthing like this


$test =$idtotal - $row+10 ;
If ($test <10){
$num=$test;
}
else
{
$num=10;
for ($i = $row; $i < ($row + $num); $i++)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum