0

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?

DanielAttard
  • 3,467
  • 9
  • 55
  • 104

2 Answers2

1

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.

Ayman Safadi
  • 11,502
  • 1
  • 27
  • 41
  • This makes sense. Let me give it a try and let you know how I make out. – DanielAttard Dec 04 '13 at 03:40
  • Hi again @Ayman. I still seem to be having a problem. I am trying to get the resulting `$qryOriginalResults` into an array using `$data['mydata'] = $qryOriginalResults->result_array();` but it does not seem to be working. What might I be doing wrong? – DanielAttard Dec 04 '13 at 04:28
  • @DanielAttard you just need to do `$data['mydata'] = $qryOriginalResults;` – Rajeev Ranjan Dec 04 '13 at 04:30
  • Thanks @RajeevRanjan, I tried your suggestion but I don't see the new `$var3` being added to the original `$qryOriginalResults`. When I output the array, I don't see the new value. – DanielAttard Dec 04 '13 at 04:43
  • @DanielAttard, what do you mean by *"not working"*? Do a `var_dump` on both `$qryOriginalResults` and `$var3`. Take a look and tell me what's wrong... – Ayman Safadi Dec 04 '13 at 09:49
1
  $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;
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41