简体   繁体   中英

How to add callback to JSON request response?

I'm displaying images from JSON, since some lines doesn't have any image url attached:

echo "<p><img src='".$offer['mobile_app_icon_url']."'></p>";

Some images appear as :null instead of URL How do i add a callback to convert :null into /images/image.png to load custom image instead ?

Response example:

"mobile_app_icon_url":null,"
"mobile_app_icon_url":"https://lh3.googleusercontent.com//UB5a8qPFoYonW8BT_zJiwtTEZVoVuWFwEzo4bvj0NrrKg3SCSdzIaBCmhDNI8M1lOq8=w100",

I answered before you update your question, maybe now this is not appropriate

You could do something like this :

echo "<p><img src='".$offer['mobile_app_icon_url'] ? $offer['mobile_app_icon_url'] : [link_to_your_default_image]."'></p>";

It's a ternary condition it's like a compact if/else. So if $offer['mobile_app_icon_url'] is set // not null , it will be used, or else [link_to_your_default_image] will be used.

Here is a documentation on the ternary condition

Solved it by this:

$x = (array)$offer['mobile_app_icon_url'];
if (empty($x)){
echo "<p><img src='images/icon.png'></p>";  
            } else {
            echo "<p><img src='".$offer['mobile_app_icon_url']."'></p>";    
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM