gpt4 book ai didi

php - 具有相同内容的自动跨行 (php/mysql/jQuery)

转载 作者:行者123 更新时间:2023-11-30 22:50:43 25 4
gpt4 key购买 nike

我正在将 mysql 中的数据显示到网页表中。
代码(我正在使用 wordpress):

    global $wpdb;
$Lists = $wpdb->get_results("select * from `price_record`;");
if($Lists== null)
{
echo"There is no price record yet!";
}
else
{
echo"<table class='priceTable' style='border:1px;border-collapse:collapse;'>";
echo"<thead>";
echo"<tr>";
echo"<th>Date</th>";
echo"<th>Category</th>";
echo"<th>Type</th>";
echo"<th>Price</th>";
echo"</tr>";
echo"</thead>";
echo"<tbody>";
foreach ($Lists as $List)
{
echo"<tr>";
echo"<td>".$List->Date."</td>";
echo"<td>".$List->Category."</td>";
echo"<td>".$List->Type."</td>";
echo"<td>".number_format($List->Price,2)."</td>";
echo"</tr>";
}
echo"</tbody>";
echo"</table>";
}

当前结果:

Date    Category    Type    Price   
---------------------------------
30/1 A T1 12
30/1 A T2 13
30/1 B T3 10
30/1 B T4 11
29/1 A T1 11

预期:

Date    Category    Type    Price   
---------------------------------
30/1 A T1 12
T2 13
B T3 10
T4 11
29/1 A T1 11

如何在显示表格时自动对日期、类别和类型进行分组?谢谢。

最佳答案

下面的代码应该可以做到。请注意,sql 查询中的 ORDER BY 很重要。

 global $wpdb;
$Lists = $wpdb->get_results("select * from `price_record` ORDER BY Date DESC;");
if($Lists== null)
{
echo"There is no price record yet!";
}
else
{
// Initiate dateElement
$dateElement = '';

echo"<table class='priceTable' style='border:1px;border-collapse:collapse;'>";
echo"<thead>";
echo"<tr>";
echo"<th>Date</th>";
echo"<th>Category</th>";
echo"<th>Type</th>";
echo"<th>Price</th>";
echo"</tr>";
echo"</thead>";
echo"<tbody>";
foreach ($Lists as $List)
{

if($List->Date == $dateElement) {

// If existing day, show a blank td
$dateElement = '&nbsp;';
} else {

// If new day, reset categoryElement and set the dateElement to the new day
$categoryElement = '';
$dateElement = $List->Date;
}

// If same category (and same day), show a blank td, otherwise show the category
$categoryElement = ($List->Category == $categoryElement) ? '&nbsp;' : $List->Category;

echo"<tr>";
echo"<td>".$dateElement."</td>";
echo"<td>".$categoryElement."</td>";
echo"<td>".$List->Type."</td>";
echo"<td>".number_format($List->Price,2)."</td>";
echo"</tr>";
}
echo"</tbody>";
echo"</table>";
}

关于php - 具有相同内容的自动跨行 (php/mysql/jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28228384/

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