2

Please help me to use multiple conditions in Codeigniter 4?

I have used the following codes but it doesn't work, threw an error, with one like its working fine.

$users = $model->like('title', $this->request->getVar('search'))->like('name', $this->request->getVar('search'))->get();
 $db  = \Config\Database::connect();
 $user = $db->table('staff');
 $user = $db->like('title', $this->request->getVar('search'));
 $user = $db->or_like('name', $this->request->getVar('search'));
 $query = $user->get();

2 Answers2

4

Your second code is using or_like but in Codeigniter 4 you should use orLike: https://codeigniter4.github.io/userguide/database/query_builder.html

I don't know what type of error it throws (you should have posted it) and which of the two codes is the correct, but your code shoul look like this:

$db = \Config\Database::connect();
$builder = $db->table('staff');
$users = $builder->like('title', $this->request->getVar('search'))->orLike('name', $this->request->getVar('search'))->get();
shaggy
  • 1,708
  • 2
  • 15
  • 17
0

This my own approach.

namespace App\Models;

use CodeIgniter\Model;

class SearchData extends Model
{
    protected $table = 'Staff';
    
    /**
     * An array of search result
     * 
     * @var $Keyword string
     *
     * @return array
     */
public function FindStaffMember($Keyword){
$ResponseData="No Record Found!";
//::You could past third parameter
//$this->like('title', 'match', 'before'); // Produces: WHERE `title` LIKE '%match' ESCAPE '!'
//$this->like('title', 'match', 'after');  // Produces: WHERE `title` LIKE 'match%' ESCAPE '!'
//$this->like('title', 'match', 'both');   // Produces: WHERE `title` LIKE '%match%' ESCAPE '!'
        $this->like("Staff_Name", $Keyword);
        $this->orLike("Staff_Id", $Keyword);
        $this->limit(10);
        $this->orderBy("Staff_Name", "ASC");
        //:::::
        $query = $this->get();

//$RecordCounted=count($query->getResultArray());   //::: 
    
    if($query->getResultArray()){
        
        $ResponseData=$query->getResultArray();
    }
    
    return $ResponseData;
}


}
ShapCyber
  • 3,382
  • 2
  • 21
  • 27