Small if statement used with images

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
randall
Forum Newbie
Posts: 16
Joined: Fri May 08, 2009 9:01 pm

Small if statement used with images

Post by randall »

Is there some reason why this is if statement is not working?
Here is the url I am using http://dev/categories.php?catid=2
For some reason the image that displays is art.jpg no matter what # I use in the URL.

Code: Select all

<img src="images/categories/<?php
$cat = intval($_GET['catid']); 
   if ($cat = '1') {
   echo "art.jpg"; 
   } elseif ($cat = '2') {
   echo "art2.jpg";
   } elseif ($cat = '3') {
   echo "art3.jpg";
   } else {
   echo "na.jpg";
   }
?>"/>
Last edited by Benjamin on Mon May 18, 2009 9:52 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Small if statement used with images

Post by califdon »

Since you are forcing $cat to be an integer and are then comparing it to characters (using quotes), they will never be equal.
randall
Forum Newbie
Posts: 16
Joined: Fri May 08, 2009 9:01 pm

Re: Small if statement used with images

Post by randall »

worked it out.. using

Code: Select all

<img src="images/categories/<?php
$category = $_GET['catid']; 
if (!$category) { 
echo " "; 
}
elseif ( $category == 1 ) {
echo "art.jpg";
}
elseif ( $category == 2 ) {
echo "art2.jpg";
} 
else {
echo "$category";
} 
?>"/>
Thx!
Last edited by Benjamin on Mon May 18, 2009 9:52 am, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply