Next - Previous link help

Discussion in 'PHP' started by olli460, Jan 14, 2009.

  1. #1
    Hello, Ive got a next and previous links setup, i was wondering how i would go about adding page numbers so people can choose the specific page.

    thanks in advance
     
    olli460, Jan 14, 2009 IP
  2. hassanahmad1

    hassanahmad1 Active Member

    Messages:
    150
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    TOTAL NO. OF PAGES = TOTAL RECORDS / RECORDS PER PAGE
    after that show the page number using a loop
    hope u understand what i mean :)
     
    hassanahmad1, Jan 14, 2009 IP
  3. olli460

    olli460 Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i understand what you mean but i dont get how i would put it in a loop to show each page number
     
    olli460, Jan 14, 2009 IP
  4. hassanahmad1

    hassanahmad1 Active Member

    Messages:
    150
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Assuming you pass the page number using a parameter "page"..
    then you can use something like:
    
    for ($i = 1; $i < $TOTAL_PAGES + 1; $i++)
      echo "<a href='index.php?page=$i'>$i</a>";
    
    Code (markup):
     
    hassanahmad1, Jan 14, 2009 IP
  5. olli460

    olli460 Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    At the moment im not doing it by pages though, im doing it by doing

    index.php?page=main&from=0
    then the next page is
    index.php?page=main&from=20
    and
    index.php?page=main&from=40

    etc etc

    So how would i link the actual 1, 2, 3 etc?
     
    olli460, Jan 15, 2009 IP
  6. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #6
    ok here is what i understand. You want to display page numbers according to your database entries?

    For example; you have 30 rows in one page and you want to display 10 rows per page. It should give links for 3 pages.

    You can do so by using this:
    
    // fetch data for your query.
    				if(isset($_GET['from'])) {
    				$get_pages = $_GET['from'];
    				if($get_pages == 1) {
    					$get_pages = 0;
    				} else {
    					$get_pages = ( $_GET['from'] - 1 )* 10;
    				}
    				
    				$query = "SELECT * 
    						FROM {TABLE_NAME} 
    						ORDER BY id DESC 
    						LIMIT {$get_pages} , 10";
    				$result = mysql_query($query);
    				if(!$result) {die("Error: ".mysql_error());}
                                    if(isset($_GET['from'])) {
    				while ($row = mysql_fetch_assoc($result)) {
                                    echo $row['fieldname']; // fieldname is the column name
                                   }
    
    PHP:
    The above query will fetch the data. Now we will display its links using a loop.

    
    // here is the next part:
    //gathering the requierd data from the database
    
    			$count = mysql_query("SELECT COUNT(*) FROM {TABLE_NAME} ");
    			$nog = mysql_fetch_row($count);
    			
    			$ge_page = $_GET['from'];
    			$x=$nog[0]/10;
    			
    			
    			echo "<table width='98%'><tr><td valign='top' width='6%'>Page: </td><td width='94%'>";
    			//the links generator
    			
    			$n=1;
    			for ($i=1; $i<= ceil($x); $i++) {
    			if ($i > 1) $n=$n+10;
    			
    			if($ge_page == $i) {
    				echo "<font color='#cc0000'><strong>[ ".$i." ]</strong></font> ";
    			} else {		
    				echo "<a href='the_page.php?from={$i}' >[ ";
    				echo $i;
    				echo " ]</a> ";
    				} 
    			}
    			
    			echo "</tr></td></table>";
    
    PHP:
     
    khan11, Jan 15, 2009 IP
  7. hassanahmad1

    hassanahmad1 Active Member

    Messages:
    150
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Okay, the following code can help you get the starting and ending record no.s by passing a page no. only:

    
                $from2 = $page *  $max_show;
                if ($from2 > $total) {
                   $diff = $total % $max_show;
                   $from2 = $total;
                   $from1 = $from2 - $diff;
                }
                else
                   $from1 = $from2 - $max_show;
    
    
                for ($i=$from1; $i < $from2; $i++) {
                      //your content here
                }
    
    
    Code (markup):
    Where
    $max_show is the no. of records to show per page.
    $total is the total no. of records.

    hope it helps
     
    hassanahmad1, Jan 15, 2009 IP
  8. olli460

    olli460 Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8

    I tryed this code but i keep getting end of document errors, Theres a few open brackets that haven't been closed but when i end them i dont get anything displaying.


    Maybe its best i post all my code so you can see excatly what i have so far.

    
    <?php
    include("connect.php");
    echo "<img src=\"images/featured.jpg\" />";
    
    if($_GET[from] == ""){ $startfrom=0; } else { $startfrom = $_GET[from]; }
    $next = $startfrom+20;
    $previous = $startfrom-20;
    
    $sql = "SELECT * FROM `db` WHERE vid_mainpage = 'yes' ORDER BY vid_id DESC LIMIT $startfrom, 20"; 
    $result = mysql_query($sql) or die(mysql_error());
    
    
    
    while($row = mysql_fetch_assoc($result))
    
    {
            $vid_id = $row['vid_id'];
            $vid_desc = $row['vid_desc'];
            $vid_cat = $row['vid_cat'];
            $vid_title = $row['vid_title'];
            $vid_tags = $row['vid_tags'];
            $vid_raters = $row['vid_raters'];
            $vid_rate = $row['vid_rate'];
            $vid_date = $row['vid_date'];
            $vid_user = $row['vid_user'];
            $vid_url = $row['vid_url'];
            $vid_screen = $row['vid_screen'];
            $vid_views= $row['vid_views'];
            $vid_length= $row['vid_length'];
    ?>
    <div align="center">
    <table border="1" width="90%" cellspacing="0" cellpadding="0" bgcolor="#151515" bordercolor="#444444">
    	<tr>
    		<td>
    		<table border="0" width="100%" cellspacing="3" cellpadding="2">
    			<tr>
    				<td width="201" rowspan="4">
    				<a href="index.php?page=view_video&vid_id=<?=$vid_id;?>"><img border="0" src="<?=$vid_screen;?>" width="201" height="154"></a></td>
    				<td width="1199"><b><a href="index.php?page=view_video&vid_id=<?=$vid_id;?>"><?=$vid_title;?></a></b></td>
    			</tr>
    			<tr>
    				<td width="1199"><?=$vid_views;?> <i>views</i></td>
    			</tr>
    			<tr>
    				<td width="1199">Runs for <?=$vid_length;?> <i> minutes</i></td>
    			</tr>
    			<tr>
    				<td width="1199"><a href="index.php?page=view_video&vid_id=<?=$vid_id;?>">Watch this video!</a></td>
    			</tr>
    		</table>
    		</td>
    	</tr>
    </table>
    </div>
    <br>
    <?php
    }
    echo "<p align=\"center\">"
    if ($startfrom != 0) 
      {
        echo "<a href=\"index.php?page=main&from=$previous\">Previous Page | </a>";
      }
    ?>
    <a href="index.php?page=main&from=<? echo $next; ?>"> Next Page</a></p>
    
    PHP:
    This is what i have so far. Hope this makes it easier
     
    olli460, Jan 15, 2009 IP
  9. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #9
    well, i couldn't find some major problem, could you please post the error which you get after trying ?

    also replace in query "DESC LIMIT $startfrom, 20"

    to
    DESC LIMIT {$startfrom} , 20
    PHP:

    it is a good habit to put your variables within mid brackets. also, the spaces should be equal on both sides of comma.
     
    khan11, Jan 15, 2009 IP
  10. olli460

    olli460 Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    When i put them two together and put my database details in i get the following error

     
    olli460, Jan 16, 2009 IP
  11. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #11
    these types of errors come when you don't close the tags correctly, like if you start if statement ;

    if($this == $that) {

    and you forget to close that, this error will come.

    plus, i caught one more syntax mistake that is:
    echo "<p align=\"center\">"
    if ($startfrom != 0)

    replace it with

    
    echo "<p align=\"center\">";
    if ($startfrom != 0) 
    
    PHP:
    you forgot a semi-colon to end the above echo command. try that and then tell me the results, also check which tag is causing the above error, check your pages.php at line 54 & 55. you must have missed some syntax somewhere or added some syntax twice.
     
    khan11, Jan 16, 2009 IP
  12. olli460

    olli460 Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Them errors are coming from your code you pasted though,

    I put them both into one file called pages.php to test it and edited it with my database details and i get the errors
     
    olli460, Jan 16, 2009 IP
  13. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #13
    oh ok then, let me recheck the code on my localhost to make it working and then i will paste it.
     
    khan11, Jan 16, 2009 IP
  14. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #14
    Ok heres the code, modify it according to your requirements, this code which im pasting is tested and works on my localhost, hope it works for you too.

    
    <?php
    // fetch data
    				if(isset($_GET['from'])) {
    				$get_pages = $_GET['from'];
    				if($get_pages == 1) {
    					$get_pages = 0;
    				} else {
    					$get_pages = ( $_GET['from'] - 1 )* 10;
    				}
    				
    				$query = "SELECT * 
    						FROM TABLE_NAME 
    						ORDER BY id DESC 
    						LIMIT {$get_pages} , 10";
    				$result = mysql_query($query);
    				check_query($result);
    				}
    				if(isset($_GET['from'])) {
    				while ($row = mysql_fetch_assoc($result)) {		
    					echo $row['SHOW_DATA'];						   
    				}
    					echo "<br /><br />";				
    				
    			}
    			// don't display this if the page is not showing up.
    			if(!isset($_GET['from'])) {
    				echo "";
    			} else {
    			//gathering the requierd data from the database
    
    			$count = mysql_query("SELECT COUNT(*) FROM TABLE_NAME ");
    			$nog = mysql_fetch_row($count);
    			
    			$ge_page = $_GET['from'];
    			$x=$nog[0]/10;
    			
    			
    			echo "<table width='98%'><tr><td valign='top' width='6%'>Page: </td><td width='94%'>";
    			//the links generator
    			
    			$n=1;
    			for ($i=1; $i<= ceil($x); $i++) {
    			if ($i > 1) $n=$n+10;
    			
    			if($ge_page == $i) {
    				echo "<font color='#000'><strong>[ ".$i." ]</strong></font> ";
    			} else {		
    				echo "<a href='index.php?page=main&from={$i}' >[ ";
    				echo $i;
    				echo " ]</a> ";
    				} 
    			}
    			
    			echo "</tr></td></table>";
    			
    			}
    ?>
    
    PHP:
    Please post your results.

    EDITED:

    and at this line: echo $row['SHOW_DATA'];

    put your field names according to your saved database fields. the order should be like this: $row['COLUMN_NAME']
     
    khan11, Jan 16, 2009 IP
  15. olli460

    olli460 Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    
    <?php
    // fetch data
    
    include("connect.php");
                    if(isset($_GET['from'])) {
                    $get_pages = $_GET['from'];
                    if($get_pages == 1) {
                        $get_pages = 0;
                    } else {
                        $get_pages = ( $_GET['from'] - 1 )* 10;
                    }
                   
                    $query = "SELECT * FROM db ORDER BY id DESC LIMIT {$get_pages} , 10";
                    $result = mysql_query($query);
                    check_query($result);
                    }
                    if(isset($_GET['from'])) {
                    while ($row = mysql_fetch_assoc($result)) {  
                        echo $row['vid_title'];         
                    }
                        echo "<br /><br />";               
                   
                }
                // don't display this if the page is not showing up.
                if(!isset($_GET['from'])) {
                    echo "123";
                } else {
                //gathering the requierd data from the database
    
                $count = mysql_query("SELECT COUNT(*) FROM db");
                $nog = mysql_fetch_row($count);
               
                $ge_page = $_GET['from'];
                $x=$nog[0]/10;
               
               
                echo "<table width='98%'><tr><td valign='top' width='6%'>Page: </td><td width='94%'>";
                //the links generator
               
                $n=1;
                for ($i=1; $i<= ceil($x); $i++) {
                if ($i > 1) $n=$n+10;
               
                if($ge_page == $i) {
                    echo "<font color='#000'><strong>[ ".$i." ]</strong></font> ";
                } else {       
                    echo "<a href='index.php?page=main&from={$i}' >[ ";
                    echo $i;
                    echo " ]</a> ";
                    }
                }
               
                echo "</tr></td></table>";
               
                }
    ?>
    
    PHP:
    Using this code im getting

    Fatal error: Call to undefined function check_query() on line 15
     
    olli460, Jan 16, 2009 IP
  16. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #16
    oops... :( actually i defined that function in my other file, you can remove that function, instead use this:

    replace
    check_query($result);

    with

    if(!$result) {die(mysql_error());}
     
    khan11, Jan 16, 2009 IP
  17. olli460

    olli460 Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Hello,

    Thanks, this work nice now. Just 1 more problem its got

    
                if(!isset($_GET['from'])) {
                    echo "123";
                } else {
    
    PHP:
    But how can i make it so if you type domain.com/pages.php it automatically loads first page?

    Thanks in advance
     
    olli460, Jan 17, 2009 IP
  18. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #18
    sorry, what do you mean?

    you want to redirect people if they try to access pages.php without pages.php?main=blah&page=Blah etc.. ?

    you can do so by using this code:
    
    if(!$_GET) { 
           header("Location: index.php");
    }
    
    PHP:
     
    khan11, Jan 17, 2009 IP
  19. olli460

    olli460 Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Well im including the pages.php in another page so the orignal file that is being included needs to go defualt to page 1 because at the moment if i go stright to pages.php it displays nothing.

    And i cant include pages.php?page=1

    so how can i make the first defualt page be page #1 ?

    Thx
     
    olli460, Jan 17, 2009 IP
  20. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #20
    What i understand is that you have all the data on pages.php but it is displayed after we request pages.php?page=1 ? right?

    you can do so by using this idea:

    
    if(!$_GET) {
           header("Location: ?page=1");
    }
    
    PHP:
    put this code on top of your pages.php file.
     
    khan11, Jan 17, 2009 IP