gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:47:16 24 4
gpt4 key购买 nike

我正在尝试从数据库表中提取 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);
}

关于php - 从 foreach 循环中的数组中提取数据库行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31698315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com