Advance Search In Codeigniter - codeigniter

i am do searching in codeigniter an its work using this query
$this->db->where("(h_name LIKE '".$term."%' )");
i would like to advanced this search like dataTable in codeigniter any way to improve the query serch all letter from the word please help me?

You can try this in two ways
$this->db->where("(h_name LIKE '%".$term."%' )");
OR
$this->db->where("(h_name REGEXP '".$term."' )");

Try This,
Model:
Public function search($search){
$this->db->like('users.email', $search);
$this->db->or_like('users.mobile', $search);
$query = $this->db->get("users");
return $result = $query->result_array();
}

Related

Laravel Scout with TNTSearch driver filtering where clause

I try to get the result of search queries with a where clause in the query using TNTSearch, but it doesn't work. The query didn't get the where clause.
Controller
<?php
public function getLigues(Request $request)
{
if ($request->has('recherche')) {
$ligues = Structure::search($request->recherche)->where('type_structure_id', '2')->get();
} else {
$ligues = Structure::where('type_structure_id', 2)->paginate(1);
}
return view('structure/ligues', compact('ligues'));
}
Does anyone have any ideas on how to get the filter query?
Please go through this issue #59 on official TNT Search Repository.
See contributors comment on same issue.
Where() clause yet does not support!
There are other workarounds to achieve this testcase, for that check issues on repository.

Case sensitive query in yii2

To find record in yii2 I use following code:
$response = Response::findOne(['unique_url' => $unique_url]);
But it return record regardless $unique_url case.
How to do it case sensitive?
I think you should use LIKE BINARY
and for this you should extended you modelSearch adding the clause in query condition
public function search($params)
{
$query = YuorModel::find();
.......
.......
$query->andFilterWhere(['like binary', 'unique_url', $this->unique_url])
->andFilterWhere(['like', 'your_field2', $this->your_field2])
.......
Best solution which I found for this:
Response::find()->where('BINARY [[unique_url]]=:unique_url', ['unique_url'=>$unique_url])->one();

laravel Eloquent ORM - How to get compiled query?

In Laravel 4.2 I want to get Compiled Query.
This is what i have:
$product = Product::where('id', '=', '100')->get();
I want compiled query like:
select * from products where id = 100
Purpose of the question is: i want to use it as sub query in another query.
I have searched and found Class Grammer and Class MySQL But i did not found solution for that.
Is there any solution?
Your help would be appreciated.
The easiest way is probably mostly finding out what query was just executed, rather than what the query will be. Something like this:
function latestQuery()
{
$queries = DB::getQueryLog();
return end($queries);
}
Hopefully that's the same for your purposes.
You can get the SQL query like this:
use DB;//write this at the top of the file above your Class
DB::enableQueryLog();//Enable query logging
$product = Product::where('id', '=', '100')->get();
dd(DB::getQueryLog());//print the SQl query
You can register an event listener in your routes file(in development phase), which will listen for the laravel query event and var_dump the executed query.
Event::listen('illuminate.query', function($sql)
{
var_dump($sql);
});
But this will turn out to be messy. So better use something like Clockwork. It is awesome, you can view all the executed query in your browser.
You can use grammer for subqueries, See below example for reference :
$users = DB::table('users')
->where('user.id', '=', $userID)
->join('product', 'product.userid', '=', 'user.id');
$price = $users->select(array(DB::raw('SUM(price)')))
->grammar
->select($users); // Compiles the statement
$result = DB::table('users')->select(array(DB::raw("({$price}) as price)))->get();
This add a subquery to your main query.

Codeigniter mimic where_in() functionality with a simple where() statement

I have a method which can take a variable $where. This is then passed to a $this->db->where($where); statement.
I am trying to mimic the functionality of where_in() for one particular function.
I have a list of IDs in either array format or imploded string format.
I have tried passing $where=array('blog.ID IN'=>'1,3');
to the method to no avail.
This is causing WHEREblog.IDIN '1,3'
to be output instead of WHEREblog.IDIN '1','3'
Can anyone advise how i can use codeigniters where() function to mimic what its where_in() function does?
Thanks
Try:
$array = [1,3];
$where='blog.ID IN' . join(',', $array);
Couldn't you do something like this?
function your_db_thingie($where)
{
$this->db->select('*');
if (is_array($where))
{
$this->db->where_in('field', $where);
}
else
{
$this->db->where('field', $where);
}
return $this->db->get('database');
}

codeigniter database how to limit the output

Hello how can i like if i use substr(); do so i only get like 400 number of characters from the database out?
You have to use core mysql function SUBSTRING to achieve this.
In codeigniter the query can be written as -
$this->db->select("SUBSTRING('COLUMN_NAME',5)");
$query = $this->db->get('TABLE_NAME');
foreach ($query->result() as $row)
{
//process result here.
}
You can use codeigniters limiter (test helper) to display only what you want
$string = "Here is a nice text string consisting of eleven words.";
$string = character_limiter($string, 400);
You can pull the entire string out of the database but only use the number of characters you need.
Or
take a look at this tutorial using "left" in mysql
http://net.tutsplus.com/tutorials/php/how-to-create-blog-excerpts-with-php/
It too late, but this is for someone like me looking for solution
public function getDetails(){
// mytable(id,name,about,...,status)
$this->db->select(array('id', 'name', 'SUBSTRING(about,1,180) AS about', 'status'));
$result=$this->get('mytable');
return result_array();
}

Resources