gpt4 book ai didi

php - 连接数据库中的相同数组(数字)

转载 作者:行者123 更新时间:2023-11-29 11:46:58 24 4
gpt4 key购买 nike

这里解释起来有点困难,但我会尽力包含所有相关信息。不懂的可以问我。

enter image description here

上面是我得到的数据,我希望将相同的数字(例如 2016)组合起来显示如下。

enter image description here

这是我的代码。

<?php
$years = mysql_query("SELECT DISTINCT YEAR(event_date) as years, DATE_FORMAT(event_date,'%b') as months from news WHERE status<>'deleted' and status<>'draft' ORDER BY years DESC");

while($page=mysql_fetch_array($years))
{ ?>
<div class="date">
<div class="year"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $page['years'] ?>&month=<?php echo $page['months'] ?>"><?php echo $page['years']; ?></a></div>

<div class="month"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $page['years'] ?>&month=<?php echo $page['months'] ?>"><?php echo $page['months']; ?></a></div>
</div>
<?php } ?>

这是我的数据库 enter image description here enter image description here

我可以知道如何合并年份吗?我使用“GROUP BY”但不能,它隐藏了第二行数据。或者我应该比较该值并将其合并?但我不知道该怎么做。请帮忙,谢谢。

最佳答案

尝试这个编码...

您将获得结果值并应用到您的 html 内容中。

     <?php
$years = mysql_query("SELECT DISTINCT YEAR(event_date) as years, DATE_FORMAT(event_date,'%b') as months from news");

$result = array();

while($page=mysql_fetch_array($years))
{

$year = $page['years'];
$month = $page['months'];

$result[$year][] = $page['months'];

}

if(isset($result)) {

foreach($result as $key=>$year_array) { ?>

<div class="year"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $key; ?>&month=<?php echo $month; ?>"><?php echo $key; ?></a></div>
<?php
if(isset($year_array) && count($year_array) != 0) {
foreach($year_array as $month) { ?>

<div class="month"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $key; ?>&month=<?php echo $month; ?>"><?php echo $month; ?></a></div>

<?php } }
} } ?>

关于php - 连接数据库中的相同数组(数字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758359/

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