get() returns empty rows in Codeigniter - php

I am using the function below but it did not return any rows. I tried to do $query->num_rows() but it has null values. I got the last query using $this->db->last_query(), used this in the database and there are results. Am I missing a setup in Code Igniter? Looking forward to your reply.
function search($conditions=NULL,$tablename="",$orderby='id', $order='DESC',$limit=1000000,$offset=0)
{
if($tablename=="")
{
$tablename = $this->table;
}
if($conditions != NULL)
$this->db->where($conditions);
if ( $orderby !== "" and $order !== "")
$this->db->order_by($orderby, $order);
$query = $this->db->get($tablename,$limit,$offset=0);
return $query->result_array();
}

Try like
if($conditions != NULL)
$this->db->where($conditions);
if ( $orderby !== "" and $order !== "")
$this->db->order_by($orderby, $order);
$this->db->limit($limit,$offset);
$query = $this->db->get($tablename);

first you check $tablename is table name or blank
function search($conditions=NULL,$tablename="",$orderby='id', $order='DESC',$limit=1000000,$offset=0)
{
if($tablename=="")
{
$tablename = $this->table;
}
if($conditions != NULL)
$this->db->where($conditions);
if ( $orderby !== "id" and $order !== "DESC")
$this->db->order_by($orderby, $order);
$this->db->limit($limit);
$this->db->offset ($offset );
$query = $this->db->get($tablename);
if($query->num_rows() == 0){
}

Related

Writing a function in laravel

I have the following function which fetch some data related to jobs from database. The user can search for jobs with job title / keyword, city and/or category. The user can either choose one option, e.g. searching jobs only by title, or by category. or he can use all options for deep search. Below is my function:
public function jobsearch(Request $request)
{
$keyword = htmlspecialchars($request->input('keyword'));
$city_id = $request->input('city_id');
$category_id = $request->input('category_id');
if($keyword !== '' && $city_id != 0 && $category_id == 0)
{
$data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->where('city_id', $city_id)->get();
}
elseif($keyword !== '' && $city_id == 0 && $category_id != 0)
{
$data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->where('category_id', $category_id)->get();
}
elseif($keyword == '' && $city_id != 0 && $category_id != 0)
{
$data = DB::table('job_details')->where('category_id', $category_id)->where('city_id', $city_id)->get();
}
elseif($keyword !== '' && $city_id == 0 && $category_id == 0)
{
$data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->get();
}
elseif($keyword == '' && $city_id == 0 && $category_id != 0)
{
$data = DB::table('job_details')->where('category_id', $category_id)->get();
}
elseif($keyword == '' && $city_id != 0 && $category_id == 0)
{
$data = DB::table('job_details')->where('city_id', $city_id)->get();
}
else
{
$data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->where('category_id', $category_id)->where('city_id', $city_id)->get();
}
foreach($data as $data)
{
echo $data->job_title.'<br>';
}
}
As you can see the function is too much messy with many if and elseif statements. My question is if there is any way to write the given function in clean way? How would you write the given function in your style? Please Help.
You're really missing out on the best parts of Laravel's query builder.
public function jobsearch(Request $request) {
// htmlspecialchars makes no sense here
$keyword = $request->input('keyword');
$city_id = $request->input('city_id');
$category_id = $request->input('category_id');
$query = DB::table('job_details');
if($keyword) {
$query->where('job_title', 'like', '%'.$keyword.'%');
}
if($city_id) {
$query->where('city_id', $city_id);
}
if($category_id) {
$query->where('category_id', $category_id);
}
$results = $query->get();
foreach($data as $data) { ... }
}

Creating a new collection from another collection

im creating a collection of specific data from a query that i made, but i need to create a new collection with only some data with custom names properties, i was using arrays, but i need to make it in collections since is easyer to format the data and access some collections methods.
My current code is like:
$activity = [];
$temp = [];
$calculations = collect($user->calculations()
->withTrashed()
->orderBy('updated_at', 'desc')
->get());
foreach($calculations as $calculation){
$temp['type'] = "calculation";
$temp['name'] = $calculation->name;
$user = $this->getUserById($calculation->pivot->user_id);
$temp['user'] = $user->name ." ".$user->surname;
if($calculation->created_at == $calculation->updated_at && $calculation->deleted_at == null)
{
$temp['operation'] = "saved";
$temp['date'] = $calculation->created_at;
$temp['diff'] = Carbon::parse($calculation->created_at)->diffForHumans();
}elseif($calculation->created_at != $calculation->updated_at && $calculation->deleted_at != null)
{
$temp['operation'] = "changed";
$temp['date'] = $calculation->updated_at;
$temp['diff'] = Carbon::parse($calculation->updated_at)->diffForHumans();
}else{
$temp['operation'] = "delete";
$temp['date'] = $calculation->deleted_at;
$temp['diff'] = Carbon::parse($calculation->deleted_at)->diffForHumans();
}
array_push($activity,$temp);
}
$conditions = collect($user->conditions()
->withTrashed()
->orderBy('updated_at', 'desc')
->get());
foreach($conditions as $condition){
$temp['type'] = "condition";
$temp['name'] = $condition->name;
$user = $this->getUserById($condition->user_id);
$temp['user'] = $user->name ." ".$user->surname;
if($condition->created_at == $condition->updated_at && $condition->deleted_at == null)
{
$temp['operation'] = "saved";
$temp['date'] = $condition->created_at;
$temp['diff'] = Carbon::parse($condition->created_at)->diffForHumans();
}elseif($condition->created_at != $condition->updated_at && $condition->deleted_at != null)
{
$temp['operation'] = "alterado";
$temp['date'] = $condition->updated_at;
$temp['diff'] = Carbon::parse($condition->updated_at)->diffForHumans();
}else{
$temp['operation'] = "delete it";
$temp['date'] = $condition->deleted_at;
$temp['diff'] = Carbon::parse($condition->deleted_at)->diffForHumans();
}
array_push($activity,$temp);
I already convert the eloquent query to "collect", but how i cant createa new collections, i need to instead using the array methods, i should use the collection methods to create them.
Basically my main reason is that i need to merge the "conditions" and "calculations" for than be able to order the dataTime the collections.
How about something like this.
I've used transform method on collections (in order to transform the key names). I've replicated your logic and then merged both collections.
$calculations = $user->calculations()
->withTrashed()
->orderBy('updated_at', 'desc')
->get();
$transformed = $calculations->transform(function($item, $key) use($user) {
$toReturn = [];
$toReturn['type'] = "calculation";
$toReturn['name'] = $item->name;
$toReturn['user'] = $user->name;
if($item->created_at == $item->updated_at && $item->deleted_at == null) {
$toReturn['operation'] = "saved";
$toReturn['date'] = $item->created_at;
$toReturn['diff'] = Carbon::parse($item->created_at)->diffForHumans();
} elseif($item->created_at != $item->updated_at && $item->deleted_at != null){
$toReturn['operation'] = "changed";
$toReturn['date'] = $item->updated_at;
$toReturn['diff'] = Carbon::parse($item->updated_at)->diffForHumans();
} else {
$toReturn['operation'] = "delete";
$toReturn['date'] = $item->deleted_at;
$toReturn['diff'] = Carbon::parse($item->deleted_at)->diffForHumans();
}
return $toReturn;
});
$conditions = $user->conditions()
->withTrashed()
->orderBy('updated_at', 'desc')
->get();
$transformed2 = $conditions->transform(function($item, $key) use($user) {
$toReturn = [];
$toReturn['type'] = "calculation";
$toReturn['name'] = $item->name;
$toReturn['user'] = $this->getUserById($item->user_id);
if($item->created_at == $item->updated_at && $item->deleted_at == null) {
$toReturn['operation'] = "saved";
$toReturn['date'] = $item->created_at;
$toReturn['diff'] = Carbon::parse($item->created_at)->diffForHumans();
} elseif($condition->created_at != $condition->updated_at && $condition->deleted_at != null){
$toReturn['operation'] = "changed";
$toReturn['date'] = $item->updated_at;
$toReturn['diff'] = Carbon::parse($item->updated_at)->diffForHumans();
} else {
$toReturn['operation'] = "delete";
$toReturn['date'] = $item->deleted_at;
$toReturn['diff'] = Carbon::parse($item->deleted_at)->diffForHumans();
}
return $toReturn
});
$merged = $transform->merge($transform2);
Building on #devk 's answer here is a neater version without so much repetitive code:
/**
* Transform a collction with a given callback
*
* #param Collection $collection A laravel collection
* #param User $user A User object
* #return Collection
**/
private function transformCollection(Collect $collection, User $user) {
return $collection->transform(function($item, $key) use ($user) {
$toReturn = [
'type' => 'calculation',
'name' => $item->name,
'user' => $user->name
];
if ($item->created_at == $item->updated_at && $item->deleted_at == null) {
$toReturn['operation'] = "saved";
$toReturn['date'] = $item->created_at;
$toReturn['diff'] = Carbon::parse($item->created_at)->diffForHumans();
} elseif ($item->created_at != $item->updated_at && $item->deleted_at != null) {
$toReturn['operation'] = "changed";
$toReturn['date'] = $item->updated_at;
$toReturn['diff'] = Carbon::parse($item->updated_at)->diffForHumans();
} else {
$toReturn['operation'] = "delete";
$toReturn['date'] = $item->deleted_at;
$toReturn['diff'] = Carbon::parse($item->deleted_at)->diffForHumans();
}
return $toReturn;
});
}
// Return all user calculations ordered by when they were updated including deleted
$calculations = $user->calculations()->withTrashed()->orderBy('updated_at', 'desc')->get();
$conditions = $user->conditions()->withTrashed()->orderBy('updated_at', 'desc')->get();
// Transform both collections
$transformed = transformCollection($calculations, $user);
$transformed2 = transformCollection($conditions, $user);
// Merge the resulting collections into a single collection
$merged = $transform->merge($transform2);
Edit
If your Calculation object has a model you can also make sure that the dates are returned as Carbon dates by adding them to the protected $dates = [] array
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
I think that created_at and updated_at are included in this by default as part of BaseModel, thought i could well be wrong.

How to join table dynamically in codeigniter?

I want to join table dynamically in codeigniter by passing table name as parameter.
below is my controller function to call model function
function index(){
$table=array('branch'=>'branch_id','specialization'=>'spec_branch_id');
$this->model->join($table);
}
and this is my model function
function join($table){
foreach($table as $table_name=>$table_id){
/*i want here table*/
$table1=$table_name;
}
$this->db->select('*');
$this->db->from(''.$table1.' t1');
$this->db->join(''.$table2.' t2','t1.'.$t1id.'=t2.'.$t2id);
return $this->db->get();
echo $this->db->last_query();die;
}
As in above function i want dynamic table name like table1=branch;
table2=specializaton in my model function, so please help me to solve and if someone have another also can share.
here is my custom code dynamically join.
mode form.
<?php public function commonGet($options) {
$select = false;
$table = false;
$join = false;
$order = false;
$limit = false;
$offset = false;
$where = false;
$or_where = false;
$single = false;
$where_not_in = false;
$like = false;
extract($options);
if ($select != false)
$this->db->select($select);
if ($table != false)
$this->db->from($table);
if ($where != false)
$this->db->where($where);
if ($where_not_in != false) {
foreach ($where_not_in as $key => $value) {
if (count($value) > 0)
$this->db->where_not_in($key, $value);
}
}
if ($like != false) {
$this->db->like($like);
}
if ($or_where != false)
$this->db->or_where($or_where);
if ($limit != false) {
if (!is_array($limit)) {
$this->db->limit($limit);
} else {
foreach ($limit as $limitval => $offset) {
$this->db->limit($limitval, $offset);
}
}
}
if ($order != false) {
foreach ($order as $key => $value) {
if (is_array($value)) {
foreach ($order as $orderby => $orderval) {
$this->db->order_by($orderby, $orderval);
}
} else {
$this->db->order_by($key, $value);
}
}
}
if ($join != false) {
foreach ($join as $key => $value) {
if (is_array($value)) {
if (count($value) == 3) {
$this->db->join($value[0], $value[1], $value[2]);
} else {
foreach ($value as $key1 => $value1) {
$this->db->join($key1, $value1);
}
}
} else {
$this->db->join($key, $value);
}
}
}
$query = $this->db->get();
if ($single) {
return $query->row();
}
return $query->result();
} ?>
and view index page
<?php function index(){
$option = array(
'table' => 't1',
'join' => array('t2' => 't2.id = t1.id')
);
$this->model->commonGet($option);
}?>
You can try this code also for two table join.
Controller function.
function index(){
$table=array('branch'=>'branch_id','specialization'=>'spec_branch_id');
$this->model->join($table);
}
Model function
function join($tables){
$i=1;
foreach($tables as $table_name=>$table_id){
${'table'.$i}=$table_name;
${'t'.$i.'id'}=$table_id;
$i++;
}
$this->db->select('*');
$this->db->from(''.$table1.' t1');
$this->db->join(''.$table2.' t2','t1.'.$t1id.'=t2.'.$t2id);
//$this->output->enable_profiler(TRUE);
return $this->db->get();
}

php if statement null and 0?

i'm having trouble executing my if statement
if($parent == $page->parent)
my dollar $parent = null and my $page->parent = 0 how do i fix this that they are equal?
i think it's a problem with the fact it doesn't know that null is equal to 0
$page = Page::find($id);
$parent = Input::get('parent'); // Null
i hope you guys can help me out i have to figure this out
here's my controller just in case you wan't to take a look at it
public function updateMenu($id)
{
$page = Page::find($id);
$parent = Input::get('parent');
$new_order = Input::get('index');
if($parent == $page->parent)
{
if($page->order_id > $new_order)
{
DB::table('pages')
->where('parent',$parent)
->where('order_id', '<', $page->order_id)
->increment('order_id');
}
else
{
DB::table('pages')
->where('parent',$parent)
->where('order_id', '>=', $page->order_id)
->decrement('order_id');
}
}
else
{
DB::table('pages')
->where('parent',$parent)
->where('order_id', '>=', $new_order)
->increment('order_id');
}
$page->order_id = Input::get('index');
$page->parent = Input::get('parent');
$page->save();
return $id;
}
I dont think 0 can be equals with NULL. maybe if($parent == $page->parent || ($parent == null && $page->parent == 0)) more helpful
Use the strict equality operator: === instead of ==
localhost> = 0 === null
false
localhost> = 0 == null
true
You can use:
if(is_null($parent) && ($page->parent==0))
{
}

Codeigniter 2.1 - counting the array before get

I have this function:
function gi_get_by($col,$id, $itd, $tbl, $limit = 10)
{
$this->db->select('*');
$this->db->from('global_info');
$this->db->join($tbl, 'id_' . $tbl . ' = id_global_info');
$this->db->where('info_type_id', $itd);
if($col == 'date_created' || $col == 'tag') {$this->db->like($col, $id);}
else {$this->db->where($col, $id);}
if($tbl == 'ad') :
$this->db->order_by('paid', 'desc');
endif;
$this->db->order_by('date_created', 'desc');
$this->db->limit($limit, $this->uri->segment(2));
$q = $this->db->get();
return $q = $q->result_array();
}
What I need is to count number of results before limit and to use them later in controller. I have an idea to duplicate this function without $limit but then it will be duplicating the same function. Is there another way to do this or I have to go with duplication?
I don't quite understand what you want to do but if you want an optional limit you can default it to false:
function gi_get_by($col,$id, $itd, $tbl, $limit=false)
{
$this->db->select('*');
$this->db->from('global_info');
$this->db->join($tbl, 'id_' . $tbl . ' = id_global_info');
$this->db->where('info_type_id', $itd);
if($col == 'date_created' || $col == 'tag') {$this->db->like($col, $id);}
else {$this->db->where($col, $id);}
if($tbl == 'ad') :
$this->db->order_by('paid', 'desc');
endif;
$this->db->order_by('date_created', 'desc');
if ($limit) {
$this->db->limit($limit, $this->uri->segment(2));
}
$q = $this->db->get();
return $q = $q->result_array();
}
This will conditionally add limit clause if it is passed in to the function.
Also it would be best to pass in wwhatever $this->uri->segment(2) into the function as a parameter instead of accessing it from within the function.
Why not select
sql_calc_found_rows
in your query?
http://www.justincarmony.com/blog/2008/07/01/mysql-php-sql_calc_found_rows-an-easy-way-to-get-the-total-number-of-rows-regardless-of-limit/
Well, how about something like this:
function gi_get_by($col,$id, $itd, $tbl, $limit = 10)
{
$count = $this->db->query('SELECT * FROM my_table')->num_rows();
//the rest stuff
}
You should use Active Record Cache:
http://codeigniter.com/user_guide/database/active_record.html#caching
function gi_get_by($col,$id, $itd, $tbl, $limit = 10)
{
// Start the cache
$this->db->start_cache();
$this->db->select('*');
$this->db->from('global_info');
$this->db->join($tbl, 'id_' . $tbl . ' = id_global_info');
$this->db->where('info_type_id', $itd);
if($col == 'date_created' || $col == 'tag') {$this->db->like($col, $id);}
else {$this->db->where($col, $id);}
if($tbl == 'ad') :
$this->db->order_by('paid', 'desc');
endif;
$this->db->order_by('date_created', 'desc');
// Stop the cache
$this->db->stop_cache();
// Get the total
$total = $this->db->count_all_results();
// Now set the limit
$this->db->limit($limit, $this->uri->segment(2));
$q = $this->db->get();
// Important! Clear the cache
$this->db->flush_cache();
return $q = $q->result_array();
}

Categories