PDA

View Full Version : Previous and Next when the first id isn't 1


zonkd
11-23-2005, 10:44 AM
Is there a way to use Next and Previous on a site when the id to begin (and end at) is not 1, and in this case is 12.

This is the usual way I do it, but this time I need it to work from id = 12.

echo '<div class="Next">';
$next = $_GET['id'] + 1;
$previous = $_GET['id'] - 1;
$total = 34;


if($_GET['id'] == 1)
{ echo ' et al etc ...

Be grateful for some guidance, please.

ronaldb66
11-23-2005, 12:38 PM
You mean you want some special processing when the id is equal to the start or end value (like, not being able to decrease/increase it any further)? Why not specify both the start and end value as variables, and test for those instead of for a literal "1"?

missing-score
11-23-2005, 01:26 PM
Theres a couple of ways I can think to do this. It doesn't look like you are using MySQL, so thats out of the question. If you could somehow separate your content into an ID indexed array, eg:


<?php

$pages = array(
1 => '...',
4 => '...',
12 => '...',
13 => '...'
19 => '...'
);

// Ok, now we need to find the next key based on the current page
$offset = array_search( $_GET['id'], array_values( $pages ) );
$current = array_slice( array_keys( $pages ), ($offset-1), 3 );

?>


That should give you an array of 3 items, the current page, the next page id and the previous page ID, you could then get them from the array like so:


$last_page = $current[0];
$current_page = $current[1];
$next_page = $current[2];

echo $pages[$current_page];


This is untested, but it looks like it should work.

zonkd
11-23-2005, 01:40 PM
Hi ronaldb66 and missing-score

Yes, that's the story. But, missing-score, I think this might be a bit advanced for me yet ... but it looks brilliant. Thank you. I'll see if I can work it. Cheers

ronaldb66
11-23-2005, 03:46 PM
By the way: this might very well come in handy for my file-chunky-previous-next-thingy (http://www.codingforums.com/showthread.php?t=73192); I'm so learning a lot here! :thumbsup: (Like that's a surprise!)