title: extracting database rows from an array within a foreach loop
body: <p>I am trying to extract 4 rows of product prices from a database table.</p>
  
  <p>AU$ 11.00</p>
  
  <p>AU$ 19.00</p>
  
  <p>AU$ 32.00</p>
  
  <p>AU$ 52.00</p>
  
  <p>When i execute the code below i only retrieve the last value AU$ 52.00 from the database.</p>
  
  <pre><code>Array ( [CURRENCY] =&gt; AU$ 52.00 )
  </code></pre>
  
  <p>This is my code
      
  
  <pre><code>$sql = DB::getInstance()-&gt;query('SELECT monthly FROM products');
  
  foreach($sql-&gt;results() as $row) {
      $price = '&lt;small&gt;AU$&lt;/small&gt; ' . number_format($row-&gt;monthly, 2) . '&lt;br&gt;';
  }
  
  $currency = array(
      'CURRENCY' =&gt; $price,
  );
  
  print_r($currency['CURRENCY']);
  die();
  ?&gt;
  </code></pre>
  
  <p>Probably overlooking somthing simple but my head is all bitter and twisted now. Any help will be useful. Thanks in advance</p>
  
  <p>EDIT TO QUESTION</p>
  
  <p>This will be a currency converter so the code below may help with how it will work</p>
  
  <pre><code>$currencies = array('AUD', 'USD', 'GBP', 'EUR', 'THB');
  
  $get_cur = Input::get('currency');
  
  if(isset($get_cur) === true &amp;&amp; in_array($get_cur, $currencies) === true) {
      $_SESSION['currency'] = $get_cur;
  } else if (isset($_SESSION['currency']) === false) {
      $_SESSION['currency'] = 'AUD';
  }
  
  include 'currency/' . $_SESSION['currency'] . '.php';
  </code></pre>
  
  <p>From each linked currency will go to AUD, USD, GBP etc etc</p>
  
  <pre><code>&lt;a href="?currency=AUD"&gt;
  </code></pre>
  
  <p>Linked to AUD.php which is the code from the original question. This is why i need to link </p>
  
  <pre><code>$currency['CURRENCY']
  </code></pre>
  
  <p>This will be the defined name in the array of each currency.</p>
  
  <p>Hope this helps</p>
  
  <p>EDIT TO QUESTION #2</p>
  
  <pre><code>&lt;?php
  // Currency Covert Class
  class CurrencyConverter {
  
      public function convert($amount, $from, $to) {
          $data = file_get_contents('https://www.google.com/finance/converter?a=' . $amount . '&amp;from=' . $from . '&amp;to=' . $to);
  
          preg_match("/&lt;span class=bld&gt;(.*)&lt;\/span&gt;/", $data, $converted);
  
          $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
  
          return $converted;
      }
  
  }
  ////////////////////////
  
  $currencies = array('AUD', 'USD', 'GBP', 'EUR', 'THB');
  
  $get_lang = Input::get('currency');
  
  if(isset($get_lang) === true &amp;&amp; in_array($get_lang, $currencies) === true) {
      $_SESSION['currency'] = $get_lang; //Get's the currency from whichever currency is selected from the $currencies = array('AUD', 'USD', 'GBP', 'EUR', 'THB');
  } else if (isset($_SESSION['currency']) === false) {
      $_SESSION['currency'] = 'AUD'; //If no currency selected, then default is AUD
  }
  
  include 'currency/' . $_SESSION['currency'] . '.php'; //Selects the currenct .php file eg; 'AUD', 'USD', 'GBP', 'EUR', 'THB'
  
  //If AUD.php then it $_GET currency eg; ?currency=AUD, ?currency=USD, ?currency=GBP
  
  // This is AUD.php  (DEFAULT CURRENCY)
  $sql = DB::getInstance()-&gt;query('SELECT monthly FROM products');
  
  foreach($sql-&gt;results() as $row) {
      $currency = array(
          'CURRENCY' =&gt; '&lt;small&gt;AU$&lt;/small&gt; ' . number_format($row-&gt;monthly, 2),
      );
  }
  
  // This is USD.php  
  $converter = new CurrencyConverter;
  
  $sql = DB::getInstance()-&gt;query('SELECT monthly FROM products');
  
  foreach($sql-&gt;results() as $row) {
      $currency = array(
          'CURRENCY' =&gt; '&lt;small&gt;US$&lt;/small&gt; ' . number_format($converter-&gt;convert($row-&gt;monthly, 'AUD', 'USD'), 2),
      );
  }
  
  //So you can see that all the currency .php files have the key array word of 'CURRENCY', and this is how it will work from the $_SESSION['currency'] to the    $currency['CURRENCY'] array
  
  //Then simply echo out $currency['CURRENCY'] and it will convert to each     $currencies = array('AUD', 'USD', 'GBP', 'EUR', 'THB');
  
  echo '&lt;p&gt;' . $currency['CURRENCY'] . '&lt;span&gt;' . $lang['PER_MONTH'] . '&lt;/span&gt;&lt;/p&gt;'; 
  ?&gt;
  </code></pre>
  
  <p>EDIT TO QUESTION #3</p>
  
  <p>I think i have found the issue, overlooked by me. It is inside a nested foreach loop</p>
  
  <pre><code>$sql = DB::getInstance()-&gt;query('SELECT * FROM products ORDER BY id ASC');
  
  foreach($sql-&gt;results() as $row) {
      if(escape($row-&gt;popular)) {
          echo '&lt;div class="col-md-3 price-table ' . escape($row-&gt;popular) . '"&gt;';
      } else {
      echo '&lt;div class="col-md-3 price-table"&gt;';
  }
  ?&gt;
  
  &lt;h3&gt;&lt;strong&gt;&lt;?php echo escape($row-&gt;name) ?&gt;&lt;/strong&gt;&lt;br&gt;&lt;?php echo $lang['PACKAGE'] ?&gt;&lt;/h3&gt;
  
  &lt;?php
  
  ///HERE IS WHERE IT GETS PULLED IN FROM AUD.php, USD.php, GBP.php, etc etc///
  
  ///START OF AUD.php///  
  $sql = DB::getInstance()-&gt;query('SELECT monthly FROM products');
  
  $currency = array();
  $currency['CURRENCY'] = array();
  
  foreach($sql-&gt;results() as $row) {
      array_push($currency['CURRENCY'],  '&lt;small&gt;AU$&lt;/small&gt; ' . number_format($row-&gt;monthly, 2));
  }
  ///END OF AUD.php///
  
  echo '&lt;p&gt;' . implode($currency['CURRENCY'], ' &lt;span&gt;' . $lang['PER_MONTH'] . '&lt;/span&gt;') . '&lt;/p&gt;';
  </code></pre>
  
  <p>The arrays is below</p>
  
  <pre><code>Array
  (
      [0] =&gt; AU$ 11.00
      [1] =&gt; AU$ 19.00
      [2] =&gt; AU$ 32.00
      [3] =&gt; AU$ 52.00
  )
  </code></pre>
  
  <p>I need it like this to be able to convert the currencies. So i need this;</p>
  
  <pre><code>echo '&lt;p&gt;' . implode($currency['CURRENCY'], ' &lt;span&gt;' . $lang['PER_MONTH'] . '&lt;/span&gt;') . '&lt;/p&gt;';
  </code></pre>
  
  <p>To echo out below in the first pass of the loop</p>
  
  <pre><code>echo '&lt;p&gt; AU$ 11.00 &lt;span&gt;Per Month&lt;/span&gt;&lt;/p&gt;';
  </code></pre>
  
  <p>And echo out below in the second pass of the loop</p>
  
  <pre><code>echo '&lt;p&gt; AU$ 19.00 &lt;span&gt;Per Month&lt;/span&gt;&lt;/p&gt;';
  </code></pre>
  
  <p>And so on. At the moment i am getting all four values on every pass of the loop</p>