Page 1 of 1

Efficient?

Posted: Tue Dec 26, 2006 2:14 pm
by spacebiscuit
Hi,

Seasonal greeting to one and all!

I have a few lines of code which I think could be a little more efficient:

Code: Select all

session_start();

if($_GET['size']=='small'){
    $_SESSION['size']='/small';
                          }

if($_GET['size']=='large'){
    unset($_SESSION['size']); 
                          }

if(isset($_SESSION['size'])){
     $size=$_SESSION['size'];
                            }
						 
else{
     $size='/';
     unset($_SESSION['size']); 
       }


It is some code I am using on an image gallery. An image is dispalyed with a 'back' and 'next' link to view the next or previous image. Easy enough but I also have the images in two different resolutions. The bigger sized images are in the default location '/' and the smaller ones are in a folder '/small'.

So if the user is viewing small images, when they click next they should view the subsequent image in the smaller format too hence the variable 'size'.

At present it does work but I am sure I have over complicated the code above.

Any suggestions would be appreciated.

Thanks,

Rob.

Posted: Tue Dec 26, 2006 2:38 pm
by RobertGonzalez
What about something like this?

Code: Select all

<?php
session_start();

// Do we have this session var yet?
if (!isset($_SESSION['size'])) {
    // Nope - default to large
    $size = '/';

    // If the user has chosen smaller...
    if(isset($_GET['size']) && $_GET['size'] == 'small') {
        $size .= 'small';
    }
    
    // Sessionize it
    $_SESSION['size'] = $size;
} else {
    // We have a sessioned size, use it
    $size = $_SESSION['size'];
}
?>

Posted: Tue Dec 26, 2006 4:32 pm
by feyd
I'd like "premature optimization" for $200 Alex.

:)