Issue with my Simple If statment

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
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Issue with my Simple If statment

Post by danjapro »

I have this simple piece of code.
I am using GET[] to retrieve the catID from URL, then match it and set a backgorund image, some backgorund images can change per catID variable. When selected or clicked on.

It is not working, it is producing the catId in ech $_Get[], but not matching it with $filename to set correct Image.

Ex. Browser catID may == 1, it code displays catid = 1, file name = deals_2.jpg.
This is worng. should be catid = 1 , filename = 1.

PLEASE HELP, missing something.

Code: Select all

<?php	
  $imgpath = '/shushmedeals/templates/shushme_deals/images/';
  //$bgimgID = $item->id;

  $bgimgID = isset($_GET['categoryId']);
  $bgimgID = $_GET['categoryId'];
  echo $bgimgID;
  //echo 'shoot her eman';
  //echo  $imgpath;

  if($bgimgID){
  	$bgimgID = 1;
	$filename = '/shushmedeals/templates/shushme_deals/images/bg_home.jpg';
  } 
  if($bgimgID = 2){
  	$filename = '/shushmedeals/templates/shushme_deals/images/shushme_bg_img1.jpg';
  }
  if($bgimgID = 3){
  	 $filename = '/shushmedeals/templates/shushme_deals/images/shushme_bg_img2.jpg';
  } else {
  	 $filename = '/shushmedeals/templates/shushme_deals/images/shushme_bg_img3.jpg';
  }			 			 				  
  if($filename){ 
  	
  	echo '<br>'.$filename; 
  	// SET COOKIE 
  	//setcookie('bgimg',  $filename);	
  	//echo $_COOKIE['bgimg'];
  	
  } else { 
  	echo "There is no Background for this category"; 
  	// DO NOT SET COOKIE 
  } 


  // Print an individual cookie
  echo $_COOKIE["location"];
  
  ?>
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Issue with my Simple If statment

Post by flying_circus »

Code: Select all

<?php
  # Define Variables
    $imgpath = '/shushmedeals/templates/shushme_deals/images/';
    $filename = NULL;
  
  # Check if GET Var Exists
    $bgimgID = isset($_GET['categoryId']) ? $_GET['categoryId'] : '';

  # Switch bgimgID
    switch($bgimgID) {
      case '1':
        $filename = "{$imgpath}bg_home.jpg";
        break;
      case '2':
        $filename = "{$imgpath}bg_home1.jpg";
        break;
      case '3':
        $filename = "{$imgpath}bg_home2.jpg";
        break;
      default:
        $filename = "{$imgpath}bg_home3.jpg";
        break;
    }
    
  # Display $filename
    if(is_null($filename))
      print "There is no Background for this category";
    else
      print "<br />{$filename}";
      
  # Print an individual cookie
    print $_COOKIE["location"];
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Issue with my Simple If statment

Post by califdon »

Your principal problem is that your PHP syntax is incorrect. In PHP, a single = operator assigns a value to a variable, it doesn't test whether a value is equal to some value, that requires a double == operator. So every one of your if statements is faulty. In addition, you should use a switch statement rather than a series of if statements, and there is no need to repeatedly spell out the identical values; that's what variables are for, as flying_circus showed you.
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: Issue with my Simple If statment

Post by danjapro »

flying_circus, you are the man!!!.
Thank you very much very on point, did the job and add some more code to make it render per cookie and timer.

califdon, appreciate the in-sight, wise words to practice. Yes, mr. miyaggi of coding. I will practice, my wax on and wax off.
Post Reply