【问题标题】:how to use json_encode for my output?如何为我的输出使用 json_encode?
【发布时间】:2017-07-19 21:43:18
【问题描述】:

我想对两个项目进行 json_encode。其中一个来自我在数据库中的表,另一个是从函数返回的值。 我想要这个输出:

{"status":-1,"message":"uuuuuuuuuuuuu"}

但我有这个:

{"status":-1,"message":{"Message":"uuuuuuuuuuuuu"}}

我的模特:

public function show_message($id)
{
    $string = "select Message from tbl_message where tbl_message.MID=$id ";

    $msg = $this->db->query($string)->row();
    $t = $msg;
    return  $t;
}

我的控制器:

$array = array("status"=>$temp,"message"=>$res);
$output = json_encode($array);
die($output);

【问题讨论】:

  • $array = array("status"=>$temp,"message"=>$res['Message']);$array = array("status"=>$temp,"message"=>$res->Message);

标签: php json codeigniter


【解决方案1】:

你的问题不清楚

你可以试试

public function show_message($id)
{
    $this->db->select('status, message');
    $this->db->from('tbl_message');
    $this->db->where('MID', $id);
    $query = $this->db->get();

    return $query->row_array();
}

Json 控制器函数

public function example() {
    $id = '1';

    $message = $this->model_name->show_message($id);

    echo json_encode($message);
}

查看

$( document ).ready(function() {
   $(".load").click(function(e){
      $.ajax({
        url: "<?php echo base_url("controller/example");?>",
        type: "get",
        dataType: 'JSON',
        success: function(json){
            alert(json[message]);
            console.log(json);
        }
      });
   });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    相关资源
    最近更新 更多