How to add value to CodeIgniter query result?

5.5k views Asked by At

I am using CodeIgniter to generate some query results, and then in a second step using a foreach loop to run a method using values from the query result. What I would like to do now is to somehow combine the new variable with the original query. Here is the code so far:

$qryOriginal = $this->db->query("SELECT 1 as BAND, 
  tblappeals.Scenario, 
  tblappeals.Year, 
  tblappeals.var1, 
  tblappeals.var2 
  FROM tblappeals 
  WHERE tblappeals.Property = $pn ");

foreach ($qryOriginal->result() as $row)
{
$var3 = $this->getPhaseIn($calcyear, $row->var1, $row->var2);
    echo $var3; //HOW CAN I ADD THIS $var3 VALUE TO qryOriginal ???  
}

There must be an easy way to add this new value to the original query. I just don't know how to do it. I was thinking to use array_push(), but that doesn't seem to be the right function. Any ideas?

2

There are 2 answers

5
Ayman Safadi On BEST ANSWER

So you can't add it to the results directly; what you can do is store the results in a variable, and add the contents of $var3 to this variable. Like this:

$qryOriginal = $this->db->query("SELECT 1 as BAND, 
  tblappeals.Scenario, 
  tblappeals.Year, 
  tblappeals.var1, 
  tblappeals.var2 
  FROM tblappeals 
  WHERE tblappeals.Property = $pn ");

$qryOriginalResults = $qryOriginal->result();

foreach ($qryOriginal->result() as $row)
{
    $var3 = $this->getPhaseIn($calcyear, $row->var1, $row->var2);
    $qryOriginalResults[] = $var3;
}

Think about it, if you where to add $var3 to the actual results, you would create a never-ending for-loop.

0
Rajeev Ranjan On
  $qryOriginal = $this->db->query("SELECT 1 as BAND, 
  tblappeals.Scenario, 
  tblappeals.Year, 
  tblappeals.var1, 
  tblappeals.var2 
  FROM tblappeals 
  WHERE tblappeals.Property = $pn ");

$qryOriginalResults = $qryOriginal->result_array();
//result() gives result object
foreach ($qryOriginalResults as $row)
{
    $var3 = $this->getPhaseIn($calcyear, $row['var1'], $row['var2']);
    $qryOriginalResults[] = $var3;
}
$data['mydata'] = $qryOriginalResults;