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.
Related
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.
i trying to search item name or item code in database into different rows in one table. The admin can search items, through item name or code.
But when i write this code to search data. They show me blank result. The question is how to i do this.
how i correct "like query". The problem is here
->like('item_name',$quer)
->like('item_code',$query)
Here is the code:
function prd_search($query)
{
$q= $this->db->from('purchase')
->like('item_name',$query)
->like('item_code',$query)
->get();
return $q->result();
}
You have to use ORoperation
Your code is :
function prd_search($query)
{
$q= $this->db->from('purchase')
->like('item_name',$query)
->or_like('item_code',$query)
->get();
return $q->result();
}
{
$q= $this->db->from('purchase')
->group_start()
->like('item_name',$query)
->or_like('item_code',$query)
->group_end()
//you can other conditions here.
->get();
return $q->result();
}
I tried to fetch data using joins and the data is repeating,
The controller code is:
public function searchjobs2()
{
//$id=$_SESSION['id'];
$lan = $_POST["picke"]; //var_dump($id);die();
$value['list']=$this->Free_model->get_jobs($lan);//var_dump($value);die();
$this->load->view('free/header');
$this->load->view('free/searchjobs2',$value);
}
And the model:
public function get_jobs($lan)
{
$this->db->select('*');
$this->db->from("tbl_work_stats");
$this->db->join("tbl_work", "tbl_work.login_id = tbl_work_stats.login_id",'inner');
$this->db->where("language LIKE '%$lan%'");
// $this->db->where('tbl_work_stats.login_id',$id);
$this->db->order_by('insertdate','asc');
$query=$this->db->get()->result_array();//var_dump($query);die();
return $query;
}
I have used
foreach ($list as $row){
...
}
for listing.
Using distinct will remove duplicate fields:
$this->db->distinct();
From what I can see, your query has ambiguity, and an error in the join statement, also your where like is part of the problem, I would recommend trying this even do there are some missing info, find out wich field you need to join from the second table.
public function get_jobs($lan){
$this->db->select("tbl_work_stats.*, tbl_work.fields");
$this->db->from("tbl_work_stats");
$this->db->join("tbl_work", "tbl_work_stats.login_id = tbl_work.login_id","inner");
$this->db->where("tbl_work.language LIKE", "%" . $lan . "%" );
$this->db->order_by("tbl_work_stats.insertdate","asc");
$query=$this->db->get()->result_array();
return $query;}
do you mean to join on login_id?
I am guessing that is the user logging in and it is the same for many entries of tbl_work_stats and tbl_work.
you didn't post your schema, , but login_id doesn't seem like right thing to join on. how about something like tbl_work.id = tbl_work_stats.tbl_work_id or similar?
also CI $db returns self, so you can do:
public function get_jobs(string $lan):array
{
return $this->db->select()
->from('tbl_work_stats')
->join('tbl_work','tbl_work.id = tbl_work_stats.work_id')
->like('language',$lan)
->order_by('insertdate')
->get()
->result_array();
}
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
I presently have 3 tables: Shows, Genres and Show_Genre (associates the two). I have a search form that submits a series of checkboxes with values into an array based on what genres they selected. Presently I want to associate the Shows table and the Genres table into a variable and run a query against it once for every genre checkbox selected. Then, once the selection is filtered, I can display the resulting Show objects that matched the users parameters.
My present setup is the following
public function searchShows(SearchRequest $request)
{
//$ShowsWithGenres give a collection inside a collection which I can't seem to even access its values without doing a bunch of ugly code using count() in a for loop
$ShowsWithGenres = Show::with('genres')->get();
$genres = $request->name;
if(isset($genres))
{
foreach($genres as $genre)
{
//iterate against the selection repeatedly reducing the number of results
}
}
}
Thanks.
You should use whereHas() and whereIn.
Perhaps something like this should do it:
$shows = Show::whereHas('genres', function($q) use($genre_ids)
{
$q->whereIn('id', $genre_ids);
})->get();
EDIT
Try this, however I'm unsure about the performance.
$query= Show::query();
foreach($genre_ids as $id){
$query->whereHas('genres', function($q) use($id)
{
$q->where('id', $id);
})
}
$shows = $query->get();
Using Eloquents whereHas() function you can query results based on the relation's data. http://laravel.com/docs/5.0/eloquent#querying-relations
public function searchShows(SearchRequest $request)
{
// Start a query (but don't execute it at this point as we may want to add more)
$query = Show::with('genres');
$genreNames = (array) $request->name;
// Check there are some genre names, if theres not any it'll cause an SQL syntax error
if (count($genreNames) > 0)
{
$query->whereHas('genres', function($subQuery) use ($genreNames)
{
$subQuery->whereIn('genre_name', $genreNames);
}
}
// Finally execute the query. $shows now contains only shows with the genres that the user has searched for, if they didn't search with any genres, then it contains all the results.
$shows = $query->get();
}