CodeIgniter-并非以JSON格式从数据库中提取对象

时间:2018-11-02 15:33:37

标签: php json codeigniter

我正在尝试从JSON格式的数据库中获取客户数据,但未提取对象。

这是我吸引客户的Model方法:

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

这是模型中的计数函数

    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();
}

这是我的控制器,用于显示客户信息:

    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');
}

客户表中有数据,但是当我使用console.log()查看数据时,看不到该对象。

<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 个答案:

没有答案