edenwax
VIP
v5 Beta Tester[M:5000]
Posts: 1,266
|
Post by edenwax on Aug 5, 2011 21:58:26 GMT
Alright, so I've been working on and off on a web comic script for quite some time and I'm seriously stumped.
The way I have it set up, I can't find an efficient way to hide the "next" comic nav link when the next comic id doesnt exist.
For example index.php?comic=3 exists, and index.php?comic=4 doesnt. On comic=3 I want the "next" link to be hidden.
|
|
Nick
VIP
v5 Beta Tester[M:5000]
Philadelphia Eagles: 8-8
Posts: 2,239
|
Post by Nick on Aug 6, 2011 0:06:13 GMT
Is this in PHP? This code you have to manually set the limit for. I didn't test it, but see if this works:
<?php $current = $_GET['comic']; $limit = "3"; $nextlink = "<a href=\"/index.php?comic=".$current++."\">Next</a>"; $previouslink = "<a href=\"/index.php?comic=".$current-1."\">Previous</a>";
function links { if($current <= '0') { echo $nextlink; } if($current >= $limit) { echo $previouslink; } else { echo $nextlink." | ".$previouslink; } }
if($current == '1') { // SHOW FIRST COMIC } if ($current == '2') { // SHOW SECOND COMIC } if($current=='3'){ // SHOW THIRD COMIC } links(); ?>
|
|
edenwax
VIP
v5 Beta Tester[M:5000]
Posts: 1,266
|
Post by edenwax on Aug 6, 2011 2:53:47 GMT
Well to prevent overly huge code, I use databases. So the author can upload the comic title, date, description and comic in one form. If you want I can send you the code, its a mess of failed experiments, but you may be able to help.
|
|
Nick
VIP
v5 Beta Tester[M:5000]
Philadelphia Eagles: 8-8
Posts: 2,239
|
Post by Nick on Aug 6, 2011 2:54:58 GMT
Sure. I'd be easier to work with if I could see how it worked.
|
|
|
Post by (/iPokemon/) on Aug 7, 2011 14:46:56 GMT
I have an awesome PHP Pagination class I found somewhere that could be of use to you. I'm using it on my Game's forum that I coded <?php class pageMe { public function __construct() { } public function calculate_pages($total_rows, $rows_per_page, $page_num) { $arr = array(); // calculate last page $last_page = ceil($total_rows / $rows_per_page); // make sure we are within limits $page_num = (int) $page_num; if ($page_num < 1) { $page_num = 1; } elseif ($page_num > $last_page) { $page_num = $last_page; } $upto = ($page_num - 1) * $rows_per_page; $arr['limit'] = 'LIMIT '.$upto.',' .$rows_per_page; $arr['current'] = $page_num; if ($page_num == 1) $arr['previous'] = $page_num; else $arr['previous'] = $page_num - 1; if ($page_num == $last_page) $arr['next'] = $last_page; else $arr['next'] = $page_num + 1; $arr['last'] = $last_page; $arr['info'] = 'Page '.$page_num.' of '.$last_page; $arr['pages'] = $this->get_surrounding_pages($page_num, $last_page, $arr['next']); return $arr; } function get_surrounding_pages($page_num, $last_page, $next) { $arr = array(); $show = 5; // how many boxes // at first if ($page_num == 1) { // case of 1 page only if ($next == $page_num) return array(1); for ($i = 0; $i < $show; $i++) { if ($i == $last_page) break; array_push($arr, $i + 1); } return $arr; } // at last if ($page_num == $last_page) { $start = $last_page - $show; if ($start < 1) $start = 0; for ($i = $start; $i < $last_page; $i++) { array_push($arr, $i + 1); } return $arr; } // at middle $start = $page_num - $show; if ($start < 1) $start = 0; for ($i = $start; $i < $page_num; $i++) { array_push($arr, $i + 1); } for ($i = ($page_num + 1); $i < ($page_num + $show); $i++) { if ($i == ($last_page + 1)) break; array_push($arr, $i); } return $arr; } } ?>
Use it like this: $page = (isset($_GET["comic"])) ? $_GET["comic"]:1; $sql = mysql_query("SELECT * FROM `table`"); $pagination = new pageMe(); $links = $pagination->calculate_pages(mysql_num_rows($sql),1,$page); //total number of rows, how many per page, current page
And if you were to var_dump $links, this is something you'd get:
Array
(
[limit] => LIMIT 0,10
[current] => 1
[previous] => 1
[next] => 2
[last] => 7
[info] => Page (1 of 7)
[pages] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
)
So.. use it like $links["limit"] in your mysql_query to limit the number of rows displayed, $links["pages"] as a foreach() or just show first and last page link
|
|
Nick
VIP
v5 Beta Tester[M:5000]
Philadelphia Eagles: 8-8
Posts: 2,239
|
Post by Nick on Aug 7, 2011 18:28:36 GMT
Sorry, forgot to post that we figured it out! Thanks iPokemon!
|
|
|
Post by Nero Yami (banned) on Aug 7, 2011 19:38:15 GMT
hehe very comical indeed!
|
|
|
Post by (/iPokemon/) on Aug 8, 2011 1:17:54 GMT
hehe very comical indeed! Yea, that's just hilarious. Yea, np, just thought I'd share.
|
|
|
Post by robert (banned) on Aug 8, 2011 11:35:08 GMT
haha
|
|
taz
Junior Poster
Posts: 10
|
Post by taz on Aug 15, 2011 3:24:15 GMT
Wow that is some nice coding. Are you intending to make it a downloadable software?
|
|