Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Loop Calculations

    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 Code:
    <?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!

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try

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

  • #3
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •