Page 1 of 1

Small if statement used with images

Posted: Sun May 17, 2009 3:21 pm
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";
   }
?>"/>

Re: Small if statement used with images

Posted: Sun May 17, 2009 3:25 pm
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.

Re: Small if statement used with images

Posted: Sun May 17, 2009 4:33 pm
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!