If statements for Image Selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mgleason
    New Member
    • Feb 2008
    • 2

    If statements for Image Selection

    Hi...
    Fairly new to PHP and trying to work with some images...

    I have a page where I need to display a different header image on based on a business type that is coming from MySQL... There are seven total options... is there an easy if statement that can handle this in PHP?
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Originally posted by mgleason
    Hi...
    Fairly new to PHP and trying to work with some images...

    I have a page where I need to display a different header image on based on a business type that is coming from MySQL... There are seven total options... is there an easy if statement that can handle this in PHP?
    Create an array with each business type as keys and corresponding image columns (or rows) as their values.
    [php]query = array("business 1"=>"b1image"," business2"=>"b2 image");
    //other code
    $sql = mysql_query("SE LECT `".query['businessX']."` FROM `table`");[/php]

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Originally posted by mgleason
      Hi...
      Fairly new to PHP and trying to work with some images...

      I have a page where I need to display a different header image on based on a business type that is coming from MySQL... There are seven total options... is there an easy if statement that can handle this in PHP?
      Since I can see no code something like the switch method:
      [php]
      $business_type= ... whatever comes from the database
      switch ($business_type ) {
      case 'a' : $img = 'type1.png';
      break;
      case 'b' : $img = 'type2.png';
      break;
      ............... .............
      ............... .............
      case 'g' : $img = 'type7.png';
      break;
      default : $img = 'noimage.png';
      break;
      }
      echo "<img src='$img' border='0' />"
      --- etc.
      [/php]
      Another method is to store it in an associative array and get the image name from there:
      [php]
      $array=array('a '=>'type1.jpg', 'b'=>'type2.jpg ', ......... 'g'=>'type7.jpg ');
      if (isset($array[$business_type]))
      $img=$array[$business_type];
      else
      $img='noimage.p ng';
      echo "<img src='$img' border='0' />"
      ..... etc ....
      [/php]
      Ronald

      Comment

      • mgleason
        New Member
        • Feb 2008
        • 2

        #4
        Maybe the code will help...

        Basically... this was an afterthought. I have a template built that in the body portion is pulling in a "more info" type section for a business listing. Then I realized that in the top of the template i need the main pic to correspond with the business type that's pulling from the database.

        The image section is just normal html:
        <td align="left" valign="top"><!-- InstanceBeginEd itable name="headerpic " --><img src="images/mbw_int_main.jp g" width="821" height="155"><!-- InstanceEndEdit able --></td>

        My main select and query statement are down in the body.

        The business types are golf, accommodations, shopping, nightlife, etc... these are the "business_t ype" pulling from the database.

        The "case a" etc seems the easiest to me, but forgive my ignorance...

        Would I place the "case" inside the <td> where the image is now?

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Assuming varaible $img holds the image path/name, you can write the image in your statement via PHP, as follows:
          [html]
          <td align="left" valign="top"><!-- InstanceBeginEd itable name="headerpic " --><img src="<?php echo $img ?>" width="821" height="155"><!-- InstanceEndEdit able --></td>
          [/html]
          Ronald

          Comment

          Working...