gpt4 book ai didi

php - 使用多个 MySql 列创建内联数组

转载 作者:行者123 更新时间:2023-11-29 13:09:55 25 4
gpt4 key购买 nike

所以,我有一个 MySQL 查询,它返回多个结果(在本例中,产品的不同货币)。它们在表中显示如下:

Product Name  |  Euros  |  Dollars  |  GBP  |  Stock?
_______________________________________________
Acme Produce | 34.00 | 52.00 | 30.00 | "In Stock"
MoreProducts | 153.00 | 160.00 | 144.00 | "In Stock"

我希望能够在 PHP 中将其全部打印在一行中(当你在表格中包含信息时听起来很愚蠢,我知道,但这是有原因的),如下所示

"You have a number of products including 'Acme Produce', currently selling for E34.00, $52.00, £30.00 in stock, and 'MoreProducts', currently selling for E153.00, $160.00, £144.00 in stock which you have not looked at for a while"

我如何为此构造一个字符串?!

我使用以下方式从 MySQL 获取产品:

$query_productDetails = sprintf("SELECT * FROM ProductsView")
$query_limit_productDetails = sprintf("%s LIMIT %d, %d", $query_productDetails, $startRow_productDetails, $maxRows_productDetails);
$productDetails = mysql_query($query_limit_productDetails, $Products) or die(mysql_error());
$row_productDetails = mysql_fetch_assoc($productDetails);

if (isset($_GET['totalRows_productDetails'])) {
$totalRows_productDetails = $_GET['totalRows_productDetails'];
} else {
$all_productDetails = mysql_query($query_productDetails);
$totalRows_productDetails = mysql_num_rows($all_productDetails);
}

然后获取 <?php echo $row_productDetails['GBP']; ?> 中的各个列(例如)。

如何将此信息放入数组中,以便我可以将其以所需的格式组合在一起?

最佳答案

你不能这样做吗:

$lines = array();
foreach ($row_productDetails) {
$lines[] = "'{$row_productDetails['NAME']}', currently selling for E{$row_productDetails['EUR']}, ${$row_productDetails['USD']}, £{$row_productDetails['GBP']} {$row_productDetails['InStock']}";
}

var_dump(implode(", and "));

关于php - 使用多个 MySql 列创建内联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22231595/

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