Efficient?

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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Efficient?

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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'];
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd like "premature optimization" for $200 Alex.

:)
Post Reply