Page 1 of 1

Issue with my Simple If statment

Posted: Mon Oct 17, 2011 12:45 pm
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"];
  
  ?>

Re: Issue with my Simple If statment

Posted: Mon Oct 17, 2011 1:49 pm
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"];
?>

Re: Issue with my Simple If statment

Posted: Mon Oct 17, 2011 2:49 pm
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.

Re: Issue with my Simple If statment

Posted: Tue Oct 18, 2011 12:35 am
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.