Pagination help please

Discussion in 'PHP' started by crazyryan, Jul 31, 2007.

  1. #1
    Hey, having some problems with this, I followed the phpfreaks pagination tutorial.

    My result:
    http://www.phpmediascript.com/news/index.php

    As you can see it isn't really functioning right (the links).

    My code:
    
    <?php
    include("config.php");
    
       $limit      = 1;               
        // Sets how many results shown per page
        
        $query_count    = "SELECT count(*) FROM articles";    
        // Sets what we want to pull from the database
        // count(*) is better for large databases (thanks Greg!)
        
        $result_count   = mysql_query($query_count);    
        // Pulls what we want from the database
        
        $totalrows  = mysql_num_rows($result_count);    
        // This counts the number of users 
        
        if(empty($page)){    // Checks if the $page variable is empty (not set)
            $page = 1;      // If it is empty, we're on page 1
        }
    
        $limitvalue = $page * $limit - ($limit);  
    
    $article = mysql_query("SELECT *  FROM `articles` LIMIT $limitvalue, $limit");
    
    $check = mysql_num_rows($article); 
    
    if ($check < 1) {
    
        echo "no rows";
    
    }
    
        else {
    
    while($row = mysql_fetch_array($article)) {
    
        $title = $row['title'];
        $short = $row['short'];
        $full = $row['full'];
        $comments = $row['comments'];
    
        echo "\n\n<div class=\"container\">\n<div class=\"title\"><a href=\"full.php?id=".$row['id']."\">" . $title . "</a></div>\n";
        echo $short;
        if($short != $full) { echo "..<br /><a href=\"full.php?id=".$row['id']."\">Read More</a>";
        }
        if ($comments != 0) {
        echo "<br /><a href=\"full.php?id=".$row['id']."\">$comments comments</a></strong>";
        }
        echo "</div><br /><br />\n\n";
        
    
        if($page != 1){ 
            $pageprev = $page--;
            
            echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); 
        }else{
            echo("PREV".$limit." ");
        }
    
        $numofpages = $totalrows / $limit; 
        
        for($i = 1; $i <= $numofpages; $i++){
            if($i == $page){
                echo($i." ");
            }else{
                echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
            }
        }
    
    
        if(($totalrows % $limit) != 0){
            if($i == $page){
                echo($i." ");
            }else{
                echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
            }
        }
    
        if(($totalrows - ($limit * $page)) > 0){
            $pagenext = $page++;
             
            echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); 
        }else{
            echo("NEXT".$limit); 
        } 
        
        } 
    }
    ?>
    
    PHP:
     
    crazyryan, Jul 31, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think your problem lies here...

    $query_count = "SELECT count(*) FROM articles";
    $result_count = mysql_query($query_count);
    $totalrows = mysql_num_rows($result_count);

    $totalrows will ALWAYS contain 1, because the select count query will always return just 1 row, containing your count. Instead of counting the rows returned by that query, you need to get the value out of the result set.

    All of which is redundant anyway because you query it all again later! So you're doing 2 queries when you could be doing one.

    Try inserting...
    $totalrows = $check;

    immediately after
    $check = mysql_num_rows($article);

    which should get you the correct value in totalrows before it's needed. If that works, you can remove all the top part.
     
    ecentricNick, Jul 31, 2007 IP
  3. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #3
    Hey,
    Thanks, but unfortunately that didn't work.
    I didn't see any difference.
     
    crazyryan, Jul 31, 2007 IP
  4. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ecentricNick, Jul 31, 2007 IP
  5. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #5
    Fixed with a new pagination script.
     
    crazyryan, Jul 31, 2007 IP
  6. nagasharmi

    nagasharmi Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    are you using count(*)
    count(*) means we need some condition like where group by field name
    otherwise give
    select count(field name) from tablanme where condition
     
    nagasharmi, Aug 2, 2007 IP