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();
}
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 tried to search students that belong to particular User/School only using the following the query. I have created one-to-many relationship between User and Students.. Everything seems okay but when I try to search students, it gives me list of students that belong to other Users too.
public function searchStudent(Request $request)
{
$q = $request->q;
$grades = Auth::user()->grades;
$searchPupils = Student::where('user_id','=',Auth::user()->id)->where('name','LIKE','%'.$q.'%')->orWhere('email','LIKE','%'.$q.'%')->get();
if(count($searchPupils) > 0)
{
return view('add-class', compact('grades'))->withDetails($searchPupils)->withQuery($q);
}
else
{
return view ('add-class', compact('grades'))->withMessage('No Details found. Try to search again !');
}
}
I also tried doing
$searchPupils = Auth::user()->students()->where('name','LIKE','%'.$q.'%')->orWhere('email','LIKE','%'.$q.'%')->get();
Still it searches for the whole Students table . How should it be done?
the problem is in your query where conditions, use advance where clause as below.
$searchPupils =Student::where('user_id','=',Auth::user()->id)
->where(function($query)use($q){
$query->where('name','LIKE','%'.$q.'%')
->orWhere('email','LIKE','%'.$q.'%');
})->get();
I'm new to PHP codeigniter. I need a big help from you people. This is one of my project matter. The thing is i'm going to access a table in the database in order to calculate the distance. Table name is gps_info. In there, there are two attributes which can be used to calculate the distance. Those are speed and timestamp. This my active record
public function get_distance() {
$this->db->select('speed','timestamp');
$this->db->from('gps_info');
$this->db->orderby('gps_info.id','desc');
$query = $this->db->get();
return $query->result();
}
This is not correct.. Can you help me please how to write this service correctly :)
The working code for fetching the data what you want:
public function get_distance()
{
$res = $this->db->select('speed, timestamp')
->from('gps_info')
->orderby('id', 'desc')
->get()
->result_array();
return $res;
}
Your select portion is wrong, it should be:
$this->db->select('speed, timestamp');
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();
}
My problem is pretty simple. I'm using codeigniter active record, and all the model class has to do is select the most recent item from a table that belongs to the user.
function get_progress($users_id)
{
$query = $this->db->get('progress');
$this->db->where('user_key', $users_id);
$this->db->order_by("id", "desc");
$this->db->limit(1);
return $query->row_array();
}
Seems simple, but for some reason, it's grabbing the lowest id that matches the user_key.
I've tried changing the where statement to
$this->db->where('id', '2');
And it works, but of course, that's just for troubleshooting. I need variables.
I've rewritten few ways, including using get_where(), and changing desc to asc. No matter what, it's grabbing the low id. How can I select the highest id where user_key is the matching number.
You can try the code below
function get_progress($users_id){
return $this->db->from('progress')
->where('user_key', $users_id)
->order_by("id", "DESC")
->get()
->row();
}
You will get the last recent row as STD Class object
function get_progress($users_id)
{
$this->db->select('*')
->from('progress')
->where('user_key',$users_id)
->order_by('id','desc')
->limit(1);
$q=$this->db->get();
return $q->result_array();
}