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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

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

Post 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!
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post 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....
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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
Post Reply