在foreach循环中从数组中提取数据库行


extracting database rows from an array within a foreach loop

我试图从数据库表中提取4行产品价格。

非盟

11.00美元非盟

19.00美元非盟

32.00美元非盟

52.00美元

当我执行下面的代码时,我只从数据库中检索最后一个值AU$ 52.00。

Array ( [CURRENCY] => AU$ 52.00 )

这是我的代码

$sql = DB::getInstance()->query('SELECT monthly FROM products');
foreach($sql->results() as $row) {
    $price = '<small>AU$</small> ' . number_format($row->monthly, 2) . '<br>';
}
$currency = array(
    'CURRENCY' => $price,
);
print_r($currency['CURRENCY']);
die();
?>

可能忽略了一些简单的事情,但我的头现在都是痛苦和扭曲的。任何帮助都是有用的。提前感谢

编辑问题

这将是一个货币转换器,所以下面的代码可能有助于它如何工作

$currencies = array('AUD', 'USD', 'GBP', 'EUR', 'THB');
$get_cur = Input::get('currency');
if(isset($get_cur) === true && in_array($get_cur, $currencies) === true) {
    $_SESSION['currency'] = $get_cur;
} else if (isset($_SESSION['currency']) === false) {
    $_SESSION['currency'] = 'AUD';
}
include 'currency/' . $_SESSION['currency'] . '.php';

从每一个联系的货币将进入澳元,美元,英镑等

<a href="?currency=AUD">

链接到AUD.php,这是原始问题的代码。这就是为什么我需要链接

$currency['CURRENCY']

这将是每个货币数组中定义的名称。

希望有帮助

编辑问题#2

<?php
// Currency Covert Class
class CurrencyConverter {
    public function convert($amount, $from, $to) {
        $data = file_get_contents('https://www.google.com/finance/converter?a=' . $amount . '&from=' . $from . '&to=' . $to);
        preg_match("/<span class=bld>(.*)<'/span>/", $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 && 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()->query('SELECT monthly FROM products');
foreach($sql->results() as $row) {
    $currency = array(
        'CURRENCY' => '<small>AU$</small> ' . number_format($row->monthly, 2),
    );
}
// This is USD.php  
$converter = new CurrencyConverter;
$sql = DB::getInstance()->query('SELECT monthly FROM products');
foreach($sql->results() as $row) {
    $currency = array(
        'CURRENCY' => '<small>US$</small> ' . number_format($converter->convert($row->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 '<p>' . $currency['CURRENCY'] . '<span>' . $lang['PER_MONTH'] . '</span></p>'; 
?>

编辑问题#3

我想我已经发现了那个被我忽略的问题。它位于嵌套的foreach循环 中
$sql = DB::getInstance()->query('SELECT * FROM products ORDER BY id ASC');
foreach($sql->results() as $row) {
    if(escape($row->popular)) {
        echo '<div class="col-md-3 price-table ' . escape($row->popular) . '">';
    } else {
    echo '<div class="col-md-3 price-table">';
}
?>
<h3><strong><?php echo escape($row->name) ?></strong><br><?php echo $lang['PACKAGE'] ?></h3>
<?php
///HERE IS WHERE IT GETS PULLED IN FROM AUD.php, USD.php, GBP.php, etc etc///
///START OF AUD.php///  
$sql = DB::getInstance()->query('SELECT monthly FROM products');
$currency = array();
$currency['CURRENCY'] = array();
foreach($sql->results() as $row) {
    array_push($currency['CURRENCY'],  '<small>AU$</small> ' . number_format($row->monthly, 2));
}
///END OF AUD.php///
echo '<p>' . implode($currency['CURRENCY'], ' <span>' . $lang['PER_MONTH'] . '</span>') . '</p>';

数组在

下面
Array
(
    [0] => AU$ 11.00
    [1] => AU$ 19.00
    [2] => AU$ 32.00
    [3] => AU$ 52.00
)

我需要它这样能够转换货币。所以我需要这个;

echo '<p>' . implode($currency['CURRENCY'], ' <span>' . $lang['PER_MONTH'] . '</span>') . '</p>';

在循环的第一次循环中回显如下

echo '<p> AU$ 11.00 <span>Per Month</span></p>';

并在循环的第二次循环中在下面回显

echo '<p> AU$ 19.00 <span>Per Month</span></p>';

以此类推。目前,我在每次循环

时获得所有四个值

您没有在循环中添加值,因此您只获得$price

中的最后一个值
foreach($sql->results() as $row) {
  $price = '<small>AU$</small> ' . number_format($row->monthly, 2) . '<br>';
  $currency[]=array('CURRENCY' => $price);
}

这样做。它会做得很好

  $sql = DB::getInstance()->query('SELECT monthly FROM products');
  $currency = array();
  $i=0;
  foreach($sql->results() as $row) {
      $price = '<small>AU$</small> ' . number_format($row->monthly, 2) . '<br>';
      $currency['CURRENCY'][$i] = $price;
      $i++;
  }

  print_r($currency['CURRENCY']);
  ?>

这样做:

$currency = array();
$currency['currency'] = array()
foreach($sql->results() as $row) {
    array_push($currency['currency'],  '<small>AU$</small> ' . number_format($row->monthly, 2));
}
echo '<p>' . implode($currency['CURRENCY'], ' <span>' . $lang['PER_MONTH'] . '</span><br />') . </p>'; 

现在foreachpush的一个值放入数组$currency中,并使用正确的货币。

将价格赋给货币后关闭foreach循环

$currency = array();
foreach($sql->results() as $row) {
    $price = '<small>AU$</small> ' . number_format($row->monthly, 2) . '<br>';
$currency[] = $price;
}