xProteuSx Posted December 29, 2011 Share Posted December 29, 2011 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?? Quote Link to comment https://forums.phpfreaks.com/topic/254008-strange-empty-array-behavior/ Share on other sites More sharing options...
silkfire Posted December 29, 2011 Share Posted December 29, 2011 Your logic is inverted. If strlen is more than 0 then you show no_image, invert it mate. It should be: if (!strlen($iu)) Quote Link to comment https://forums.phpfreaks.com/topic/254008-strange-empty-array-behavior/#findComment-1302140 Share on other sites More sharing options...
xProteuSx Posted December 29, 2011 Author Share Posted December 29, 2011 Yeah, also doesn't work. I'm really baffled. Quote Link to comment https://forums.phpfreaks.com/topic/254008-strange-empty-array-behavior/#findComment-1302143 Share on other sites More sharing options...
silkfire Posted December 29, 2011 Share Posted December 29, 2011 Try var_dump(strlen($iu)) Quote Link to comment https://forums.phpfreaks.com/topic/254008-strange-empty-array-behavior/#findComment-1302144 Share on other sites More sharing options...
xProteuSx Posted December 29, 2011 Author Share Posted December 29, 2011 Returns nothing if $iu is empty. Doesn't even return a 0. Quote Link to comment https://forums.phpfreaks.com/topic/254008-strange-empty-array-behavior/#findComment-1302146 Share on other sites More sharing options...
xProteuSx Posted December 29, 2011 Author Share Posted December 29, 2011 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 />";} Quote Link to comment https://forums.phpfreaks.com/topic/254008-strange-empty-array-behavior/#findComment-1302147 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.