Jump to content

[SOLVED] Problems with php paging


daveh33

Recommended Posts

Hi - I have followed a tutorial online for paiging my mysql results but it doesn't seem to display a link to the next page..

 

the code is

 

<?php
require("dbconnect.php");
   @ $rpp;        //Records Per Page
    @ $cps;        //Current Page Starting row number
    @ $lps;        //Last Page Starting row number
    @ $a;        //will be used to print the starting row number that is shown in the page
    @ $b;         //will be used to print the ending row number that is shown in the page 
if(empty($_GET["cps"]))
    {
        $cps = "0";
    }
    else
    {
        $cps = $_GET["cps"];
    } 
  $a = $cps+1;
    $rpp = "1";

    $lps = $cps - $rpp; //Calculating the starting row number for previous page 

$q="Select SQL_CALC_FOUND_ROWS * from videos limit $cps, $rpp";
    $rs=mysql_query($q) or die(mysql_error());
    $nr = mysql_num_rows($rs); //Number of rows found with LIMIT in action 

    $q0="Select FOUND_ROWS()";
    $rs0=mysql_query($q0) or die(mysql_error());
    $row0=mysql_fetch_array($rs0);
    $nr0 = $row0["FOUND_ROWS()"]; //Number of rows found without LIMIT in action 
    if (($nr0 < 10) || ($nr < 10))
    {
           $b = $nr0;
    }
    else
    {
        $b = ($cps) + $rpp;
    } 
  
// $result = mysql_query("SELECT * from videos ORDER BY RAND()")  or die(mysql_error());
while($row = mysql_fetch_array($rs)){
$image = $row['imgurl'];
$video = $row['videourl'];
if ($image && $video) {
echo "<p align=\"center\"><img src=\"$image\" / ><br><a href=\"$video\">DOWNLOAD NOW</a><br><br>";
}
}
if ($cps <> 0)
    {
        $prv =  "<a href='?cps=$lps'>Previous</a>";
    }
    else   
    {
        $prv =  "<font color='cccccc'>Previous</font>";
    } 
?>

Link to comment
https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/
Share on other sites

I followed the tutorial and copied the code - it still isnt working

 

the code is

 

<?php
require("dbconnect.php");

    @mysql_connect($localhost, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER");
    @mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB");

    $limit          = 7;               
    $query_count    = "SELECT count(*) FROM videos";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM videos LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#E0E0E0"; // light gray

    echo("<table>");
    
    while($row = mysql_fetch_array($result)){
        if ($bgcolor == "#E0E0E0"){
            $bgcolor = "#FFFFFF";
        }else{
            $bgcolor = "#E0E0E0";
        }
$image = $row['imgurl'];
$video = $row['videourl'];
   echo "<p align=\"center\"><img src=\"$image\" / ><br><a href=\"$video\">DOWNLOAD NOW</a><br><br>";
  
  
    }

    echo("</table>");

    if($page != 1){ 
        $pageprev = $page--;
        
        echo("<a href=\"?&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=\"?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); 
    }
    
    mysql_free_result($result);
?>

 

It displays the results ok - but it just links PREV NEXT with no hyperlinks. Someone please help me!

Change from this:

 

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 

 

To this:

 

$page = $_GET['page'];
    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $limit * ($page - 1); 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html>
<link rel="stylesheet" type="text/css" href="style.css" />
<h1><p align="center">WELCOME TO WOW TELECOM<p>
</p></h1>
</head>
<p align="center">
<b>THANKS FOR YOUR PURCHASE</b><br><FONT COLOR="#FF0000""><b>Feel free to download as many of the below videos as you like! ENJOY!!</FONT></b><br>
<img src="hardcoreheader.gif" alt="TOP RATED" /><p>

<p align="center"><b>SHE JUST LOVES TO SUCK COCK</b><br><img src="http://www.mobycam.co.uk/Images/img5_702601.gif" /><br>
<a href="http://www.mobycam.co.uk/videos/ten/vid5_702630.3gp"><font size="2">DOWNLOAD LINK</a><font><br>

<br><FONT SIZE="3" COLOR="#0000FF">DOWNLOAD THE REST</FONT>
<table><p align="center"><img src="http://www.mobycam.co.uk/videos/Videos2/IMAGES/img_700101.gif" / ><br><a href="http://www.mobycam.co.uk/videos/Videos2/Videos/vid_700130.3gp">DOWNLOAD NOW</a><br><br><p align="center"><img src="http://www.mobycam.co.uk/videos/Videos2/IMAGES/img_700201.gif" / ><br><a href="http://www.mobycam.co.uk/videos/Videos2/Videos/vid_700230.3gp">DOWNLOAD NOW</a><br><br><p align="center"><img src="http://www.mobycam.co.uk/videos/Videos2/IMAGES/img_700301.gif" / ><br><a href="http://www.mobycam.co.uk/videos/Videos2/Videos/vid_700330.3gp">DOWNLOAD NOW</a><br><br><p align="center"><img src="http://www.mobycam.co.uk/videos/Videos2/IMAGES/img_700401.gif" / ><br><a href="http://www.mobycam.co.uk/videos/Videos2/Videos/vid_700430.3gp">DOWNLOAD NOW</a><br><br><p align="center"><img src="http://www.mobycam.co.uk/videos/Videos2/IMAGES/img_700501.gif" / ><br><a href="http://www.mobycam.co.uk/videos/Videos2/Videos/vid_700530.3gp">DOWNLOAD NOW</a><br><br><p align="center"><img src="http://www.mobycam.co.uk/videos/Videos2/IMAGES/img_700601.gif" / ><br><a href="http://www.mobycam.co.uk/videos/Videos2/Videos/vid_700630.3gp">DOWNLOAD NOW</a><br><br><p align="center"><img src="http://www.mobycam.co.uk/videos/Videos2/IMAGES/img_700701.gif" / ><br><a href="http://www.mobycam.co.uk/videos/Videos2/Videos/vid_700730.3gp">DOWNLOAD NOW</a><br><br></table>PREV7 1 NEXT7</p>
<br>
<p align="center" class="footer">Copyright © Mobile Media Production Ltd 2007 </p>
<p align="center" class="footer"> </p>

</body>
</html>

Echo out the following variables to see what their values are:

$totalrows;

$limitvalue;

$numofpages;

 

See if there's an error in any mysql query:

    $result_count   = mysql_query($query_count) or die(mysql_error());    

 

I found this piece of code here, which makes absolutely no sense to me as the piece of code (for loop) right above it does the correct pagination.

    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }

echo "total rows";
echo $totalrows;
echo "<P>limit value:";
echo $limitvalue;
echo "<P>Number pages:";
echo $numofpages;

produces: -

total rows1

limit value:0

Number pages:

 

 

Shall I swap those 2 pieces of code you just said? That was just what I copied from the tutorial

Ok, to be honest, I hate this pagination code. Everyone keeps linking to this one and it always freaking fails... Here's the one I use :-D

 

http://www.phpfreaks.com/tutorials/73/1.php

 

Here's your code implemented into the above:

<?php

// Database Connection
require("dbconnect.php");

    @mysql_connect($localhost, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER");
    @mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB");


// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 7;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results); 

// Perform MySQL query on only the current page number's results

$sql = mysql_query("SELECT * FROM videos LIMIT $from, $max_results");

//set initial $bgcolor just to be logical and to have it previously defined
$bgcolor = "#E0E0E0"; // light gray

//echo the start of the table
echo "<table>\n";//the \n are to make the html source look a little cleaner (line by line), the next html echoed will be on the next line

while($row = mysql_fetch_array($sql)){
        if ($bgcolor == "#E0E0E0"){$bgcolor = "#FFFFFF";}else{$bgcolor = "#E0E0E0";}
        $image = $row['imgurl'];
        $video = $row['videourl'];
        //note, I used single quotes for the echo statement while escaping the variables " '.$var.' "
        echo '<p align="center"><img src="'.$image.'" / ><br><a href="'.$video.'">DOWNLOAD NOW</a><br><br>\n';
}

//let's clear the memory of the query now so we can perform other queries a little faster
mysql_free_result($sql);

//end the table, duh o_o
echo "</table>\n";

// Figure out the total number of results in DB:
$total_results = mysql_num_rows(mysql_query("SELECT COUNT(*) as Num FROM videos"));

// Figure out the total number of pages. Always round up using ceil() [10.1 -> 11, IE: 10.1 pages, thus an 11th page to display the ".1" extra
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?>

 

I won't explain how each part works, check out the tutorial for the best explanation. It's simple enough for you to modify and the variables are named.. logically o_o

 

I modified some of your echo statements, added some \n line breaks to make the html source slightly cleaner, etc...

 

Copy and paste this into a diff file and let me know if that one works.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.