irkevin Posted August 13, 2007 Share Posted August 13, 2007 Ok, i read all the post about paging but still it doesn't solve my problem. That's why i'm here now Here's a code i got on phpfreaks for paging <?php //connect to database $conn = mysql_connect("localhost","blah","****"); mysql_select_db("kevin",$conn); // 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 = 6; // 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 $temp_go_to = $_GET['go_to']; $sql = mysql_query("SELECT * FROM multi_file WHERE mult_id = $temp_go_to LIMIT $from, $max_results"); while($myarray = mysql_fetch_array($sql)){ // Build your formatted results here. echo $myarray['synopsis']."<br /><br />"; echo "<font color=\"#3399FF\">Episode:</font>" . " " .$myarray['file_name']."<br />"; echo "<font color=\"#3399FF\">Title:</font>" . " " .$myarray['file_description']."<br />"; echo "<font color=\"#3399FF\">Link:</font>" ." ". "<a href=\"".$myarray['file_link']. "\" target=\"_blank\">Click Here To Download</a>"; } // Figure out the total number of results in DB: $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM multi_file"),0); // Figure out the total number of pages. Always round up using ceil() $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=\"multimedia.php?go_to={$temp_go_to}&page=$prev\">Previous</a> "; } for($i = 1; $i <= $total_pages; $i++){ if(($page) == $i){ echo "$i "; } else { echo "<a href=\"multimedia.php?go_to={$temp_go_to}&page=$i\">$i</a> "; } } // Build Next Link if($page < $total_pages){ $next = ($page + 1); echo "<a href=\"multimedia.php?go_to={$temp_go_to}&page=$next\">Next>></a>"; } echo "</center>"; ?> So, i have a menu on my site.. like this Bleach DeathNote Dragon Ball etc etc etc etc when i click on bleach, it goes on a page name multimedia.php, there it shows the data which i inserted in mysql database.. when i click on death note, it shows the data from mysql about death note on the same page..thats ok For bleach, i have the pagination set and it works fine because i have set it to 6 information per page and there are moe than 6 info for bleach.. it shows 5 pages in total for bleach. i like that, but for death note, i have only three information and it still show 5 pages ! this is not normal, isn't it? there is no data for dragon ball and it still show the 5 links.. how come ??? Same for the other one :S if there is no data, it shouldn't show the link, right? the query for my menu go like this <?php //connect to database $conn = mysql_connect("localhost","blah","****"); mysql_select_db("kevin", $conn); $sql = "Select * from multimedia"; $result = mysql_query($sql, $conn) or die(mysql_error()); while($myarray = mysql_fetch_array($result)) { ?> <a href=multimedia.php?go_to=<?php echo $myarray['mult_id']; ?> > <?php echo $myarray['mult_name']; ?></a> <br /> <?php }; ?> i've used $_GET['go_to'] to show the data on the multimedia.php page and $_GET['page'] for the pagination can someone tell me whats wrong? for bleach everything is ok.. but for dragon ball there's no data on the page yet, why i'm i seeing the next link?? :S my website is http://www.mu-anime.com have a look :S Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/ Share on other sites More sharing options...
irkevin Posted August 13, 2007 Author Share Posted August 13, 2007 no one?? Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-322108 Share on other sites More sharing options...
sasa Posted August 13, 2007 Share Posted August 13, 2007 change $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM multi_file"),0); to $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM multi_file WHERE mult_id = $temp_go_to"),0); Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-322117 Share on other sites More sharing options...
irkevin Posted August 13, 2007 Author Share Posted August 13, 2007 yay you rock man.. i was sure it was something very easy to do.. neways.. thans for your help Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-322290 Share on other sites More sharing options...
irkevin Posted August 18, 2007 Author Share Posted August 18, 2007 yeps i know the topic is solved but well, just like this forum, for the pagination, it's like this 1 2 3 4 5 ........ 41 how do i make this code look like this?? See what i mean? Thanks in advance mates Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-327266 Share on other sites More sharing options...
irkevin Posted August 18, 2007 Author Share Posted August 18, 2007 No one? ??? Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-327299 Share on other sites More sharing options...
irkevin Posted August 18, 2007 Author Share Posted August 18, 2007 wow there's no one to help with this code? Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-327330 Share on other sites More sharing options...
sasa Posted August 18, 2007 Share Posted August 18, 2007 try to change for($i = 1; $i <= $total_pages; $i++){ if(($page) == $i){ echo "$i "; } else { echo "<a href=\"multimedia.php?go_to={$temp_go_to}&page=$i\">$i</a> "; } } with $how_much_before = 2; $how_much_after = 2; $pages = array(1); if ($page > $how_much_before + 2) $pages[] = '...'; for ($i = max($page - $how_much_before, 2); $i < min($page + $how_much_after + 1,$total_pages); $i++) $pages[] = $i; if ($page < $total_pages - $how_much_after - 1) $pages[] = '...'; $pages[] = $total_pages; foreach ($pages as $i){ if($page == $i or $i == '...'){ echo "$i "; } else { echo "<a href=\"multimedia.php?go_to={$temp_go_to}&page=$i\">$i </a> "; } } Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-327335 Share on other sites More sharing options...
sasa Posted August 18, 2007 Share Posted August 18, 2007 ups change $pages[] = $total_pages; to if ($page < $total_pages) $pages[] = $total_pages; Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-327342 Share on other sites More sharing options...
irkevin Posted August 18, 2007 Author Share Posted August 18, 2007 i'll try this .. i'll keep u updated Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-327796 Share on other sites More sharing options...
irkevin Posted August 19, 2007 Author Share Posted August 19, 2007 many thanks man. it works.. but i have a little favor.. you mind explain me what that bit of code says?? Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-327857 Share on other sites More sharing options...
sasa Posted August 19, 2007 Share Posted August 19, 2007 generate $pages array that have all value in navigation part $how_much_before = 2;<-- define how much link want to display before current page $how_much_after = 2; (for this forum value is 5) $pages = array(1); <-- always set page no 1 if ($page > $how_much_before + 2) $pages[] = '...'; <-- check if I need ... for ($i = max($page - $how_much_before, 2); $i < min($page + $how_much_after + 1,$total_pages); $i++) $pages[] = $i;<-- add middle part but not 1st and last page if ($page < $total_pages - $how_much_after - 1) $pages[] = '...'; <-- check if I need ... if ($total_pages > 1) $pages[] = $total_pages; <-- add last page if number of pages is > 1 (I tested after and i see if i have just 1 page in pagination it shows 2 times) foreach ($pages as $i){ <--do what you do before but use just $i in pages array if($page == $I or $i == '...'){ <-- no link for current page and for ... Quote Link to comment https://forums.phpfreaks.com/topic/64604-solved-little-paging-problem-with-a-menu-from-database/#findComment-327905 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.