daveh33 Posted October 31, 2007 Share Posted October 31, 2007 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/ Share on other sites More sharing options...
atlanta Posted October 31, 2007 Share Posted October 31, 2007 http://www.phpfreaks.com/tutorials/43/0.php Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382032 Share on other sites More sharing options...
daveh33 Posted October 31, 2007 Author Share Posted October 31, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382316 Share on other sites More sharing options...
revraz Posted October 31, 2007 Share Posted October 31, 2007 I hope that isnt your real DB login info... Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382319 Share on other sites More sharing options...
kratsg Posted October 31, 2007 Share Posted October 31, 2007 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); Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382336 Share on other sites More sharing options...
daveh33 Posted October 31, 2007 Author Share Posted October 31, 2007 That change hasn't effected the result Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382340 Share on other sites More sharing options...
kratsg Posted October 31, 2007 Share Posted October 31, 2007 What is this so-called result looking like? Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382350 Share on other sites More sharing options...
daveh33 Posted October 31, 2007 Author Share Posted October 31, 2007 With 150 eligible database entries - it displays 7 loops of the while statement then "PREV7 1 NEXT7" with no hyperlink no next/prev pages Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382352 Share on other sites More sharing options...
kratsg Posted October 31, 2007 Share Posted October 31, 2007 When you get the results page, copy the source code, paste it in between some [ code ][ /code ] tags so I can see what is being generated (not the source code for the php, but for the html generated). Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382354 Share on other sites More sharing options...
daveh33 Posted October 31, 2007 Author Share Posted October 31, 2007 <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> Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382359 Share on other sites More sharing options...
kratsg Posted October 31, 2007 Share Posted October 31, 2007 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> "); } } Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382369 Share on other sites More sharing options...
daveh33 Posted October 31, 2007 Author Share Posted October 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382371 Share on other sites More sharing options...
kratsg Posted October 31, 2007 Share Posted October 31, 2007 Just the first one with the mysql_query, you've got an error there, as you're only getting one row returned... :-o Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382373 Share on other sites More sharing options...
daveh33 Posted October 31, 2007 Author Share Posted October 31, 2007 The number must be calculated incorrectly as each row provides a hyperlink & image url - the html code shows the results of 7 rows Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382377 Share on other sites More sharing options...
kratsg Posted November 1, 2007 Share Posted November 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382484 Share on other sites More sharing options...
daveh33 Posted November 1, 2007 Author Share Posted November 1, 2007 Thats for that code. I tried saving it as a different page and your right it does produce a much clearer html page - but at the bottom it shows: - Select a Page 1 With no hyperlinks again.. Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382626 Share on other sites More sharing options...
daveh33 Posted November 1, 2007 Author Share Posted November 1, 2007 Not to worry I have fixed the code I changed: - // $total_results = mysql_num_rows(mysql_query("SELECT COUNT(*) as Num FROM videos")); $query = mysql_query("SELECT * from videos"); $total_results = mysql_num_rows($query); Quote Link to comment https://forums.phpfreaks.com/topic/75518-solved-problems-with-php-paging/#findComment-382634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.