Asking for help - Pagination. Any help greatly appreciated

Discussion in 'PHP' started by Nocturne, Feb 4, 2007.

  1. #1
    Hi all,

    I have been trying to add pagination to a function I have in my script, but I just can't seem to get it right. I have tried to read some tutorials about how to do this but it always gets messed up.

    If anyone has a clue about how to do this I would greatly appreciate your reply/help. Thank you very much!

    Here's the code I'm trying to work with, which I would like to have pagination.

    {
    
          $game_category_query ="SELECT * FROM games where gameid = $gameid LIMIT 1";
          $result = mysql_query($game_category_query);
          while($row = mysql_fetch_array($result)){
          $game_category = $row['category'];
          $gameid = $row['gameid'];
          $gametitle = stripslashes($row['gametitle']);
          }
    
          $category_query ="SELECT * FROM categories where catid = $game_category && status = 1 LIMIT 1";
          $result2 = mysql_query($category_query);
          while($row = mysql_fetch_array($result2)){
          $catname = stripslashes($row['catname']);
          $catid = $row['catid'];
          $parentcatid = $row['parentcatid'];
          }
          
          $parentcategory_query ="SELECT * FROM categories where catid = $parentcatid && status = 1  LIMIT 1";
          $result3 = mysql_query($parentcategory_query);
          while($row = mysql_fetch_array($result3)){
          $parentcatname = stripslashes($row['catname']);
          $parentcatid = $row['catid'];
          
          $parenttitle = make_friendly($parentcatname);
    			  if ($rewrite ==0)
            $parentcatlink = $base_url."index.php?action=browse&cat=".$parentcatid;
    				else
    				$parentcatlink = $base_url."categories/".$parentcatid."/".$parenttitle;
          
          
          
          }
          
          $maintitle = make_friendly($catname);
    
    			  
    			  
    			  
    			  // Determines if the link to the category page is SE Friendly or NOT
    			  if ($rewrite ==0)
            $catlink = $base_url."index.php?action=browse&cat=".$catid;
    				else
    				$catlink = $base_url."categories/".$catid."/".$maintitle;
    			  // End
    
            if ($parentcatid > 0)
            $content = "<a class=\"gameBreadcrumb\" href=\"$base_url\">Home</a> >> <a class=\"gameBreadcrumb\" href=\"$parentcatlink\">$parentcatname</a> >> <a class=\"gameBreadcrumb\" href=\"$catlink\">$catname</a> >> $gametitle";        
            else
            $content = "<a class=\"gameBreadcrumb\" href=\"$base_url\">Home</a> >> <a class=\"gameBreadcrumb\" href=\"$catlink\">$catname</a> >> $gametitle";
    
    return $content;
    
    }
    
    function featured_content($base_url,$rewrite,$sedir){
    
        $sql_query_games = "SELECT * FROM games WHERE gamestatus = '1' AND gamelocation = '0' AND iconlocation = '0' ORDER BY gameid DESC LIMIT 15";
        //store the SQL query in the result variable
        $result_games = mysql_query($sql_query_games);
        if(mysql_num_rows($result_games))
        {
        //output as long as there are still available fields
        while($row = mysql_fetch_array($result_games))
        { // Get game ID and Name
               
                    $gamedesc = stripslashes($row['gamedesc']);
                    $gametitle = $row['gametitle'];
                    $gametitle = stripslashes($gametitle);
                    $rating = $data['rating'];
                    $number_of_votes = $data['totalvotes'];
                    $gameid = $row['gameid'];
                    $gamelocation = $row['gamelocation'];
                    $iconlocation = $row['iconlocation'];
                    $gameicon = $row['gameicon'];
                    $featuredicon = $row['gameicon'];
                    $timesplayed = $data['timesplayed'];
                    
                    
    $gamedate = $row['dateadded'];
    if (!empty($gamedate))
    $gamedate = date("F j, Y", $gamedate); 
    else
    $gamedate = "";
          
                    
                    if (!empty($featuredicon)){
                          
                          if (strlen($gamedesc)>65)
                          $gamedesc = substr($gamedesc,0,200)." ";
                          
                          $featuredicon = $base_url."media/images/".$featuredicon;
                              
                          $title = make_friendly($gametitle);
                          
                          if ($iconlocation == 1)
                          $gameicon = $gameicon;
                          else
                          $gameicon = $base_url."media/images/".$gameicon;
                          
                          if ($rewrite ==0)
                          $gamelink = $base_url."index.php?action=playgame&gameid=".$gameid;
                          else
                          $gamelink = $base_url.$sedir."/".$gameid."/".$title;
                          
                          $content = $content."<tr>
    <td>
    <table width=\"100%\" class=\"gamebox\">
    <tr>             
    <td rowspan=\"2\" width=\"200\" class=\"gametitle\" align=\"center\" valign=\"top\">
    <a href=\"$gamelink\"><img src=\"$featuredicon\" class=\"0\" width=\"120\" height=\"90\" alt=\"$gametitle\"></a>
    </td>
    <td width=\"100%\" align=\"left\" class=\"listSmallText\" valign=\"top\">
    <a href=\"$gamelink\" class=\"gametitle\"><B>$gametitle</B></a><br><br>
    $gamedesc
    </td>
    <tr>
    <td class=\"gameinfo\"valign=\"bottom\">
    Added: $gamedate | Views: $gameplayed
    </td>
    </tr>
    </tr>
    </table>
    </td>
                 </tr>
    
    ";
    
                    }
    
    
                    
        }
        }
    
            
    
    return $content;
    
    
    }
    
    Code (markup):
    Best Regards
     
    Nocturne, Feb 4, 2007 IP
  2. Seph

    Seph Peon

    Messages:
    84
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't have much time so I'm sorry I can't write this based on your case, but I'll show you how I generate my pagination array. Hope you can make sense of it, if not I'll make sense of it tomorrow. (1 AM here)

    <?php
    	// Generates pagination.
    	// Variables:
    	//  $system: System to get amount of pages for. ( String )
    	//  $offset: Current page offset. ( Int )
    	// Uses:
    	//  $this->files_to_fetch. ( Int )
    	// Returns:
    	//  An array in the form of $array[DISPLAY PAGE] = ACTUAL PAGE. ( Array )
    	public function generatePagination($system,  $offset = 0, $subdir = false)
    	{
    		list($system, $offset, $subdir) = $this->Clean( Array($system, $offset, $subdir) );
    
    		// Why would $offset be less than zero?
    		if ($offset < 0)
    		{
    			$offset = 0;
    		}
    		
    		// Offset starts at 0, for presentation we want it to start at 1.
    		$offset_display = $offset + 1;
    		
    		// Getting total amount of pages, total files / files per page.
    		$max_pages = $this->getEntries($system, $subdir);
    		$max_pages = ceil($max_pages / $this->files_to_fetch);
    
    		// Why would $offset be more than $max_pages?
    		// >= since max_pages starts at 1, so the actual max_page is $max_pages - 1
    		if ($offset >= $max_pages)
    		{
    			$offset = $max_pages - 1;
    		}
    		
    		// Time to build an array that we can echo.
    		$pagination['First'] = 0;
    		
    		if ($offset > 1)
    		{
    			$offset - 1;
    		}
    			
    		for($x = $offset - 8; $x < $offset + 8; $x++)
    		{
    			if ($x >= 0 && $x < $max_pages)
    			{
    				$pagination[$x + 1] = $x;
    			}
    		}
    		
    		if ($offset != $max_pages - 1)
    		{
    			$pagination['Next'] = $offset + 1;
    		}
    		
    		$pagination['Last'] = $max_pages == 0 ? '' : $max_pages - 1; // Minus 1 as $max_pages starts at 1 too.
    		
    		return $this->Out($pagination);
    	}
    ?>
    Code (markup):
    Then in the query that fetches the records I do

    LIMIT 
    		' . $offset * 50 . ', 50';
    Code (markup):
     
    Seph, Feb 4, 2007 IP
    Nocturne likes this.
  3. Nocturne

    Nocturne Member

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Hi thanks a bunch for your reply.

    I have just tried to make this work using the code that you posted but it returns a blank page. I will try again to see if I can get this working, if not then I'm hoping you can give me more clues about how to do this.

    But thank you again for the reply it's very kind of you :)
     
    Nocturne, Feb 4, 2007 IP
  4. vinodkv

    vinodkv Peon

    Messages:
    183
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    vinodkv, Feb 5, 2007 IP
    Nocturne likes this.
  5. Nocturne

    Nocturne Member

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Great! Thank you so much. I will fetch my reading glasses and read the tutorial you posted, I just skimmed through it and it seems like a great help for noobs like myself :)
     
    Nocturne, Feb 5, 2007 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Barti1987, Feb 5, 2007 IP
  7. Nocturne

    Nocturne Member

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #7
    Hello again,

    I appreciate all the feedback on this, but I just can't get this to work.
    I have read all the information supplied in this thread, but I think my current code is just too complicated for me to understand.

    I found another tutorial on pagination which I almost got working but then there were some problem with the links and some other stuff.

    Maybe I will have at look at this some other time...
    Thank you anyway :eek:
     
    Nocturne, Feb 6, 2007 IP
  8. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #8
    Give me your database name, table name and table structure.

    I will give you coding of paging.

    Thank you
     
    technoguy, Feb 7, 2007 IP
  9. Nocturne

    Nocturne Member

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #9
    Hey guys,

    I feel like I'm close to getting this to work I'm using the code below, and it displays the pagination part but no data.

    You can see an example at dailybeat.net/pagination.php

    Here is the code I'm working with, it's probably a minor error (I hope) if anyone has a clue to why this isn't working I would be very grateful.

    The code:

    
    <?php
    include ("./includes/config.php");
    include ("./includes/functions.php");
    
    function paginate($display, $pg, $total) {
      if(isset($_SERVER['QUERY_STRING']) && trim(
        $_SERVER['QUERY_STRING']) != '') {
        if(stristr($_SERVER['QUERY_STRING'], 'pg='))
          $query_str = '?'.preg_replace('/pg=\d+/', 'pg=', 
            $_SERVER['QUERY_STRING']);
        else
          $query_str = '?'.$_SERVER['QUERY_STRING'].'&pg=';
      } else
        $query_str = '?pg=';
        
      $pages = ($total <= $display) ? 1 : ceil($total / $display);
        
      $first = '<a href="'.$_SERVER['PHP_SELF'].$query_str.'1">«
    </a>';
      $prev = '<a href="'.$_SERVER['PHP_SELF'].$query_str.($pg - 1).'">
    ‹</a>';
      $next = '<a href="'.$_SERVER['PHP_SELF'].$query_str.($pg + 1).'">
    ›</a>';
      $last = '<a href="'.$_SERVER['PHP_SELF'].$query_str.$pages.'">
    »</a>';
       
      echo '<div><p align="center">';
      echo ($pg > 1) ? "$first : $prev :" : '« : ‹ :';
      
      $begin = $pg - 4;
      while($begin < 1)
        $begin++;
      $end = $pg + 4;
      while($end > $pages)
        $end--;
      for($i=$begin; $i<=$end; $i++)
        echo ($i == $pg) ? ' ['.$i.'] ' : ' <a href="'.
          $_SERVER['PHP_SELF'].$query_str.$i.'">'.$i.'</a> ';
        
     echo ($pg < $pages) ? ": $next : $last" : ': › : »';
      echo '</p></div>';
    }
    
    $display = 15;
    $pg = (isset($_REQUEST['pg']) && ctype_digit($_REQUEST['pg'])) ?
      $_REQUEST['pg'] : 1;
    $start = $display * $pg - $display;
    
    $result = mysql_query("SELECT count(*) FROM games");
    $total = mysql_result($result, 0);
    $news = mysql_query("SELECT * FROM games WHERE gamestatus = '1' AND gamelocation = '0' AND iconlocation = '0' ORDER BY gameid DESC LIMIT $start, $display");
    
    
    while ($row = mysql_fetch_array($news))
            {
    $gamedesc = stripslashes($row['gamedesc']);
                    $gametitle = $row['gametitle'];
                    $gametitle = stripslashes($gametitle);
                    $rating = $row['rating'];
                    $number_of_votes = $row['totalvotes'];
                    $gameid = $row['gameid'];
                    $gamelocation = $row['gamelocation'];
                    $iconlocation = $row['iconlocation'];
                    $gameicon = $row['gameicon'];
                    $featuredicon = $row['gameicon'];
                    $timesplayed = $row['timesplayed'];
                    
                    
    $gamedate = $row['dateadded'];
    if (!empty($gamedate))
    $gamedate = date("F j, Y", $gamedate); 
    else
    $gamedate = "";
          
                    
                    if (!empty($featuredicon)){
                          
                          if (strlen($gamedesc)>65)
                          $gamedesc = substr($gamedesc,0,200)." ";
                          
                          $featuredicon = $base_url."media/images/".$featuredicon;
                              
                          $title = make_friendly($gametitle);
                          
                          if ($iconlocation == 1)
                          $gameicon = $gameicon;
                          else
                          $gameicon = $base_url."media/images/".$gameicon;
                          
                          if ($rewrite ==0)
                          $gamelink = $base_url."index.php?action=playgame&gameid=".$gameid;
                          else
                          $gamelink = $base_url.$sedir."/".$gameid."/".$title;
    
    
    
               $content = $content."<tr>
    <td>
    <table width=\"100%\" class=\"gamebox\">
    <tr>             
    <td rowspan=\"2\" width=\"200\" class=\"gametitle\" align=\"center\" valign=\"top\">
    <a href=\"$gamelink\"><img src=\"$featuredicon\" class=\"0\" width=\"200\" height=\"150\" alt=\"$gametitle\"></a>
    </td>
    <td width=\"100%\" align=\"left\" class=\"listSmallText\" valign=\"top\">
    <a href=\"$gamelink\" class=\"gametitle\"><B>$gametitle</B></a><br><br>
    $gamedesc
    </td>
    <tr>
    <td class=\"gameinfo\"valign=\"bottom\">
    Added: $gamedate | Views: $gameplayed
    </td>
    </tr>
    </tr>
    </table>
    </td>
                 </tr>
    
    ";
    
                    }
    }
    
                
    paginate($display, $pg, $total);
    
    return $content;
    
    paginate($display, $pg, $total);
    
    ?> 
    
    Code (markup):
     
    Nocturne, Feb 9, 2007 IP