Can i get a part of an variable? - php

Is it possible to get an part of a variable and run this to a where loop.
function rapport_detail_kosten($idKlant){
$this->db->from('Project');
$this->db->join('Kosten', 'Kosten.idProject = Project.idProject');
if ($idKlant > 0){
$this->db->where('idKlant', $idKlant);}
$query = $this->db->get();
$project = array();
foreach($query->result() as $row){
$project[] = $row->idProject;
}
$hoi = implode(",", $project);
print_r($hoi);
$this->db->select_sum('Prijs');
$this->db->from('Kosten');
$this->db->where('Kosten.idProject',$hoi);
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result();
}
else{
return false;
}
}
the $hoi has the value: 14,79,9,85,85,85,85,91,6
these are all id's but i would like to run this where statement only when the id is in this variable. So i would like to run id=6 but for example not id=7

You can use method where_in of codeigniter that search a value inside an array, in this case you can use directly $project, you don't need $hoi
try this:
$sum = 0;
$this->db->select_sum('Prijs');
$this->db->from('Kosten');
$this->db->where_in('Kosten.idProject',$project);
$query = $this->db->get();
foreach ($query->result() as $row){
$sum = $row->Prijs;
}
return($sum);
MANUAL

Related

How to get the data from database using foreach loop?

I am trying to get the data from database in CodeIgniter using foreach loop with the following query but I am getting the last result only.
public function get_tags($limit, $start, $tag_id)
{
$snippetstagdata = $this->getDataOneColumn("snippets_tags","tag_id",$tag_id);
foreach ($snippetstagdata as $getSnippet) {
$snippet_id = $getSnippet->snippet_id;
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('snippets');
$this->db->where("id", $snippet_id);
//$this->db->like('snippet_tags',$query);
$query = $this->db->get();
}
$result = $query->result();
$this->db->save_queries = false;
return $result;
}
Whereas getDataOneColumn function is
public function getDataOneColumn($table, $col1_name, $col1_value)
{
$this->db->where("$col1_name", $col1_value);
$query = $this->db->get("$table");
$result = $query->result();
$this->db->save_queries = false;
return $result;
}
This is incorrect:
foreach ($snippetstagdata as $getSnippet) {
$snippet_id = $getSnippet->snippet_id;
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('snippets');
$this->db->where("id", $snippet_id);
//$this->db->like('snippet_tags',$query);
$query = $this->db->get();
}
$result = $query->result();
You are looping through each item in your data and resetting $query each time but then only using $query once when you are out of the loop.
You need to move this line:
$result = $query->result();
inside the loop to get the correct output.
EDIT:
As pointed out, this isn't 100% either. $result should be an array and inside the loop the query should be pushed into the array like this:
$result[] = $query->result();
Then the final array ($result) can be looped through on its own.
Like #steve said you are replacing $query->result() each time you run the loop. so first you need to create an associative array to handle this. So your code should be:
$data = array();
foreach ($snippetstagdata as $getSnippet) {
$snippet_id = $getSnippet->snippet_id;
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('snippets');
$this->db->where("id", $snippet_id);
//$this->db->like('snippet_tags',$query);
$query = $this->db->get();
$result = $query->result();
$data[$snippet_id][] = $result;
}
Then you can get all the content from the $data array.
Hope that helps
You should try this
public function get_tags($limit, $start, $tag_id) {
$snippetstagdata = $this->getDataOneColumn("snippets_tags", "tag_id", $tag_id);
$review = [];
foreach ($snippetstagdata as $getSnippet) {
$snippet_id = $getSnippet->snippet_id;
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('snippets');
$this->db->where("id", $snippet_id);
//$this->db->like('snippet_tags',$query);
$query = $this->db->get();
$result[] = $query->result();
}
$this->db->save_queries = false;
return $result;
}

How to send the foreach data from the model to the controller

I am getting the Nameno from the database like below format.
$form_data['Nameno']='1,2,3,4';
$getName=$this->Home_model->getNameData($form_data['Nameno']);
Now I am passing that Nameno to the model to get the Name. So I used explode and passing to the foreach
Model
public function getNameData($NameId){
$getTempid=explode(",",$NameId);
$arrayData=[];
foreach ($getTempid as $row){
$where = array('is_tempActive' => 1,'Name_id'=>$row);
$this->db->select('templateName');
$this->db->from('tbl_templatename');
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
//print_r($result);
//$arrayData[]=$result;
}
return $result;
}
I need an output from my model like $getName='ABC,XYZ,POD,RED'
After suggested #Barmar answer
controller
$ids = explode(',', $form_data['Nameno']);
$names = array();
foreach ($ids as $id) {
$names[] = $this->Home_model->getNameData($id)->templateName;;
// print_r($names);
}
$getNames = implode(',', $names);
print_r($getNames);
Model
public function getNameData($tempId){
$where = array('is_tempActive' => 1,'Name_id'=>$row);
$this->db->select('templateName');
$this->db->from('tbl_templatename');
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;
}
Would you help me out in this issue?
You need to explode the original data into an array, call the function on each element, then implode the result back into a string.
$ids = explode(',', $form_data['Nameno']);
$names = array();
foreach ($ids as $id) {
$nameData = $this->Home_model->getNameData($id);
$names[] = $nameData[0]->templateName;
}
$getNames = implode(',', $names);
Instead of running multiple queries in a loop, you can use where_in() to get the results with one query.
public function getNameData($NameId){
$getTempid = explode(",", $NameId);
$this->db->select('Name');
$this->db->from('tbl_templatename');
$this->db->where('is_tempActive', 1);
$this->db->where_in($getTempid);
$query = $this->db->get();
$result = $query->result();
foreach ($query->result() as $row) {
$names[] = $row->Name;
}
return implode(',', $names);
}

explode using foreach inside codeigniter model not working

I have a code which separate , from the string using explode.
Now the problem is the foreach loop only iterating once. Am using this code in codeigniter model.
My code is,
//Model
$product_id = '1,2,3';
$products = explode(',', $product_id);
foreach($products as $products_id) {
echo $products_id.'<br><br>'; //output: 1
$query = $this->db->query('SELECT * FROM tbl_products WHERE products_id = "'.$products_id.'" AND products_status = "1"');
$count = $query->num_rows();
if($count > 0) {
return $result = $query->result_array();
}
else {
return 0;
}
}
The output for the above code is 1.
The other digit 2 and 3 is missing. Am I doing anything wrong. Please help me out. I can't figure it out.
To correct it, you could do this
$product_id = '1,2,3';
$products = explode(',', $product_id);
$data = [];
foreach($products as $products_id) {
echo $products_id.'<br><br>'; //output: 1
$query = $this->db->query('SELECT * FROM tbl_products WHERE products_id = "'.$products_id.'" AND products_status = "1"');
$count = $query->num_rows();
if($count > 0) {
$data = array_merge($data, $query->result_array());
}
}
if(empty($data)){
return 0;
}
return $data;
Which avoids exiting in the original if statement you return on more then 0 results or 0 results, so basically on the first iteration of the loop.
But, that said, possibly a better way to fix it is this
$product_id = '1,2,3';
$query = $this->db->query('SELECT * FROM tbl_products WHERE products_id IN( ? ) AND products_status = "1"',$product_id);
$count = $query->num_rows();
if($count > 0) {
return $query->result_array();
}else{
return 0;
}
Using IN you can avoid the foreach,explode and additional query calls, also you should use prepared queries. This should work with numbers, but strings still need to be wrapped in quotes, 'IN( "string","string", "string" )' or $string = '"string","string","string"'; as the input, which may complicate things for prepared queries.
Everthing is fine in your code .If you want get all data , Then you should return all in array . check the code :
$product_id = '1,2,3';
$products = explode(',', $product_id);
$data = array();
foreach($products as $products_id) {
echo $products_id.'<br><br>'; //output: 1
$query = $this->db->query('SELECT * FROM tbl_products WHERE products_id = "'.$products_id.'" AND products_status = "1"');
$count = $query->num_rows();
if($count > 0) {
$data[]= $query->result_array();
}
else {
$data[]= 0;
}
}
return $data;
On $data .. You can do any action with $data as like you want.

Return the highest value from model to view

I want to know how to compare these 2 outputs http://prntscr.com/bx7ay9 and return the highest value in the array ?
MY MODEL CODES
$this->db->select('additional');
$this->db->from('order_detail');
$this->db->where('order_id',$id);
$query = $this->db->get();
foreach ($query->result() as $row)
{
$return[] = $row->additional;
}
return $return;
Just use select_max in query to get highest value and use row() to fetch single row without using foreach loop as
$this->db->select_max('additional AS max_value');
$this->db->from('order_detail');
$this->db->where('order_id', $id);
$query = $this->db->get();
$ret = $query->row();
return $ret->max_value;
$this->db->select('additional');
$this->db->from('order_detail');
$this->db->where('order_id',$id);
$query = $this->db->get();
foreach ($query->result() as $row)
{
$return[] = max($row->additional);
}
return $return;
Just used select_max of Codeigniter Query Bulder Class.
Model,
public function get_max ($id){
$this->db->select_max('additional');
$this->db->where('order_id',$id);
$query = $this->db->get('order_detail');
return $query->row();
}
In Controller,
$max = $this->Model->get_max($id);
echo $max['additional']; // Display and see the value.
If you want to get the max number then use select_max(),
$this->db->select_max('additional');
$this->db->from('order_detail');
$this->db->where('order_id',$id);
$query = $this->db->get();
if($query->num_rows()){// check if data is not empty
return $query->row()->additional;
}
return false;// you can return false here
And if you want to get the result array for another use then, you can use max() with $this->db->select('additional'); with your code to get the max number in array.

How to pass multiple array of Id in where condition codeigniter?

I want to pass multiple id in where condition how to do it?
A query fetches org_id from database.
now I want to pass that in my where condition, so how to do it?
I tried foreach loop:
Below is my code:
$devices = $this->activation_m->get_activated_devices();
$org_id = array(); // create org_id array
foreach ($devices as $row)
{
$org_id[]= $row['org_id']; // assign ids into array
//$companys =$this->data['devices'] = $this->activation_m->get_company($org_id); // don't call again and again
}
//Now $org_id is array
$companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
echo $this->db->last_query();
Model Code
public function get_activated_devices()
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.site_key = activation.site_key');
$this->db->from('activation');
$query =$this->db->get();
$result = $query->result_array();
return $result;
}
public function get_company($org_id)
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
$this->db->where('company.id IN',(implode(',',$org_id))); // see the change here
$this->db->from('company');
$query =$this->db->get();
$result = $query->result_array();
return $result;
}
now currently my query passes only one org_id , I want to pass all the org_id I get from my first query.
You can use where_in from active-records codeigniter
as
$devices = $this->activation_m->get_activated_devices();
$org_id = array();
foreach ($devices as $row)
{
$org_id[] = $row['org_id'];
}
$companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
if( $companys->num_rows() > 0 )
{
echo "<pre>";
print_r( $companys->result());
echo "</pre>";
}
And for Model
public function get_company($org_ids = array())
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
$this->db->where_in('company.id', $org_ids ); //this is condition
$this->db->from('company');
return $this->db->get();
}
You can use codeigniter's $this->db->or_where() for the purpose. Just traverse the array of organization id's and apply or_where conditions.
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
foreach($org_id as $org)
{ // where $org is the instance of one object of active record
$this->db->or_where('company.id',$org);
}
$this->db->from('company');
$query =$this->db->get();
$result = $query->result_array();
return $result;
Another way to do this is to make a custom query by traversing the array like this and appending where clauses in string.

Categories