Searching for EXACT word in Database using Code Igniter - php

I am filtering some data, I have a field called "faculty" and the options are "Professor, Associate Professor, Research Professor" ... so on. My filtering works for every other field but for this specific case I am having trouble because the professor word appears in all the data and as a result, I get all the data that matches the word "professor" so it is not filtering anything. How can I make it to search only for the specific word 'Professor' and avoid getting all the others (research professor, associate professor...)?
// My code
function get_search_filters($limit, $start, $search_faculty)
{
$this->db->order_by('lname', 'asc'); //order records by last name
$this->db->limit($limit, $start);
/* search keyword in all columns*/
$this->db->like('faculty', $search_faculty);
$query = $this->db->get('expertise');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}

$this->db->order_by('lname', 'asc'); //order records by last name
$this->db->limit($limit, $start);
$this->db->where('faculty', $search_faculty);
$query = $this->db->get('expertise');
https://www.codeigniter.com/userguide3/database/query_builder.html#looking-for-specific-data

Related

Complicated condition of getting from database

I want get line_id from my lines_stations table:
This is timetable search so I have from form station_from_id and station_to_id.
My search function is here:
public function search($station_from_id, $station_to_id) {
$stations = array($station_from_id, $station_to_id);
$this->db->from('lines_stations');
$this->db->where_in('station_id', $stations);
$q = $this->db->get();
$lines_stations = $q->result();
return $q->result();
}
This return me all row from lines_stations where station_id is station_from_id or station_to_id but I need get only these where order is correctly - for example:
station_from_id = 2
station_to_id = 1
I should get only this:
I haven't idea how to do this.
I'm using CodeIgniter 3.

Facing problem at codeigniter multiple word search

I am creating a CodeIgniter search for my bookshop project. It is okay with a single word search but I am facing the problem when I search with multiple words. How I can fix this problem.
MY controller
public function search()
{
/*=== LOAD DYNAMIC CATAGORY ===*/
$this->load->model('admin_model');
$view['category'] = $this->admin_model->get_category();
/*==============================*/
$this->form_validation->set_rules('search_book', "Search",'required');
if($this->form_validation->run() == FALSE)
{
#...Redirected same page after action
redirect($_SERVER['HTTP_REFERER']);
}
else
{
$query = $this->input->post('search_book');
$this->load->model('user_model');
$view['books'] = $this->user_model->search($query);
$view['user_view'] = "users/search_books";
$this->load->view('layouts/user_layout', $view);
}
}
MY Model
public function search($query)
{
$this->db->order_by('id', 'DESC');
$this->db->from('books');
$this->db->like('book_name', $query);
$this->db->where('status', 1);
$q = $this->db->get();
return $q->result();
}
Like if I write PHP in my search box it brings my all books which title have the word PHP. It is okay for me.
but when I write PHP BOOKS in my search box it shows me no book found.
I want it to show the result if any word is matched.
You can replace this
$this->db->like('book_name', $query);
with condition as
$str = str_replace(" ","|", $query);
$this->db->where("book_name rlike '$str'");
You can get more details about rlike here.
Here is a better full text search help provided for your reference.

How to get single record if the multiple records are on single ID?

In this image, 5 record are with id 80, but when i fetch them they all are coming but i want to show just one record only.
My rest code is below here
$data['query7'] = $this->ORB_Model->get_skilldash();
public function get_skilldash()
{
$this->load->database('orb');
//$this->db->distinct('master_id');
$query = $this->db->get_where('skills_tb', array('master_id' => $this->session->userdata('master_id')));
return $query->result();
}
Use $query->row() instead of $query->result();
public function get_skilldash()
{
$this->load->database('orb');
$master_id = $this->session->userdata('master_id');
$query = $this->db->get_where('skills_tb', array('master_id' =>$master_id));
return $query->row();
}
If you want a specific row returned you can submit the row number as a digit in the first parameter:
$row = $query->row(3);
Or Simply use it with $this->db->distinct(); do like this:
$this->load->database('orb');
$this->db->distinct();
$master_id = $this->session->userdata('master_id');
$query = $this->db->get_where('skills_tb', array('master_id' => $master_id));
return $query->row();
for more : https://www.codeigniter.com/user_guide/database/results.html#result-rows

Data sorting by current date in Codeigniter

I've got a sorting problem. I get data properly from the database, but the records are not sorted as I want: current date records displayed first and then other records.
I've tried to use order_by but later date records are displayed first. I want to get current date records first. How to achieve it...??
See my model.
public function getshopsponsered()
{
$this->db->select('*');
$this->db->from('shop_sponsered');
$this->db->order_by('startdate', 'DESC');
$query = $this->db->get();
$shop_list = array();
foreach($query->result() as $row)
{
$sm = new Shop_sponsered_model();
$sm->setId($row->id);
$sm->setStartdate($row->startdate);
$sm->setEnddate($row->enddate);
$sm->setDisplayIndex($row->display_index);
//get business name//
$this->db->select('name');
$this->db->from('business');
$this->db->where('id',$row->businessid);
$query = $this->db->get()->row();
if($query > 0)
{
$sm->setBusinessid($query->name);
}
array_push($shop_list,$sm);
}
return $shop_list;
}
change the $query instead of.
$query_outter = $this->db->get();
$shop_list = array();
foreach($query_outter->result() as $row)
{...}

how to query like for more than one keyword

i have a query to search content table to find rows with some keywords.(codeigniter)
i have a column to store keywords(the name of provinces).
i have a main province and related provinces that i should search them.
i have an array of keywords and its not clear that how many indexes it have.
so how can i search the content with multiple keywords
this is my query in model
public function province($province_name,$provinces_name,$language)
{
$ps_name = str_replace("-", " ", $provinces_name);
$lang=$language;
$data = $this->db
->select('*')
->from('content')
->where("language",$lang)
->where("keywords",$province_name)
->or_like("keywords","%$ps_name%")
->limit("14")
->order_by("id","DESC")
->get();
if ($data->num_rows > 0) {
return $data->result();
} else{
return false;
}
}
If you have an array of keywords and you want each keyword to be searched at once, you can use $this->db->or_like() in a loop. Try code below:
$keywords = array("keyword1",
"keyword2",
"keyword3",
);
foreach($keywords as $k)
{
$this->db->or_like("keywords", $k);
}
$this->db->where("language", $lang);
$this->db->limit("14");
$this->db->order_by("id", "DESC");
$query = $this->db->get("content");
Not sure about CI specific implementation, but if you're doing a search-like function with multiple keywords you can take a look at MySQL's full-text search (for InnoDB or MyIASM). Or a pretty good tutorial here.

Categories