Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8
  1. #1
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    wrote a stats script recently and its got to 550ish hits and takin a while to load so trying to spread it across multiple pages got it to do it with this script fine but it puts the page numbers backwards.

    script here

    PHP Code:
            $page $_GET['page'];

            
    $limit"20";
            
            
    $action $_GET['action'];
            
            
    $select "SELECT * from visitors";
            
            
    $result mysql_query($select);
            
            
    $num mysql_num_rows($result);
            
            
    $num $num 30;
            
            
    $num ceil($num);
            
            
    $start = (($page 1) * ($limit) + 1);
            
                echo 
    "<b>Page:</b>";
            while ( 
    $num >= 1) {
        echo <<<END
        <a href="?action=$action&page=$num">$num</a>
    END;
        
    $num--;
        } 
    how would i get the numbers to order 1 2 3 4 rather than 4 3 2 1
    Regards Phil,


  2.  

  3. #2
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    First off, the script is pretty inefficient. You are selecting all columns and all rows, whereas you could use COUNT to simply return the number of rows.

    Second off, the way the script is coded, $num will always start above 1 (assuming you have > 1 rows). Add another variable to the mix, and change the logic to
    PHP Code:
    $i 0;
    while (
    $i $num)
      {
        echo 
    $i;
        
    $i++;
      } 

  4. #3
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Yes, you definitely want to take bfsog's advice and use COUNT. Something like this:

    PHP Code:
    $res mysql_query("SELECT COUNT(*) AS count FROM table"$cxn);
    $res mysql_fetch_assoc($res);
    $count $res['count']; 

  5. #4
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    Quote Originally Posted by bfsog
    First off, the script is pretty inefficient. You are selecting all columns and all rows, whereas you could use COUNT to simply return the number of rows.

    Second off, the way the script is coded, $num will always start above 1 (assuming you have > 1 rows). Add another variable to the mix, and change the logic to
    PHP Code:
    $i 0;
    while (
    $i $num)
      {
        echo 
    $i;
        
    $i++;
      } 
    Wouldn't it be more efficient to use a for loop rather than a while loop? ie.

    PHP Code:
    for ($i 0$i $num$i++) {
    echo 
    $i;

    Not sure if it's worth bothering with, mind; I might just be being pedantic

  6. #5
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Well, that's what for loops are designed for, so yes

  7. #6
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    so would this be more efficient then?
    PHP Code:
    <?php
    include 'config.php';

    $page $_GET['page'];

            
    $limit"20";
            
            
    $action $_GET['action'];
            
            
    $res mysql_query("SELECT COUNT(*) AS count FROM visitors");
            
            
    $res1 mysql_fetch_assoc($res);
            
            
    $count $res1['count'];  
            
            
    $num $count 30;
            
            
    $num ceil($num);
            
            
    $start = (($page 1) * ($limit) + 1);
            

            
    $i 1;
    while (
    $i <= $num)
      {
        echo 
    "<a href='?action=$action&page=$i'>$i</a> ";
        
    $i++;
      }
    Regards Phil,


  8. #7
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Yep, that looks about right.

  9. #8
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    ah cheers for the help all
    Regards Phil,



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 02:11 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com