简体   繁体   English

如何在JSON请求响应中添加回调?

[英]How to add callback to JSON request response?

I'm displaying images from JSON, since some lines doesn't have any image url attached: 我正在显示来自JSON的图像,因为某些行没有附加任何图像URL:

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 ? 一些图像显示为:null而不是URL如何添加回调将:null转换为/images/image.png以加载自定义图像?

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. 这是一个三元条件,就像一个紧凑的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. 因此,如果设置了$ offer ['mobile_app_icon_url'] //不为null,则将使用它,否则将使用[link_to_your_default_image]

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>";    
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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