数据表错误未定义索引Codeigniter

时间:2018-11-29 07:24:15

标签: sql ajax codeigniter datatables

我以前一直在使用数据表,但这是我第一次遇到此类错误。

我有错误:

Message: Undefined index: order
Message: Undefined index: start
Message: Undefined index: length

Message: Call to a member function result_array() on boolean

Line: 253

(我只是将错误简化)

我的Ajax代码:

var dataTable = $('#table-grid').DataTable({ // *** This is for Data Table
    "processing": true,
    "serverSide": true,
    columnDefs: [
        { orderable: false, targets: [6,5]},
        { class: "text-center", targets: [5,6]},
    ],
    "ajax":{
        url :base_url+"LandClient/view_properties", // json datasource
        type: "post",  // method  , by default get
        data: {'email': $('#email').val()},
        error: function(){  // error handling
            $(".table-grid-error").html("");
            $("#table-grid").append('<tbody class="table-grid-error"><tr><th colspan="5">No data found in the server</th></tr></tbody>');
            $("#table-grid_processing").css("display","none");
        }
    }
});

我的控制器:

public function view_properties(){
    $email = $this->session->userdata('login_email');
    generate_json($this->LandClientModel->view_properties($email));
}

我的模特:

public function view_properties($email){
    // storing  request (ie, get/post) global array to a variable  
    $requestData= $_REQUEST;


    $columns = array( 
        // datatable column index  => database column name for sorting
            0 => 'pr.date_created',
            1 => 'dc.tdn_no',
            2 => 'oct.orig_cert_no,tct.orig_cert_no',
            3 => 'tct_no',
            4 => 'owner_name',
            5 => 'pr.status',
        );

    // getting total number records without any search

    $sql = "SELECT *,oct.orig_cert_no as oct1, tct.orig_cert_no as oct2  FROM land_property pr 
            LEFT JOIN land_property_orig_title oct ON pr.prop_id = oct.prop_id
            LEFT JOIN land_property_transfer_title tct ON pr.prop_id = tct.prop_id
            LEFT JOIN land_property_declaration dc ON pr.prop_id = dc.prop_id
            WHERE pr.email = ? ";
    $params = array($email);
    $query = $this->db->query($sql, $params);
    $totalData = $query->num_rows();

    $totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.





    if( !empty($requestData['columns'][0]['search']['value']) ) //email address
    { 
        $sql.=" AND tdn_no LIKE '%". $this->db->escape_like_str(sanitize($requestData['columns'][0]['search']['value']))."%' ";
    }

    if( !empty($requestData['columns'][1]['search']['value']) ) //email address
    { 
        $sql.=" AND oct.orig_cert_no LIKE '%". $this->db->escape_like_str(sanitize($requestData['columns'][1]['search']['value']))."%' OR tct.orig_cert_no LIKE '%". $this->db->escape_like_str(sanitize($requestData['columns'][1]['search']['value']))."%' ";
    }


    if( !empty($requestData['columns'][2]['search']['value']) ) //email address
    { 
        $sql.=" AND owner_name LIKE '%". $this->db->escape_like_str(sanitize($requestData['columns'][2]['search']['value']))."%' ";
    }


    if( !empty($requestData['columns'][3]['search']['value']) ) //email address
    { 
        $sql.=" AND tct_no LIKE '%". $this->db->escape_like_str(sanitize($requestData['columns'][3]['search']['value']))."%' ";
    }



    $query = $this->db->query($sql, $params);
    $totalFiltered = $query->num_rows(); // when there is a search parameter then we have to modify total number filtered rows as per search result.

     $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";  // adding length


    $query = $this->db->query($sql, $params);
    $data = array();

    foreach( $query->result_array() as $row ) {  // preparing an array for table tbody
        $nestedData=array(); 
        // $nestedData[] = $row["date_created"];
            if($row['tdn_no']==''){
                $tdn = "<h1>-</h1>";
            } else {
                $tdn = $row['tdn_no'];
            }
            $nestedData[] = $tdn;
            if($row['oct1']!=""){
                $nestedData[] = $row["oct1"];
            } else {
                $nestedData[] = $row["oct2"];
            }
            $nestedData[] = $row["tct_no"]==""?"-":$row["tct_no"];
            $nestedData[] = $row["owner_name"];


            if($row["status"] == 4){
                $status = "<span class='badge badge-danger tbl-badge'>Hold</span>";
            }
            elseif($row["status"] == 1){
                    $status = "<span class='badge badge-success tbl-badge'>Assessed</span>";
            }
             elseif($row["status"] == 2){
                    $status = "<span class='badge badge-warning tbl-badge'>For Assessment</span>";
            }
            $nestedData[] = $status;

            $nestedData[] = $row["status"];




            $nestedData[] = '<button class="btn btn-primary btnView btnView-1 btn-cus" > View</button>  ';
            $data[] = $nestedData;
    }
    $json_data = array(
        "recordsTotal"    => intval( $totalData ),  // total number of records
        "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
        "data"            => $data,   // total data array
    );

    return $json_data;
}

顺便说一句,第253行是

 $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";  // adding length

2 个答案:

答案 0 :(得分:0)

在您显示的代码中唯一使用result_array的是这一行

foreach( $query->result_array() as $row ) 

对我来说,这意味着错误是“您正在尝试将布尔值$query用作具有成员函数的对象。”

看来这行

$query = $this->db->query($sql, $params);

false的值分配给$query。我们知道它是false,因为对于“读取”语句,query()将在成功时返回数据库结果对象,或者在查询失败时返回FALSE。 (注意:“成功”并不意味着有任何行,只是说事情没有崩溃。)

这只是说您的查询语句不起作用的一种漫长的方法。

基于“未定义的索引”错误,我最好的猜测是您正试图从$requestData获取不存在的项目。这些似乎是用于设置ORDER BY子句的子句; $requestData['order']$requestData['start']$requestData['length']

我认为您最好使用输入类方法post_get() documentation(或get_post())而不是复制$_REQUEST。我不清楚您要尝试访问的各种索引是什么,因此我无法提供有关如何使用get_post()的清晰示例。

问题的一部分可能是试图以一种旧的方式构建查询字符串-根据级联条件连接字符串。此用例正是Query Builder的用途。

答案 1 :(得分:0)

首先检查像这样使用isset函数设置或不设置的变量或kay

if(isset($requestData['order'][0]['column']) && isset($requestData['order'][0]['dir']) && isset($requestData['start']) && isset($requestData['length'])){
     $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";  // adding length
    }

在代码仓库中查看

public function view_properties($email){
    // storing  request (ie, get/post) global array to a variable  
    $requestData= $_REQUEST;

    $columns = array( 
        // datatable column index  => database column name for sorting
            0 => 'pr.date_created',
            1 => 'dc.tdn_no',
            2 => 'oct.orig_cert_no,tct.orig_cert_no',
            3 => 'tct_no',
            4 => 'owner_name',
            5 => 'pr.status',
        );

    // getting total number records without any search

    $sql = "SELECT *,oct.orig_cert_no as oct1, tct.orig_cert_no as oct2  FROM land_property pr 
            LEFT JOIN land_property_orig_title oct ON pr.prop_id = oct.prop_id
            LEFT JOIN land_property_transfer_title tct ON pr.prop_id = tct.prop_id
            LEFT JOIN land_property_declaration dc ON pr.prop_id = dc.prop_id
            WHERE pr.email = ? ";
    $params = array($email);
    $query = $this->db->query($sql, $params);
    $totalData = $query->num_rows();

    $totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.

    if( !empty($requestData['columns'][0]['search']['value']) ) //email address
    { 
        $sql.=" AND tdn_no LIKE '%". $this->db->escape_like_str(sanitize($requestData['columns'][0]['search']['value']))."%' ";
    }

    if( !empty($requestData['columns'][1]['search']['value']) ) //email address
    { 
        $sql.=" AND oct.orig_cert_no LIKE '%". $this->db->escape_like_str(sanitize($requestData['columns'][1]['search']['value']))."%' OR tct.orig_cert_no LIKE '%". $this->db->escape_like_str(sanitize($requestData['columns'][1]['search']['value']))."%' ";
    }

    if( !empty($requestData['columns'][2]['search']['value']) ) //email address
    { 
        $sql.=" AND owner_name LIKE '%". $this->db->escape_like_str(sanitize($requestData['columns'][2]['search']['value']))."%' ";
    }

    if( !empty($requestData['columns'][3]['search']['value']) ) //email address
    { 
        $sql.=" AND tct_no LIKE '%". $this->db->escape_like_str(sanitize($requestData['columns'][3]['search']['value']))."%' ";
    }

    $query = $this->db->query($sql, $params);
    $totalFiltered = $query->num_rows(); // when there is a search parameter then we have to modify total number filtered rows as per search result.

    if(isset($requestData['length'])){
     $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";  // adding length
    }

    $query = $this->db->query($sql, $params);
    $data = array();

    foreach( $query->result_array() as $row ) {  // preparing an array for table tbody
        $nestedData=array(); 
        // $nestedData[] = $row["date_created"];
            if($row['tdn_no']==''){
                $tdn = "<h1>-</h1>";
            } else {
                $tdn = $row['tdn_no'];
            }
            $nestedData[] = $tdn;
            if($row['oct1']!=""){
                $nestedData[] = $row["oct1"];
            } else {
                $nestedData[] = $row["oct2"];
            }
            $nestedData[] = $row["tct_no"]==""?"-":$row["tct_no"];
            $nestedData[] = $row["owner_name"];

            if($row["status"] == 4){
                $status = "<span class='badge badge-danger tbl-badge'>Hold</span>";
            }
            elseif($row["status"] == 1){
                    $status = "<span class='badge badge-success tbl-badge'>Assessed</span>";
            }
             elseif($row["status"] == 2){
                    $status = "<span class='badge badge-warning tbl-badge'>For Assessment</span>";
            }
            $nestedData[] = $status;

            $nestedData[] = $row["status"];

            $nestedData[] = '<button class="btn btn-primary btnView btnView-1 btn-cus" > View</button>  ';
            $data[] = $nestedData;
    }
    $json_data = array(
        "recordsTotal"    => intval( $totalData ),  // total number of records
        "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
        "data"            => $data,   // total data array
    );

    return $json_data;
}