duanouyong4228 2011-06-30 22:27
浏览 35
已采纳

Echo不会显示从json_decode()中检索到的任何值

I have an extremely simple JSON object that looks like the following:

var data =
{
    "id" : 1
}

I then decode this in PHP:

$decoded_data = json_decode(stripslashes($_POST['data']));  //this works
$id = intval($decoded_data->id); //in my debugger this is equal to 1 as expected

I then proceed to pass the $id variable into a function that queries the database and returns a set of 'Sub Activities'

$sub_activities = alp_get_all_sub_activities($id);  //this function works as expected and returns the correct result set

Now that I have the Sub Activities for the designated $id, I attempt to access them using a loop:

foreach ($sub_activities as $activity) {
    echo __("<td><a id='" . $activity->id . "' href='' title='Activity'><div style=' border: 3px solid purple; width: 200px; height: 200px; overflow: scroll;'>" . $activity->name . "<br />" . $activity->id . "<br />" . $activity->description) . "</div></a></td>";
}

My problem is: echo displays nothing when $id is set to intval($decoded_data->id), but when I hardcode $id = 1 then everything works as expected and shows in my browser. I'm not quite sure how to approach this problem, because my debugger is telling me that when I set $id = intval($decoded_data->id); that $id is equal to 1. I can do arithmetic with this number and it seems like it behaves as any integer would, but for some reason echo and print() will not display anything.

If anyone has any insight I'd really appreciate your input.

  • 写回答

2条回答 默认 最新

  • dongmu5815 2011-07-21 17:39
    关注

    This turned out to be a problem with ajax in Wordpress with Buddypress installed. Buddypress was sending the request elsewhere for its own purposes. I just created my own javascript namespace object and rerouted the data. Here are tutorials that helped me figure this out:

    http://codex.wordpress.org/AJAX_in_Plugins

    http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/

    JSON now works perfectly. Thanks for the help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题