【问题标题】:Codeigniter error if "no result found" or no "input was entered"; Else works fine如果“未找到结果”或未输入“输入”,则出现 Codeigniter 错误;其他工作正常
【发布时间】:2016-01-05 16:18:16
【问题描述】:

我正处于学习阶段,正在尝试开发此搜索结果应用程序。 如果找到结果,一切正常,但如果没有输入或没有结果,它会显示这 2 个错误;

消息:未定义的偏移量:0

消息:试图获取非对象的属性

这是发生错误的地方 search_model.php

function get_city_id_by_input($keyword){
    $this->db->select('id');
    $this->db->from('vbc_city');
    $this->db->where('v_city_name', $keyword);
    $query = $this->db->get();
    $query_result = $query->result();
    $row = $query_result[0];
    return $row->id;
}

和控制器:

$keyword = $this->input->post('search[1]');
$city_id = $this->search_model->get_city_id_by_input($keyword);
$data['results'] = $this->search_model->get_search_results($city_id);
$this->load->view('search', $data);

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案1】:

    这样试试

    在模型中

    function get_city_id_by_input($keyword){
        $this->db->select('id');
        $this->db->from('vbc_city');
        $this->db->where('v_city_name', $keyword);
        $query = $this->db->get();
        $query_result = $query->result_array(); # Changed
        if (empty($query_result)) {
            return FALSE;
        }
        elseif (count($query_result) > 1) {
            return 0;
        }
        else{
            $rowId = $query_result[0]['id']; # Changed
            return $rowId; # Changed    
        }
    }
    

    在控制器中

    $keyword = $this->input->post('search[1]');
    if (empty($keyword)) {
        echo "Input is Empty";
    }
    else{
        $city_id = $this->search_model->get_city_id_by_input($keyword);
        $result = $this->search_model->get_search_results($city_id);
        if ($result == FALSE) {
            echo "No Data Found";
        }
        elseif ($result == 0) {
            echo "Multiple of records founds";
        }
        else{
            $data['results'] = $result
            $this->load->view('search', $data);
        }
    
    }
    

    【讨论】:

    • * 如果没有输入或没有结果*
    • 更像是事实^)
    • @splash58 也添加了另一个 ;)
    • @Abdulla,非常感谢...虽然我不必使用控制器...模型都做到了
    • 它仅通过您提供的模型代码起作用..可能是控制器代码甚至在之前就已经设置好了..
    猜你喜欢
    • 1970-01-01
    • 2021-02-12
    • 2013-06-08
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    相关资源
    最近更新 更多