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

时间:2015-07-29 10:48:02

标签: php mysql

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

AU $ 11.00

AU $ 19.00

AU $ 32.00

AU $ 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>';

等等。目前我在循环的每次传递中得到所有四个值

4 个答案:

答案 0 :(得分:0)

这样做:

$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>'; 

现在foreach将使用正确的货币push $currency数组{{1}}中的值。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

这样做。它会完美地完成它

  $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']);
  ?>

答案 3 :(得分:-1)

在为货币分配价格后关闭foreach循环。

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