Problem with PHP code

Discussion in 'PHP' started by lv211, Jan 10, 2008.

  1. #1
    I'm trying to implement this code I have, but the if else statement does not seem to be working. I want to use a default image if there is no image returned in the $photo array.

    
    $photo = $link->getGbaseAttribute("image_link");
    
    foreach ($photo as $image) {
         if ($image->text){
              echo $image->text;
         } 
         else {
         echo "basic.jpg";
         }
    }
    Code (markup):
     
    lv211, Jan 10, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Echo $image->text and see if it contains the expected content.
     
    nico_swd, Jan 10, 2008 IP
    lv211 likes this.
  3. lv211

    lv211 Peon

    Messages:
    168
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sometimes it does and sometimes it doesn't.

    That script is in a foreach loop. Some of the entries have text in the image and sometimes the data is missing.

    I wanted to write a loop that would check to see if it existed then printed the default image if it did not.
     
    lv211, Jan 10, 2008 IP
  4. ausome

    ausome Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try this

    $photo = $link->getGbaseAttribute("image_link");
    
    foreach ($photo as $image) {
         if (strlen($image->text)>0){
              echo $image->text;
         } 
         else {
         echo "basic.jpg";
         }
    }
    PHP:
    If you are never getting your basic.jpg, thats because $image->text is
    always returning true.
    Checking the actual String Length avoids confusion.

    Let me know if that does the trick for you.

    Cheers
    Tim
     
    ausome, Jan 11, 2008 IP
  5. banias

    banias Peon

    Messages:
    4
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    allow only gif and jpg, for example..


    
    $photo = $link->getGbaseAttribute("image_link");
    
    foreach ($photo as $image) {
         if (substr($image->text,-4) == ".gif" || substr($image->text,-4) == ".jpg"){
              echo $image->text; 
         }else {
              echo "basic.jpg";
         }
    }
    
    PHP:
     
    banias, Jan 11, 2008 IP
  6. lv211

    lv211 Peon

    Messages:
    168
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Tim, Banias,

    That code isn't working for me. The data still doesn't show up.

    Thanks anyway.
     
    lv211, Jan 11, 2008 IP
  7. Gordaen

    Gordaen Peon

    Messages:
    277
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I assume the first line always returns an array even on error. If not, you would want to check it before throwing $photo into a foreach.

    $photo = $link->getGbaseAttribute("image_link");
    
    foreach ($photo as $image) {
         if (!empty($image->text)){
              echo $image->text;
         } 
         else {
         echo "basic.jpg";
         }
    }
    PHP:
     
    Gordaen, Jan 11, 2008 IP
    lv211 likes this.
  8. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    always initiate a clean array if you are expecting one

    
    $photo = array();
    $photo = $link->getGbaseAttribute("image_link");
    foreach ($photo as $image) 
    {
         if (!empty($image->text)){
              echo $image->text;
         }
          else {
         echo "basic.jpg";
         }
    }
    
    Code (markup):
    or you can do

    
    $photo = array();
    $photo = $link->getGbaseAttribute("image_link");
    if(count($photo)>0)
    {
    	foreach ($photo as $image) 
    	{
    	     if (!empty($image->text)){
    	          echo $image->text;
    	     }
    	     else {
    	        echo "basic.jpg";
    	     }
    	}
    }
    else
    {
        echo "basic.jpg";
    }
    
    Code (markup):
     
    lfhost, Jan 11, 2008 IP
    lv211 likes this.
  9. lv211

    lv211 Peon

    Messages:
    168
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I kinda figured it out.

    I used the code that Gordaen and lfhost posted. Instead of just printing the variable $image->text I had it print the whole image source.

    I couldn't get it to work by printing the basic.jpg with the if loop, but this works for my own purposes.

    Thanks guys.
     
    lv211, Jan 12, 2008 IP