Home > Software design >  Condition Using Controller in Code Igniter
Condition Using Controller in Code Igniter

Time:12-12

I'm trying to create a function, it exists and does not exist in the search feature. Condition does not exist, will display Error 404. Condition, exists. Then it will display from search. But in the code I wrote, it only shows Error 404.

This my controller:

function search(){
    $keyword=str_replace("'", "", $this->input->post('xfilter',TRUE));
    $x['data']=$this->model_album->search_album($keyword);
    if(empty($x)  &&  empty($keyword)){
        $this->load->view('view_contents',$x);
    }
    else if (!empty($x)  &&  !empty($keyword)){
        $this->load->view('view_error_404');
    }
    
}

I've tried from this source, but it doesn't work. Can you help me?

CodePudding user response:

you need to mention search function in routes.php file like this:-

$routes['search'] ='your_controller_name/search';

CodePudding user response:

without knowing your function model_album->search_album($keyword) and assuming it returns a string or array, you don't need to worry about checking for the keyword, since without a keyword the function should return false or empty.

anyway you mixed up your if/else conditions, since you are checking for not empty results to return a 404, instead of empty result returning the 404.

this should do it:

if(empty($x)){
    $this->load->view('view_error_404');
}
else {
    $this->load->view('view_contents',$x);
}
  • Related