Northern Flame Posted January 6, 2008 Share Posted January 6, 2008 I created a Pagination tutorial that I plan to submit to phpfreaks soon, but someone asked me to add something and I thought I'd try to add this before submitting it here. Someone recommended that I tell the script only to display a certain amount of pages so that it doesn't mess up the template or the website. So that instead of being like <<PREV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 NEXT>> I want it to be like <<PREV 1 2 3 4 5 ... 14 16 17 18 19 20 NEXT>> or something like that, just so that it shortens the links. Can someone guide me on how I can do this? Heres the script: <?php include('mysql.php'); // We are including the file we made with your MySQL info $conn = mysql_connect($host, $user, $pass); // Connect to MySQL mysql_select_db($db, $conn)or die(mysql_error()); // Select Database // We are going to establish a few variables so that we can start $limit = 14; // This will give us 14 results per page, change this if you want if(empty($_GET['page'])){ $page = 1; $s_limit = 0; } else{ $page = $_GET['page']; $s_limit = ($page - 1) * $limit; } /* $page tells us which page we are on and $s_limit tells us which previous results to exclude */ $query = mysql_query("SELECT * FROM mp3 ORDER BY artist LIMIT $s_limit, $limit"); // We are going to order the results by the name of the artist so they are organized well on the page $query2 = mysql_query("SELECT * FROM mp3"); // This query is for $num $num = mysql_num_rows($query2); // This tells us how many rows are in our results $blow = $num / $limit; $blown = explode('.', $blow); if(empty($blown[1])){ $top = $blown[0]; } else{ $top = $blown[0] + 1; } // That tells us how many total pages we are going to have // Variables for Previous and Next Links $prev = $page - 1; $next = $page + 1; /* Now we are going to put in the links, <<PREV 1 2 3 (etc.) NEXT>> */ if($page == 1){ echo '«PREV '; } else{ echo '<a href="data.php?page='. $prev .'">«PREV</a> '; } for($x = 1; $x < $page; $x += 1){ echo '<a href="data.php?page='. $x .'">'. $x .'</a> '; } echo $page . ' '; for($y = $page + 1; $y <= $top; $y += 1){ echo '<a href="data.php?page='. $y .'">'. $y .'</a> '; } if($page == $top){ echo 'NEXT»'; } else{ echo '<a href="data.php?page='. $next .'">NEXT»</a>'; } // Create Some Seperation from our results and the data echo "\n<br>\n<br>\n"; // Get the data from our databse and echo it while($row = mysql_fetch_array($query)){ echo '<a href="http://www.yourwebsite.com/directory/'. $row['file'] .'.mp3">'. $row['name'] .' by '. $row['artist'] .'</a><br>'."\n"; } // Be Sure To Edit The Link! /* Re- Create the links <<PREV 1 2 3 (etc) NEXT>> */ if($page == 1){ echo '«PREV '; } else{ echo '<a href="data.php?page='. $prev .'">«PREV</a> '; } for($x = 1; $x < $page; $x += 1){ echo '<a href="data.php?page='. $x .'">'. $x .'</a> '; } echo $page . ' '; for($y = $page + 1; $y <= $top; $y += 1){ echo '<a href="data.php?page='. $y .'">'. $y .'</a> '; } if($page == $top){ echo 'NEXT»'; } else{ echo '<a href="data.php?page='. $next .'">NEXT»</a>'; } // Close Connection mysql_close($conn); ?> the SQL and more info is located here: http://forums.tizag.com/showthread.php?t=5670 thanks for any replies I get Quote Link to comment https://forums.phpfreaks.com/topic/84703-solved-pagination-if-page-10/ Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 <<PREV 1 2 3 4 5 ... 14 16 17 18 19 20 NEXT>> I don't see a pattern in that. Do you mean: <<PREV 1 2 3 4 5 ... 16 17 18 19 20 NEXT>> ? Quote Link to comment https://forums.phpfreaks.com/topic/84703-solved-pagination-if-page-10/#findComment-431690 Share on other sites More sharing options...
Northern Flame Posted January 6, 2008 Author Share Posted January 6, 2008 yea sorry about that, i didnt really put in a pattern i just wanted 2 paint a mental picture, but yea something like that Quote Link to comment https://forums.phpfreaks.com/topic/84703-solved-pagination-if-page-10/#findComment-431696 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 Then just change your loop. <?php $skip = -1; if ($page > 10) $skip = 5; if ($skip = -1){ for ($blah=0; $blah<$page; $blah++){ // display links // }} else { for ($blah=0; $blah<=$skip; $blah++){ // display links // } echo " ... "; for ($blah=$page; $blah>=$page-$skip; $blah--){ // display links // }} ?> Quote Link to comment https://forums.phpfreaks.com/topic/84703-solved-pagination-if-page-10/#findComment-431699 Share on other sites More sharing options...
Northern Flame Posted January 6, 2008 Author Share Posted January 6, 2008 well i havnt slept in days so im going to go to sleep right now and take a look at this in the morning, from what i see it looks like it will work but ill try it out in the morning, thanks for the suggestion! Quote Link to comment https://forums.phpfreaks.com/topic/84703-solved-pagination-if-page-10/#findComment-431701 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.