gpt4 book ai didi

php - 在 PHP 中按列值显示 MySQL 结果

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

我有一个 MySQL 查询,它将几个表连接在一起以收集数据以供显示。我的目标是通过名为“project_id”的列显示数据。示例:

[project_id][project_name][order_date][ordered_by]
[31][project_a][2013-04-15][1]
[31][project_a][2013-04-16][1]
[31][project_a][2013-04-21][1]
[41][project_c][2013-02-21][1]
[41][project_c][2013-02-27][1]

所以我的问题是,我不确定如何循环遍历数据数组以显示project_id一次,然后显示下面的数据,因为我将使用jQuery按project_id切换/显示组数据

这是我的 mysql 查询:

SELECT p.user_customer_id,
p.name AS 'project_name',
p.project_id,
o.order_id,
o.order_date,
o.order_tech,
pro.name,
pro.purchase_price,
pro.sell_price
FROM project AS p
JOIN `order` AS o ON p.project_id = o.project_id
JOIN products_in_orders AS pir ON o.order_id = pir.order_id
JOIN product AS pro ON pir.product_id = pro.product_id
WHERE p.user_customer_id = 51
ORDER BY p.project_id ASC

最佳答案

类似这样的吗?:

<?php

/** @var $mysqli mysqli */
$result = $mysqli->query($yourOrderedQuery);

while ($row = $result->fetch_object())
{
// first row with this project_name
if ($project_name != $row->project_name)
{
$project_name = $row->project_name; // remember we have seen this project (now)
// display $row->project_id, etc.
}
// second or later row of with this project_name
else
{
// display data of project from $row->order_date, etc.
}
}

关于php - 在 PHP 中按列值显示 MySQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18539344/

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