Codeigniter: Extract partial data from a query - codeigniter

I have a small question pls:
Let's say i have a long query:
$this->db->select('*')
->from('x')
->join(...)
->where('y >',$id)
->where('z <',$nr)
->where(...)
...
How can i put into a different variable all where statements so i can use them individually but only using one single query?
Thank you.

You need to create different model methods. Each method should have the where query outputting results. In your controller, you call each of these methods and store the results in variables.
Why not do this:
Controller:
function controller_method(){
$res1 = $this->model_name->get_where_results1($id, $nr);
$res2 = $this->model_name->get_where_results2($id, $nr);
$res3 = $this->model_name->get_where_results3($id, $nr);
}
Model:
function get_where_results1($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
function get_where_results2($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
function get_where_results3($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
UPDATE - if you wanted to use filters, i would this way:
Controller:
function controller_method() {
$res1 = $this->model_name->model_method($filters);
}
Model:
function model_method($filters) {
$this->db->select('*');
$this->db->from('x');
$this->db->join("");
if ($filters['filt1']) {
$this->db->where("something", $filters['filt1']);
}
if ($filters['filt2']) {
$this->db->where("something", $filters['filt2']);
}
$query = $this->db->get();
return $query->result();
}

Related

How can i create a conditional check in laravel query

hi how can we implement a condition checks in laravel query builder
$flag_email =true;
$query = DB::table('customer');
if($flag_email) {
$query->where('email','=',$email);
}
if(!$flag_email) {
$query->where('mobile','=',$email);
}
$query->get();
use when method here to check condition see
$query = DB::table('customer')
->when($flag_email, function ($query,$email) {
return $query->where('email', $email);
})
->when(!$flag_email, function ($query,$email) {
return $query->where('mobile', $email);
})->get();
you can use ->when to do conditional check
$query = DB::table('customer')
->when($flag_email, function ($query, $email) {
return $query->where('email', $email);
})
->when(!$flag_email, function ($query, $email) {
return $query->where('mobile', $email);
})->get();
Ternary operator to the rescue:
$query = DB::table('customer')->where($flag_email?'email':'mobile',$email);
You can try by this way. This way will outputs your desire one. This just a way, I am not sure, this one is the proper way.
$flag_email =true;
$query = DB::table('customer');
if($flag_email)
$query = $query->where('email','=',$email);
if(!$flag_email)
$query = $query->where('mobile','=',$email);
$result= $query->get();

fetching data of joining tables in Codeigniter

can you please tell me what's wrong with my function
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
}
I wanna display Two tables in one table using the foriegn key (code_filiere)
Solution 1 : You need to return data for controller
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return $query->result();
}
In the controller
$data = $this->your_model->get_by();
Solution 2 : You need to return data for controller
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return $query;
}
In the controller
$data = $this->your_model->get_by()->result();
From the doc
nothing wrong in your function, but you didn't return the captured value from the function to the controller.
You should use
$query->result() for multi-value
OR
$query->row() for single value
from database
update your function
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return ($query->num_rows() > 0) ? $query->result() : false;
}

Codeigniter get database value in controller

This is my controller
function index($id=NULL)
{
$this->load->model('article');
$id= $this->uri->segment(3);
$data = array('article' => $this->article->show_a_article($id),
'sidebar' => $this->article->side_bar(9),
'related'=> $this->article->related_keyword($article_title),
);
echo $article->News_News_ID;
$this->load->view('page/baiviet',$data);
}
I want to get article_title value from table article and put it in related_workword() function.
Update model The show a article model is to get article detail. The second model is to get all article related to the chosen article.
function show_a_article($id)
{
$query= $this->db->get_where('news_news', array('News_News_ID'=>$id));
return $query->row();
}
function related_keyword($rkey)
{
$sql="SELECT `News_News_ID`,`News_News_Headline`,`News_News_thumb1` FROM `news_news` WHERE `news_tags` like '%$rkey%' ORDER BY `News_News_ID` desc LIMIT 10";
$query= $this->db->query($sql);
return $query->result();
}
Update: Igot it
echo $this->article->show_a_article($id)->News_News_Headline;
Call model method from controller
$article_title = $this->article->get_article_details($id)->article_title;
Add function in model to return data.
function get_article_details($id)
{
$query = 'select article_title from table where col = '.$this->db->escape($id);
return $this->db->query($query)->row();
}

how to use activerecord count_all_result()? with specific column?

I want to get count of specific column of table using codeigniter active record, something like this:
$this->db->count_all_results('t.column1');
so how can i change this db_active_record function? for any help thanks.
To count a specific column, You would do this;
$query = $this->db->get_where('table', array('column' => $column);
return $query->num_rows(); // This is what you're after...
create the query in your model, load it in your controller and then just count the array items in the view.
for example:
Model:
function getAll() {
$this->db->select('*');
$this->db->order_by('user_ad', 'asc');
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return $query->result();
}
}
controller:
$this->load->model('Users');
$data['allusers'] = $this->Users->getAll();
this->load->view('users', $data);
and in your view you can use
count($allusers);

Translate from CodeIgniter Active Record model to Laravel Eloquent

I am a CodeIgniter trying to adopt Laravel, however, I have been having a lot of problems understanding how to use Eloquent.
I suspect that if I could figure out how to translate some of my CodeIgniter Model methods to Laravel Eloquent I might be able to get on better. Hopefully, this will help others with the same problem.
Could anybody please rewrite the following from CodeIgniter to Eloquent:
public function get_products($product_id = NULL, $category_id = NULL, $limit = NULL)
{
$this->db->select('*, product.id AS product_id');
$this->db->from('product');
$this->db->join('product_unit', 'product.source_unit_id = product_unit.id', 'left');
$this->db->join('stock_levels', 'product.stock_level_id = stock_levels.id', 'left');
$this->db->join('categories', 'product.category_id = categories.cat_id', 'left');
if(isset($product_id)) {
$this->db->where('product.id', $product_id);
}
if(isset($category_id)) {
$this->db->where('product.category_id', $category_id);
}
if(isset($limit)) {
$this->db->limit($limit);
}
#$this->db->order_by('categories.cat_name', 'ASC');
$this->db->order_by('categories.cat_name', 'ASC');
$this->db->order_by('product.name', 'ASC');
$query = $this->db->get();
return $query->result_array();
}
Here's an approximate version of your query, there should be some things to tweak, but I hope you get the idea:
public function get_products($product_id = NULL, $category_id = NULL, $limit = NULL)
{
$query = Product::leftJoin('product_unit', 'source_unit_id', '=', 'product_unit.id')
->leftJoin('stock_levels', 'stock_level_id', '=', 'stock_levels.id')
->leftJoin('categories', 'category_id', '=', 'categories.cat_id');
if(isset($product_id)) {
$query->where('product.id', $product_id);
}
if(isset($category_id)) {
$query->where('product.category_id', $category_id);
}
if(isset($limit)) {
$query->limit($limit);
}
#$this->db->order_by('categories.cat_name', 'ASC');
$query->orderBy('categories.cat_name', 'ASC');
$query->orderBy('product.name', 'ASC');
dd( $query->toSql() ); /// this line will show you the sql generated and die // remove it to execute the query
return $query->get()->toArray();
}

Resources