doutan2456 2018-11-02 15:33
浏览 40

CodeIgniter - 没有以JSON格式从数据库中提取对象

I am trying to get customers data from a database in JSON format but its not pulling the object.

This is my Model method for grabbing the customer:

function getCustomers($per_page=0,$page=0, $key=0,$company="",$type='billing',$order_by="id",$order="desc"){
    if(!empty($per_page) && !empty($page) ){
        $start=0;
        if($page>1)
            $start=(($page-1)*$per_page);
        $this->db->limit($per_page, $start);
    }
    if (!empty($company)) {
        $this->db->like('company', $company, 'both');
    }
    if (!empty($type)) {
        $this->db->where('type', $type);
    }

    $this->db->where('is_active', 1);
    $this->db->order_by($order_by,$order);
    $query = $this->db->get("customers");
    $records=$query->result_array();
    if(empty($key)){
        return $records;
    }
    $newArray = array();
    foreach ($records as $record) {
        $id = $record['id'];
        $newArray[$id] = $record;
        if (!empty($record['lane_lists'])) {
            $newArray[$id]['unserialized_lanes'] = unserialize($record['lane_lists']);
        } else {
            $newArray[$id]['unserialized_lanes'] = array();
        }
        if (!empty($record['comment'])) {
            $newArray[$id]['unserialized_comment'] = unserialize($record['comment']);
        } else {
            $newArray[$id]['unserialized_comment'] = array();
        }
        if (!empty($record['created_at'])) {
            $newArray[$id]['since'] = date('m/d/Y', $record['created_at']);
        } else {
            $newArray[$id]['since'] = "";
        }

    }

    return $newArray;
}

Here is my count function in model

    function getCustomersCount($company="", $type='billing'){
    if (!empty($company)) {
        $this->db->like('company', $company, 'both');
    }
    if (!empty($type)) {
        $this->db->where('type', $type);
    }

    $this->db->select('count(*) as count');
    $query = $this->db->get("customers");
    return $query->row_array();
}

This is my Controller to display the customers information:

    public function index($type='billing', $page=1){

    $company="";
    if(!empty($_GET['company']))
        $company=$_GET['company'];
    $per_page=$this->config->item('per_page_limit');
    $total_rows = $this->admin_model->getCustomersCount( $company, $type);
    $total_count=$total_rows['count'];
    if($total_count<=($per_page*($page-1)) && $total_count > 0){
        $currentPage=($total_count%$per_page)==0?($total_count/$per_page):ceil($total_count/$per_page);
        redirect(base_url().'admin/customers/index/'. $type .'/'.$currentPage);
    }
    $links=paginationLink($this,'admin/customers/index/' . $type . '/', $total_count,$per_page);
    $records = $this->admin_model->getCustomers($per_page, $page,0,$company, $type,"company");


    if ($type == 'billing') {
        $submenu = 'bill_to_customers';
        $pageTitle = 'Bill To Customers';
    } else if ($type == 'shed') {
        $submenu = 'pickup_drop_customers';
        $pageTitle = 'Pickup Drop Customers';
    } else if ($type == 'shipper') {
        $submenu = 'shipper_customers';
        $pageTitle = 'Shipper Customers';
    } else {
        $submenu = '';
        $pageTitle = 'Customers';
    }
    $this->data=array('page_title'=>$pageTitle,'records'=>$records,'main_menu'=>'customers','submenu'=>$submenu,'links'=>$links, 'type' => $type);
    layout($this,'customers/list');
}

There is data in my customers table but when I use console.log() to view it, I cannot see the object.

<script type="text/javascript">
var app = new Vue({
    el: "#customerList",
    data: {
        loginUserType: <?php echo $this->session->userdata('logged_in')['type']; ?>,
        customers: <?php if(!empty($records)) { echo json_encode($records); } else {echo '{}';} ?>,
        customerSelected: {},
        customerType: '<?php if (!empty($type)) echo $type; ?>',


        links:  <?php if(!empty($links)) echo json_encode($links); else echo '{}'; ?>,
        states: <?php echo json_encode($this->config->item('us_states')); ?>,

    },
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大