Jump to content

Strange Empty Array Behavior


xProteuSx

Recommended Posts

I am working with the Amazon API, and I am trying to display a default image if a product does not have an image associated with it.  The query is coming back as an array.  Typically, each product image array looks like this:

 

$d = SimpleXMLElement Object ( => http://ecx.images-amazon.com/images/I/51aUIul6XjL._SL160_.jpg [Height] => 160 [Width] => 112 )

 

The code goes like this:

 

if ($d=$E->MediumImage) 
            	{
                $iu=$d->URL;
                $ih=$d->Height;
                $iw=$d->Width;
                
                echo count($d);
                
                if (strlen($iu) > 0)
                	{echo "<center><a href='$url' target='_blank'><img src='images/amazon_noimage.jpg' width='175' height='175' border='0'></a></center>";}
                else
                	{echo "<center><a href='$url' target='_blank'><img src='$iu' width='$iw' height='$ih' border='0'></a></center>";}
            	}

 

However, images/amazon_noimage.jpg never shows up (even though it is linked correctly, as I've tested this link).  I have tried the following:

 

if (strlen($iu) > 0)

if (count($iu) > 0)

if (strlen($d->URL) > 0)

if (count($d->URL) > 0)

if (isset($d))

if (!isset($d))

etc ...

 

If I display the following, where there is no image, I get nothing displayed:

 

echo $iu;

print_r($iu);

echo $d->URL;

etc ...

 

However, if there is an image, I get a link, such as the following:

 

http://ecx.images-amazon.com/images/I/51c2BFpDN0L._SL160_.jpg

 

There seems to be NOTHING that I can do to trigger the 'ELSE' part of the if statement.  This is a total enigma to me ... any ideas??

 

 

 

Link to comment
Share on other sites

Got it!

 

This is what I was originally trying:

 

if ($d=$E->MediumImage) 
            	{
                $iu=$d->URL;
                $ih=$d->Height;
                $iw=$d->Width;
                
                if (!strlen($iu))
                	{echo "<center><a href='$url' target='_blank'><img src='images/amazon_noimage.jpg' width='175' height='175' border='0'></a></center><br />";}
                else
                	{echo "<center><a href='$url' target='_blank'><img src='$iu' width='$iw' height='$ih' border='0'></a></center><br />";}
                    
                $image = $iu;
            	}

 

What I should have been doing, and what works is:

 

if ($d=$E->MediumImage) 
            	{
                $iu=$d->URL;
                $ih=$d->Height;
                $iw=$d->Width;
                
                echo "<center><a href='$url' target='_blank'><img src='$iu' width='$iw' height='$ih' border='0'></a></center><br />";
            	}
            else
            	{echo "<center><a href='$url' target='_blank'><img src='images/amazon_noimage.jpg' width='175' height='175' border='0'></a></center><br />";}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.