如何使用json_encode输出?

时间:2017-02-28 19:10:15

标签: php json codeigniter

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

(?:\S+\s+){,3}  # match a word, followed by space(s). Up to 3 times.
\b[aA]s         # assert word boundary and match "as"
(?:\s+\S+)+?    # match any number of words, but as few as possible
\s+             # followed by space(s)
as\b            # and another "as"
(?:\s+\S+){,3}  # match up to 3 more words

但我有这个:

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

1 个答案:

答案 0 :(得分:0)

你的问题不清楚

你可以试试像

这样的东西
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);
        }
      });
   });
});