【问题标题】:Concatenate same array(number) from database从数据库连接相同的数组(数字)
【发布时间】:2016-01-13 04:21:50
【问题描述】:

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

上面是我得到的数据,我希望将相同的数字(例如 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”但不能,它隐藏了第二行数据。或者我应该比较价值并结合它?但我不知道该怎么做。请帮忙,谢谢。

【问题讨论】:

    标签: php mysql database row concatenation


    【解决方案1】:

    试试这个编码...

    您将获得结果值并应用于您的 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 } }
             } }   ?>
    

    【讨论】:

    • 收到错误信息 --> 函数名必须是字符串
    • 哪一行显示错误并发布更新的编码?
    • 抱歉是 print_r($result);我已经更新了编码
    • 奥普斯。我也没有意识到 print_r。现在我得到了 Array ( )
    • 检查您的查询返回结果。
    【解决方案2】:

    像这样更新你的代码:

    $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 
    } ?>
    

    【讨论】:

    • 很抱歉,因为我没有完全包含我的代码。我需要通过单击月份来过滤信息。所以我的代码如上所示(已编辑)。当我单击要过滤的月份时,我的 URL 反映了 index.php?year=2016&amp;month=Feb&lt;br%20/&gt;Jan
    • 检查代码是否获取所需的格式。 html你可以根据你的要求绑定
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多