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;
}
Related
I am trying to limit the result for pagination in foreach loop with the following query but instead of limiting I am getting all the results with the following query.
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->where("id", $snippet_id);
$this->db->select('*');
$this->db->from('snippets');
$this->db->limit($limit, $start);
$query = $this->db->get();
$result[] = $query->result();
}
$this->db->save_queries = false;
return $result;
}
Whereas getDataOneColumn
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;
}
Updated
I hope this will help you
public function get_tags($params = array(), $tag_id)
{ $result = array();
$snippetstagdata = $this->getDataOneColumn("snippets_tags","tag_id",$tag_id);
foreach ($snippetstagdata as $data) {
$this->db->where('id',$data['snippet_id']);
if(isset($params) && !empty($params))
{
$this->db->limit($params['limit'], $params['offset']);
}
$result[$data['snippet_id']] = $this->db->get('snippets')->result_array();
}
return $result;
}
and
public function getDataOneColumn($table, $col1_name, $col1_value)
{
return $this->db->get_where($table,array($col1_name=>$col1_value))->result_array();
}
Your approach to query your database with the help of the foreach loop is wrong. The correct approach would be using a join in the query:
from your function getDataOneColumn() you are only using the snippet_id values, which you can use to create the join:
public function getDataOneColumn($table, $col1_name, $col1_value, $limit, $start)
{
$this->db->select("t1.*,t2.*")
->where("t1.$col1_name", $col1_value)
->join("snippets t2","t1.snippet_id=t2.id")
->limit($limit, $start);
$query = $this->db->get("$table t1");
$result = $query->result();
return $result;
}
note: in your example $tag_id=$col1_value, so use whatever variable name fits better.
Your second function get_tags() is then not necessary at all.
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);
}
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.
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.
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