SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Issue with my Simple If statment

  1. #1
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    vi
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue with my Simple If statment

    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:
    <?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"];
      
      ?>

  2. #2
    Community Advisor silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    1,642
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The "if" statement is clumsy way to do this.
    There are a few different ways to approach this problem.
    If your filenames are consistent (eg a catId of 1 is for bg_img2.jpg, catId of 2 is bg_img3.jpg, etc) you can simply concatenate the catId (after modifying it)
    Code:
    $filename = $imgpath . 'shushme_bg_img' . ($bgimgID + 1) . ".jpg";
    That single line replaces the entire 'if' block. But that will NOT accommodate your first case (the bg_home).


    The other way to do this is by replacing your 'if' statement with a 'switch' statement (also known as a 'case').
    Code:
    switch($bgimgID) {
      case 1 : $filename = $imgpath . 'bg_home.jpg';
           break;
      case 2 : $filename = $imgpath . 'shushme_bg_img1.jpg';
          break;
      case 3 : $filename = $imgpath . 'shushme_bg_img2.jpg';
          break;
      default : //this is the safety valve in case the value does not match any of these cases
         $filename = $imgpath . 'bg_home.jpg';
    }
    This is precisely the type of logic for which the SWITCH statement is intended.

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use an array to lookup the ID to the background image

    PHP Code:
    if( isset($_GET['categoryId']) ) {
        
    $imgpath '/shushmedeals/templates/shushme_deals/images/';
        
    $bgimgID = (int)$_GET['categoryId'];
        
    $bgs = array(
            
    => 'bg_home.jpg',
            
    => 'shushme_bg_img1.jpg',
            
    => 'shushme_bg_img2.jpg',
        );
        
        
    //Have image for ID
        
    if( isset($bgs[$bgImgID]) ) {
            
            
    $filename $imgpath $bgs[$bgImgId];
            
            
    //Set cookie
        
    }

    mikehealy.com.au
    diigital.com art, design . Latest WorkSaturday Morning

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One problem is with this line

    PHP Code:
    if($bgimgID 2){       $filename '/shushmedeals/templates/shushme_deals/images/shushme_bg_img1.jpg';   } 
    For comparison you should use a double equal "=="

    PHP Code:
    if($bgimgID == 2){       $filename '/shushmedeals/templates/shushme_deals/images/shushme_bg_img1.jpg';   } 

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    vi
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by SteveS1951 View Post
    One problem is with this line

    PHP Code:
    if($bgimgID 2){       $filename '/shushmedeals/templates/shushme_deals/images/shushme_bg_img1.jpg';   } 
    For comparison you should use a double equal "=="

    PHP Code:
    if($bgimgID == 2){       $filename '/shushmedeals/templates/shushme_deals/images/shushme_bg_img1.jpg';   } 
    Appreciate everyones feedback, you guys are genuis.

    I was able to come up with code as you posted using case switch method.
    And added more lines for settings of session variables.

    Amazing work fellas.

    Thank you all very much.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •