从数据库连接相同的数组(数字)
Concatenate same array(number) from database
这里有点难以解释,但我会尽力包含所有相关信息。有不懂的可以问我
以上是我得到的数据,我希望将相同的数字(例如2016)组合起来显示如下。
这是我的代码。
<?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 } ?>
这是我的数据库
请问如何合并年份?我使用 "GROUP BY" 但不能,它隐藏了第二行数据。或者我应该比较价值并合并它?但我不知道该怎么做。请帮忙,谢谢。
像这样更新您的代码:
$years = mysql_query("SELECT YEAR(event_date) as years, GROUP_CONCAT(DATE_FORMAT(event_date,'%b') SEPARATOR '<br />') as months from news GROUP BY YEAR(event_date)");
while($page=mysql_fetch_array($years))
{ ?>
<div class="year"><?php echo $page['years']; ?></div>
<div class="month"><?php echo $page['months']; ?></div> <?php
} ?>
试试这个编码...
您将获得结果值并应用到您的 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 } }
} } ?>
这里有点难以解释,但我会尽力包含所有相关信息。有不懂的可以问我
以上是我得到的数据,我希望将相同的数字(例如2016)组合起来显示如下。
这是我的代码。
<?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 } ?>
这是我的数据库
请问如何合并年份?我使用 "GROUP BY" 但不能,它隐藏了第二行数据。或者我应该比较价值并合并它?但我不知道该怎么做。请帮忙,谢谢。
像这样更新您的代码:
$years = mysql_query("SELECT YEAR(event_date) as years, GROUP_CONCAT(DATE_FORMAT(event_date,'%b') SEPARATOR '<br />') as months from news GROUP BY YEAR(event_date)");
while($page=mysql_fetch_array($years))
{ ?>
<div class="year"><?php echo $page['years']; ?></div>
<div class="month"><?php echo $page['months']; ?></div> <?php
} ?>
试试这个编码...
您将获得结果值并应用到您的 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 } }
} } ?>