Page 1 of 1

How would you guys do this? Sorting through an array...

Posted: Fri Jul 25, 2003 9:52 pm
by JPlush76
I have users that can have images on my site. On their profile page you can click on a link that brings up a pop up with one of the user's images and two links. PREVIOUS and NEXT

I want them to be able to page through the user's images using the PREVIOUS and NEXT buttons.

Whats the best way to handle that?

I was thinking you'd have to create a session array with the user's img ids. Anyone do this before?

thanks all!

Posted: Fri Jul 25, 2003 10:20 pm
by Drachlen
You could like do something like:

Code: Select all

<?php
  $result = mysql_query("SELECT * FROM user_table WHERE id = '$id'");
while ($fetch = mysql_fetch_assoc($result)) {
echo "<img src=$fetch[image]>";
}
echo "<a href=?id=" . ($id-1) . ">Previous</a>-<a href=?id=" . ($id+1) . ">Next</a>";
?>
of course connecting to your database before that....

Posted: Sat Jul 26, 2003 11:17 am
by JPlush76
the problem with that is that they aren't in numeric order

for example someone might add images 4 months apart and the id's would be 34, 69, 343, 585, etc

so -1 would give me someone else's image.

Posted: Sat Jul 26, 2003 12:33 pm
by pootergeist
yup - I'd build an array of image_ids based on a query where the user=user_id

then run $_SESSION['name'] = serialize($the_array);

some basic isset logic should make sure the query only runs the first page view - then just unserialize to a base variable and determine next-previous to mimic the array key

$img_array = unserialize($_SESSION['name']);
echo '<img src="' .$img_array[($_GET['ref'])]. '" />';

determine if $_GET['ref'] is zero or count($img_array)-1 and output next/previous