• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Next and Back buttons.

I have a database as follows:

image id
image categoryID
image Filename
image Description


Image id is obviously unique, image category is relevant for a specific page, image name is the file name to source from the file system and image text is some descriptive text to go with the image.

NOW

How would I pull out all images of a certain category AND display one at a time with next and back buttons along with the text ?

Im pretty sure I can get the query right, I just need help with the next and back buttons,

thanks in advance
0
Eternal_Student
Asked:
Eternal_Student
  • 4
  • 2
1 Solution
 
RoonaanCommented:
You would just have:

mygallery.php?category=13123&imageid=1313 (or just mygallery.php?category=12323, for the first image)

Then in your script have
.. connect to db ..
$category = isset($_REQUEST['category']) ? intval($_REQUEST['category']) : 0;
$imageId  = isset($_REQUEST['imageid']) ? intval($_REQUEST['imageid']) : 0;

// SQL to get image with certain id
$imageSql1 = 'SELECT Filename, Description FROM image WHERE category='.$category.' AND id='.$imageId.' LIMIT 1';
// SQL to get first image in category
$imageSql2 = 'SELECT Filename, Description FROM image WHERE category='.$category.' ORDER BY id ASC LIMIT 1';

// Lets see if we find image with id
$result = mysql_query($imageSql1) or die('Query failed');
// If we don't find it, just call the first image
if(mysql_num_rows($result) < 1) {
  $result = mysql_query($imageSql2) or die('Query failed');
}
// If there is no first image and no image with id, then category is incorrect or empty
if(mysql_num_rows($result) != 1) {
  exit('Category not found, or no images in category');
}

// Fetch current images info.
$currentImage = mysql_fetch_assoc($result);
$imageId = intval($currentImage['id']);

// Retrieve previous image info
$previousSql = 'SELECT id FROM image WHERE category='.$category.' AND id < '.$imageId.' ORDER BY id DESC LIMIT 1';
$resultPrev = mysql_query($previousSql);
$prevImage = mysql_num_rows($resultPrev) == 1 ? mysql_fetch_assoc($resultPrev) : false;

// Retrieve next image info
$nextSql = 'SELECT id FROM image WHERE category='.$category.' AND id > '.$imageId.' ORDER BY id ASC LIMIT 1';
$resultNext = mysql_query($nextSql);
$nextImage = mysql_num_rows($resultNext) == 1 ? mysql_fetch_assoc($resultNext) : false;

// Link for previous image, or just plain text grayed out span.
if(is_array($prevImage)) {
  echo '<a href="'.$_SERVER['PHP_SELF'].'?category='.$category.'&imageid='.$prevImage['id'].'">Previous</a>';
} else {
  echo '<span style="color:#999;">Previous</span>';
}


// Link for next image, or just plain text grayed out span.
if(is_array($nextImage)) {
  echo '<a href="'.$_SERVER['PHP_SELF'].'?category='.$category.'&imageid='.$nextImage['id'].'">Next</a>';
} else {
  echo '<span style="color:#999;">Next</span>';
}

I hope this helps,

-r-
0
 
RoonaanCommented:
First sql's are missing the 'id' field itself in the SELECT part. Should be:

// SQL to get image with certain id
$imageSql1 = 'SELECT id, Filename, Description FROM image WHERE category='.$category.' AND id='.$imageId.' LIMIT 1';
// SQL to get first image in category
$imageSql2 = 'SELECT id, Filename, Description FROM image WHERE category='.$category.' ORDER BY id ASC LIMIT 1';

-r-
0
 
Eternal_StudentAuthor Commented:
Thanks Roonaan,

Could you kindly explain what these lines do:

            $category = isset($_REQUEST['category']) ? intval($_REQUEST['category']) : 0;
            $imageId  = isset($_REQUEST['imageid']) ? intval($_REQUEST['imageid']) : 0;

thanks.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RoonaanCommented:
It is the ternairy operator. It is short for an if/else statement:

if($someBooleanExpression == true) {
  $someVar = $valueWhenTrue;
} else {
  $someVar = $valueWhenFalse;
}

In short:

$someVar = ($someBooleanExpression == true) ? $valueWhenTrue : $valueWhenFalse;

-r-

0
 
Eternal_StudentAuthor Commented:
I see. One further question, how do I output the image and text ?
0
 
RoonaanCommented:
You can use the associative $currentImage array:

echo '<img src="'.$currentImage['FileName'].'" />';

echo '<p>'.$currentImage['Description'].'</p>';

-r-
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now