where() not working in codeigniter database - php

I wrote this code for search in my table search for a string in posts where show = 1.
but when i run this code where('show',1) not affected and rows where having show = 0 returned from database, what this problem?
public function json_search($str)
{
$out['Threadt'] = $this->db
->select('id')
->from('posts')
->like('title',$str)
->or_like('story',$str)
->or_like('description',$str)
->or_like('full-story',$str)
->where('date <',time())
->where('show',1)
->get()
->result_array();
}

you should try this, this should work:
function json_search($str)
{
$where = "( story like '%".$str."%' OR title like '%".$str."%' OR description like '%".$str."%' OR full-story like '%".$str."%' )";
$out['Threadt'] = $this->db
->select('id')
->from('posts')
/*->like('title',$str)
->or_like('story',$str)
->or_like('description',$str)
->or_like('full-story',$str)*/
->where( $where, false, false )
->where('date <',time())
->where('show',1)
->get()
->result_array();
}
See the commented part above, when you're using it like this, you are appending OR clauses and this interferes with the show clause.

Related

LIKE does not work in codeigniter

I am using LIKE to see if the similar order_id exists or not in my db. When i run the application, i see that this below code doesnt return any value. It is returning only null value.
$temp_orders = $this->transaction_model->get_similar_user_temp_transaction($cart_order_id);
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->from('temp_transaction')
->like('order_id', $order_id)
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
Kindly check the above code. Help would be appreciated
try your code after changing statement order
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->like('order_id', $order_id)
->from('temp_transaction')
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
EDIT
If you want to match exact order_id, use where instead of like
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->where('order_id', $order_id)
->from('temp_transaction')
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
write like in where cluase
$query = $this->db->select('*')
->from('temp_transaction')
->where("order_id LIKE '%$order_id%'")->get();
Here SQL like not work, because like is not working with INT values
You have two options :-
1. ->like(CONVERT(order_id, CHAR(16), $order_id)
2. Use ->where(order_id, $order_id)
If you are trying to match exact use where clause.
//dont need ->select('*') //excluding select gives same equivalent
//dont need from
return $this->db->where('order_id', $order_id)->get('temp_transaction')->result();

Symfony2 Query Builder Not Returning Many To One Value

I have an entity that has a column "inventoryLcoation_id" that has a many to one relationship. For some reason when I use createQueryBuilder() it is not returning that value in my ajax call, but it returns everything else: id, name, etc etc.. This is not an issue with Symfony2, its an issue with my lack of knowledge :)
Here is my query builder code:
$qb = $this
->createQueryBuilder('p')
->select('p.id', 'p.name')
->where('p.inventoryLocation = :inventoryId')
->andWhere('p.account = :account_id')
->setParameter('inventoryId', $value)
->setParameter('account_id', $account_id)
->orderBy('p.id', 'DESC');
if($qb->getQuery()->getArrayResult()){
return $qb->getQuery()->getArrayResult();
}else{
return false;
}
What do I need to code to have it also include the value from the inventoryLocation table which is mapped to the column value "inventoryLocation_id"?
Thanks so much for your help in enlightening my understanding to the awesome world of Symfony2!
try a join:
$qb->join('p.inventoryLocation','i')
and in the select especify both entities
$qb->select('p','i')
it should look like this:
$qb = $this
->createQueryBuilder('p')
->select('p','i')
->join('p.inventoryLocation','i')
->where('p.inventoryLocation = :inventoryId') // or ->where('i = :inventoryId')
->andWhere('p.account = :account_id')
->setParameter('inventoryId', $value)
->setParameter('account_id', $account_id)
->orderBy('p.id', 'DESC');

trying to Join 2 table query in Fluent laravel 4

Im trying to Joing 2 table in Fluent trough my Model i run the query on mysql client and give me the return i want.
this is the standard query
select `songlist`.*, `queuelist`.* from `songlist` inner join `queuelist` on `songlist`.`id` = `queuelist`.`songID` where `queuelist`.`songID` = songlist.ID and `songlist`.`songtype` = 'S' and `songlist`.`artist` <> "" order by `queuelist`.`sortID` ASC limit 4
it does return the data im looking for.
now on my model using fluent.
i did like this.
public static function comingUp()
{
$getcomingUp = DB::table('songlist')
->join('queuelist', 'songlist.id', '=', 'queuelist.songID')
->select('songlist.*','queuelist.*')
->where('queuelist.songID', '=', 'songlist.ID')
->where('songlist.songtype', '=', 'S')
->where('songlist.artist', '<>', '')
->orderBy('queuelist.sortID', 'ASC')
->take('4')
->get();
return $getcomingUp;
}
and my controller to test if i get the data look like this
public function getComingUp()
{
$getcomingUp = Radio::comingUp();
foreach ($getcomingUp as $cup)
{
echo $cup->title;
}
// return View::make('comingup', compact('comingup'));
}
as you can see the foreach is no returning any data there is nothing wrong with the query because laravel give me no error at all. but i cant just get the foreach to display the data im looking for.
i try with $cup->songlist.title;
and it dont work ether.
Thanks any help will be appretiated. againg sorry for my English.
You can do something like this.
public static function comingUp()
{
$getcomingUp = DB::table('songlist')
->select('songlist.*','queuelist.*')
->join('queuelist', 'queuelist.songID', '=', 'songlist.id')
->where('songlist.songtype', '=', 'S')
->where('songlist.artist', '<>', '')
->orderBy('queuelist.sortID', 'ASC')
->take('4')
->get();
return $getcomingUp;
}

laravel using query builder without chaining

I have a query builder that works:
$article = Page::where('slug', '=', $slug)
->where('hide', '=', $hidden)
->first();
But I want to only add the second where statement if hidden is equal to 1. I've tried the code below which shows the logic of what I'm trying to do, but it doesn't work.
$article = Page::where('slug', '=', $slug);
if ($hidden == 1) {
$article->where('hide', '=', 1);
}
$article->first();
I'm using Laravel 4, but I think the question still stands with Laravel 3.
Yeah there's a little "gotcha" with Eloquent and the query builder. Try the code below ;)
$query = Page::where('slug', '=', $slug);
if ($hidden == 1) {
$query = $query->where('hide', '=', 1);
}
$article = $query->first();
Note the assigning of $query within the conditional. This is becuase the first where (statically called) returns a different object to the query object within the conditional. One way to get around this, I believe due to a recent commit, is like so:
$query = Page::where('slug', '=', $slug)->query();
This will return the query object and you can do what you want as per normal (Instead of re-assigning $query).
Hope that helps.

Query building using Kohana Query Builder

If I got this:
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->execute()->as_array();
For normal, dsiplaying all orders date_created. Now I can filter by user, doing this ?user_id=112
This will make me having this:
if(isset($get['user_id']))
{
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->where('user_id', '=', $get['user_id'])
->execute()->as_array();
}else{
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->execute()->as_array();
}
Although this is no problem for me have this, but this get to be real much ugly code when I have 4 params to filter out from(4 where statements) e.g ?user_id=112&quantity=2&...
Can I somehow make a inline if/action statement, which would grab all user_ids if there's nothing in $get['user_id'] ?
So I can end up having only this:
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->where('user_id', '=', (isset($get['user_id']) ? $get['user_id'] : 'GRAB ALL!'))
->execute()->as_array();
or is there another, even better way to do this?
Tried looking in Kohana docs if there's some ->if()->where()->exitif().. (just some imagination of how it could work)
You can modify Query object before executing:
$query = DB::select()->from(...);
if (isset($get['user_id'])) {
$query->where('user_id', '=', intval($get['user_id']));
}
if (isset($get['quantity'])) {
$query->where('quantity', '=', floatval($get['quantity']));
}
$result = $query->execute()->as_array();

Categories