PDA

View Full Version : How can I add a link like this to my photo album?


John_Saunders
11-24-2002, 06:21 PM
I am going to put a personal photo album online and I have my links to the different photos/descriptions using the method below. How can I add a "Next" link to the bottom of every description instead of only having the link off the thumbnail?

<?php
// If the $_GET['id'] is not here, you will get an error, so check
if(!isset($_GET['id'])) $_GET['id'] = "";

$page[] = "1";
$page[] = "2";

// check to see if it is valid.
if(!in_array($_GET['id'], $page)) $_GET['id'] = "";

// do the stuff
switch($_GET['id']) {
case "1":
include("photo1.php");
break;
case "2":
include("photo2.php");
break;
default:
include("photo1.php");
break;
}
?>

I would like to make it so I can add the link at the bottom and it will automatically put the link to id=2 if you're on id=1 and so on. How can I make it so if a visitor is on the last photo or id=20, it will know not to print the "Next" link since there is no id=21?

Any help would be greatly appreciated.


John

raptori
11-24-2002, 06:58 PM
make a counter....display the picture with the id 1 in th beginning then when you write the link make a counter that adds one to the link and it would keep going until the end of the array.
ex:

<?php
switch($_GET['id'])_{

case"1":

include("photo1.php");

break;

case"2":

include("photo2.php");

break;

default:

include("photo1.php");

break;

}
$id += 1;

echo "page.php?page=$id";
?>

then the ID would keep getting +1 bigger every time.

druffus
11-24-2002, 07:20 PM
I am gonna shorten your code a little
<?php

$page = Array('1','2');

//This checks first if it was sent to the page with GET method,
//then will check if in the array. if it is not in the array it will default to 1

$id = (!isset($_GET['id']) ) ? 'no id' : (!in_array($_GET['id'], $page)) ? '1' : $_GET['id'] ;

//Instead of the switch statement you can just add the id on
//if they are matching the pages you want to include.

$page = "photo" . $id . ".php";
include($page);

//Now, if I assume the array holds the numbers of all the images
// you want to use, then here is a way to show the other pages.

if($id != $page[0]){
$page_links .= "<a href='" . $PHP_SELF . "?id=" . ($id-1) . "'>Previous</a>";
}


for($a=0;$a<count($page);$a++){

if($page[$a] == $id){

$page_links .= "&nbsp;" . $a . "&nbsp;";

} else {

$page_links .= "&nbsp;<a href='" . $PHP_SELF . "?id=" . $a . "'>" . $a . "</a>&nbsp;";

}
}

if($id != $page[count($page)]){
$page_links .= "<a href='" . $PHP_SELF . "?id=" . ($id+1) . "'>Next</a>";
}

?>


I think thats it. Or at least enough to help you figure out the logic behind it. Hope that helps