Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Nikolaos Papathanassopoulos
Nikolaos Papathanassopoulos
10,322 Points

Help: A fast way to display a dynamic image-src with php?

Hey Guys, im working on a project for a friend.

I got a logo (home.svg) which is black & white and 5 colored versions (news.svg / about.svg / media.svg / projects.svg / contact.svg ) and i want to display the correct file in relation to the pagetitle ( which is a php variable $pageTitle).

i managed to do this with following code:

<?php 
if($pageTitle == "news"){$logo = "img/logo/news";}
elseif($pageTitle == "about"){$logo = "img/logo/about";}
elseif($pageTitle == "media"){$logo = "img/logo/media";}
elseif($pageTitle == "gallery"){$logo = "img/logo/media";}
elseif($pageTitle == "projects"){$logo = "img/logo/projects";}
elseif($pageTitle == "lektron"){$logo = "img/logo/projects";}
elseif($pageTitle == "loops"){$logo = "img/logo/projects";}
elseif($pageTitle == "studio"){$logo = "img/logo/projects";}
elseif($pageTitle == "jazz"){$logo = "img/logo/projects";}
elseif($pageTitle == "contact"){$logo = "img/logo/contact";}
elseif($pageTitle == "startseite"){$logo = "img/logo/home";}
elseif($pageTitle == "impressum"){$logo = "img/logo/home";}
else{$logo = "img/logo/home";}
?>

It is working the way i want it to, BUT the server request is drastically reducing the pagespeed. you can see the site loading all content and 10s later the logo assigned to it.

Attempting to keep the markup DRY and still have a valid and fast solution how would you solve this problem?

Best Regards, ~Nikos.

2 Answers

Hello Nikos,

You might want to try using the switch statement instead of elseif.

From php.net: In a switch statement, the condition is evaluated only once and the result is compared to each case statement. In an elseif statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a switch may be faster.

Hope it helps

Nikolaos Papathanassopoulos
Nikolaos Papathanassopoulos
10,322 Points

thanks Tuguldur, changed it to switch... but its still very slow (4-6 seconds loading time)...

Can anyone recommend another solution to implement different image sources depending on a variable (in this case my $pageTitle)?

to make it clear where the $logo variable is used:

<object data="<?php echo BASE_URL . $logo; ?>.svg" 
            type="image/svg+xml" class="chart">
<a href="<?php echo BASE_URL . $logo; ?>.svg">
<!--[if lte IE 8]-->
<img src="<?php echo BASE_URL . $logo; ?>.png" alt="Logo" />
<!--[endif]-->
</a>
</object>

~ Nikos.