【问题标题】:Data always show [object object] why?数据总是显示[object object] 为什么?
【发布时间】:2017-03-08 03:43:17
【问题描述】:

我的查询有问题。我在这个实践中使用数据表和代码点火器。 我想统计数据,并按postedby排序,但我统计的数据总是显示[object object]。

public function ajax_list_peneliti_perpostingan()
{

    $list = $this->post->get_datatables_peneliti_perpostingan();
    $data = array();
    $no = $_POST['start'];
    foreach ($list as $post) {
        $no++;

        $query = $this->db->query('SELECT count(postedby) FROM peneliti where postedby = "'.$post->postedby.'"'); 
        $row_count = $this->post->count_postedby($post->postedby); 

        $row = array();
        $row[] = $post->postedby;
        $row[] = $row_count['0'];

        $data[] = $row;
    }

    $output = array(
        "draw" => $_POST['draw'],
        "recordsTotal" => $this->post->count_all_peneliti_perpostingan(),
        "recordsFiltered" => $this->post->count_filtered_peneliti_perpostingan(),
        "data" => $data,
    );
    //output to json format
    echo json_encode($output);
}

是我的控制台结果

{"draw":"2","recordsTotal":481,"recordsFiltered":481,"data":[["administrator",{"postedby":"administrator","Total":"1"}],["fadil",{"postedby":"fadil","Total":"25"}],["",{"postedby":"","Total":"1"}],["tilan",{"postedby":"tilan","Total":"160"}],["Editor",{"postedby":"Editor","Total":"2"}],["budi",{"postedby":"budi","Total":"1"}],["admin",{"postedby":"admin","Total":"291"}]]}

【问题讨论】:

  • 您是在使用alert() 还是将某些内容与对象连接起来?使用console.log(obj)进行调试。
  • 如果您尝试在 HTML 元素/警报消息上输出 ACTUAL 结果,则会出现实际结果。如果您尝试调试结果,则应提取对象:alert(result.draw)
  • 也向我们展示脚本

标签: javascript sql codeigniter


【解决方案1】:

我认为,问题是

$row = array();
        $row[] = $post->postedby;
        $row[] = $row_count['0'];

        $data[] = $row;

你可以改成

 $data[] = array(
                 'postedby'=>$post->postedby,
                 'total'   =>count()
                    );

【讨论】:

    猜你喜欢
    • 2014-10-24
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 2020-04-08
    • 2016-06-16
    • 2021-07-11
    • 1970-01-01
    相关资源
    最近更新 更多