gpt4 book ai didi

php - 将 SQL 数据库中的数据显示为拓扑

转载 作者:行者123 更新时间:2023-11-29 03:26:39 25 4
gpt4 key购买 nike

我有以下情况:

Site_A   Link_AB   Site_B    Link_BC   Site_C
001 001-002 002 002-003 003
001 001-002 002 002-004 004
001 001-005 005 005-006 006

我想将其显示为:

001      001-002   002       002-003   003
002-004 004
001-005 005 005-006 006

有机会吗?

谢谢

最佳答案

您可以在 PHP 中执行此操作,并使 SQL 保持简单和标准:

$query = "SELECT   Site_A, Link_AB, Site_B, Link_BC, Site_C 
FROM sitelinks
ORDER BY 1, 2, 3, 4, 5";
$result = $mysqli->query($query) or die($mysqli->error());

// maintain an array with previous values
$previous = array();
echo "<table border=1>";
while ($row = $result->fetch_assoc()) {
if (!count($previous)) {
// display column headers
echo "<tr>";
foreach($row as $key => $value) {
echo "<th>" . htmlentities($key) . "</th>";
}
echo "</tr>";
// initialise $previous with empty values for each column:
$previous = array_fill_keys(array_keys($row), '');
}
echo "<tr>";
foreach($row as $key => $value) {
// display value only when different from previous for this column:
echo "<td>".($value !== $previous[$key] ? htmlentities($value) : '')."</td>";
}
// copy current values into $previous
$previous = $row;
echo "</tr>";
}
echo "</table>";

输出:

enter image description here

关于php - 将 SQL 数据库中的数据显示为拓扑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35141124/

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