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.
Related
I want to get data from two tables, the second table is for rating so i want to get rating of products simultaneosly. Below code is not working for me if i change
$this->db->select('dg_products.',', AVG(dg_rating.rating) As
averageRating');
to
$this->db->select('*');
then it is working.
Please help to sort out my issue.
public function get_rating()
{
$this->db->select('dg_products.*','*, AVG(`dg_rating.rating`) As averageRating');
$this->db->from('dg_products');
$this->db->join('dg_rating', 'dg_products.id = dg_rating.product_id','left');
$this->db->where('dg_products.is_featured_prod','1');
$this->db->group_by("dg_products.id");
$query = $this->db->get();
$result = $query->result();
return $result;
}
Try it like this :
$this->db->select('dg_products.*, AVG(`dg_rating.rating`) As averageRating');
you just have an unneeded quotes in there.
I have two join tables; parent and student. I have to update both tables from one button click. For that, I want to write a function "get data by id". I managed to write that code only for one table.
How do you write the following code if I want to get data from two tables? if p_id (parent id) is the foreign key?
Model
function get_by_id($id)
{
$this->db->from('student');
$this->db->where('p_id',$id);
$query = $this->db->get();
return $query->row();
}
Controller
public function ajax_edit($id)
{
$data = $this->Model_Action->get_by_id($id);
echo json_encode($data);
}
Hi I think you are looking for this. I use a sample from your code:
function get_by_id($id)
{
$this->db->from('student');
$this->db->join('table_b', 'student.p_id=table_b.p_id');
$this->db->where('student.p_id',$id);
$query = $this->db->get();
return $query->row();
}
Actually you can find more here
function get_by_id($id)
{
$this->db->select('*')
$this->db->from('student');
$this->db->join('parent','student.p_id=parent.p_id');
$this->db->where('student.p_id',$id);
$query = $this->db->get();
return $query->row();
}
I make a join between two tables using codeigniter framework and this is my query:
public function SearchDataUnderCondition($firsttable,$secondtable,$data)
{
$this->db->select("atm.* , tender.status as tenderstatus");
$this->db->from($firsttable);
$this->db->join($secondtable,'atm.id_tender=tender.id');
$this->db->where('tenderstatus','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
I am using database model when is remove
$this->db->where('tenderstatus','1');
from my code operation done and i get result but I want to make search under this condition. what is my problem?
Try this
public function SearchDataUnderCondition($data)
{
$this->db->select('atm.* , tender.status');
$this->db->from('atm');
$this->db->join('tender','atm.id_tender=tender.id');
$this->db->where('tender.status','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
Try this
$this->db->where('tender.status','1');
I think, we can not aliases in where clause
Using aliases (and also making sure you want and WHERE AND LIKE clause):
$fist_table = 'atm';
$second_table = 'tender';
$this->db->select('aliasone.* , aliastwo.status as "tenderstatus"');
$this->db->from("$first_table as aliasone");
$this->db->join("$second_table as aliastwo", "aliastwo.id = aliasone.tender_id");
$this->db->where('aliastwo.status','1');
$this->db->like('serial', $data);
return $this->db->get();
I m having a trouble how can i show or display a table with multiple relationships to be returned with its relation inteadt of the table / user only.
Forexample:
I have Publication Table and Issue Table ..related to 1-Publication can Have multiple/many-ISSUES.
And , when i wanted to load for example
http://restapi.url/publications/format/json
I wanted to see Publication and also all related Issues on code igniter. This my code on my Controller..
function publications_get(){
$this->output->enable_profiler(TRUE);
$this->db->select();
$this->db->from('publication');
$return = $this->db->get();
if( $return!= null ) {
$this->response($return->result_array(),200); //success
} else {
$this->response(NULL,404);
}
Juts to be like this :
<officer id="1">
<name>James T. Kirk</name>
<rank>Captain</rank>
<subordinates>
<link ref="http://ncc1701/api/officers/2">
<link ref="http://ncc1701/api/officers/3">
</subordinates>
</officer>
use two queries, one for all publications (like now) and for every issues by publication
function publications_get(){
$this->output->enable_profiler(TRUE);
$this->db->select();
$this->db->from('publication');
$return = $this->db->get();
$publications = $return->result_array(); //success
foreach($publications as $index => $publication){
$publications[$index]['issues'] = issues_get($publication['id']);
}
$this->response($publications,200);
}
Getting the issues in publication
function issues_get($id_publication){
$this->output->enable_profiler(TRUE);
$this->db->select();
$this->db->from('isssues');
$this->db->where('id_publication', $id_publication);
$return = $this->db->get();
$this->response($return->result_array(),200); //success
}
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.