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

标签 php mysql arrays

所以,我有一个 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/

相关文章:

php - 简单的 javascript 隐藏内容功能不起作用

javascript - 使用 array.map() 计算二维数组列的平均值

php - 无法从表中获取所有用户名

php - 在查询中使用变量

mysql - 令人困惑的时间输出

php - 在 If 语句插入数据库

mysql - 将 MySQL 数据导入 Access 2010 时出现问题

python - 使用常量内存旋转 m 个位置左侧的 n 个元素的一维数组?

c - 将 strtok 的指针分配给二维结构数组中的指针,所有条目都应该是唯一的,但打印出来是相同的

php 数组未正确形成