PHP code advice please

Author
Discussion

philthy

Original Poster:

4,689 posts

241 months

Monday 6th August 2007
quotequote all
I'm setting up an image archive. I found a script that displays it nicely, and have been tinkering. Originally, it would only go forward or backward 1 pic at a time. I've added a day button, and a week button. If you click the forward 1 week button, it takes you to the latest picture (only got a couple of days pics so far). If you click the back 1 week button, it tries to get to an image with a negative reference ie, you're looking at image 20, and it wants to go back to image -148 (24 pics a day, 7 days = -168)
Here's the script:
$number_pics = count ($pic_info);
if (($currentPic > $number_pics)||($currentPic == $number_pics)||!$currentPic)
$currentPic = '0';
$item = preg_split('/;/', rtrim($pic_info[$currentPic]), 2);
$last = $number_pics - 1;
$last24 = $number_pics - 24;
$lastweek = $number_pics - 168;
$next = $currentPic + 1;
$next24 = $currentPic + 24;
$nextweek = $currentPic + 168;
$next_item = @preg_split('/;/', rtrim($pic_info[$next]), 2);
if ($currentPic > 0 ) $back = $currentPic - 1;
else $currentPic = "0";


I'm guessing using if or else will work, but unsure of the syntax.

Thanks in advance.
Phil

LivinLaVidaLotus

1,626 posts

202 months

Monday 6th August 2007
quotequote all
Could do with a lot more tidying up to be honest, but here you go:

$number_pics = count ($pic_info);
if (($currentPic > $number_pics)||($currentPic == $number_pics)||!$currentPic)
$currentPic = '0';
$item = preg_split('/;/', rtrim($pic_info[$currentPic]), 2);
$last = $number_pics > 1 ? $number_pics - 1 : 0;
$last24 = $number_pics > 24 ? $number_pics - 24 : 0;
$lastweek = $number_pics > 168 ? $number_pics - 168 : 0;
$next = $number_pics >= $currentPic + 1 ? $currentPic + 1 : $currentPic;
$next24 = $number_pics >= $currentPic + 24 ? $currentPic + 24 : $currentPic;
$nextweel = $number_pics >= $currentPic + 168 ? $currentPic + 168 : $currentPic;
$next_item = @preg_split('/;/', rtrim($pic_info[$next]), 2);
$back = $currentPic > 0 ? $currentPic - 1 : 0;


Edited by LivinLaVidaLotus on Monday 6th August 20:39

philthy

Original Poster:

4,689 posts

241 months

Thursday 9th August 2007
quotequote all
Thanks for that LLVL. I've been away for a couple of days, sorry I couldn't get back sooner. Going to go and have another tinker....

Edit to add:

Works a treat.
Thanks very much smile


Edited by philthy on Thursday 9th August 16:27