简体   繁体   English

PHP / MYSQL年月表,用于新闻存档

[英]PHP/MYSQL Year Month table for news archive

I am creating a news archive for my site and want to create an overview page from the following DB table: 我正在为我的站点创建新闻档案,并希望从以下数据库表中创建概述页面:

id - Unique identifier
newsDate - in a format XXXX-XX-XX
title - News Item title
details - News item
photo - News Item Photo
caption - News Item Photo caption
update - Timestamp for record

The news on the site is current but I hope to add some data from years gone by over the next few months and years. 该网站上的新闻是最新的,但我希望在接下来的几个月和几年中增加一些过去几年的数据。

What I want to do is create a new line for each year and highlight the month which corresponds to a record in the DB table, similar to that below. 我想要做的是为每年创建一个新行,并突出显示与数据库表中的记录对应的月份,类似于下面的记录。

2002 JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
2004 JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
2005 JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
2008 JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC

Any help or advice would be much appreciated 任何帮助或建议,将不胜感激

Stick with the table layout you've got, then for the overview page select newsDate as year and month and group by that (possibly with count if you want to show how many articles there are in each month), then iterate through it for you lines. 坚持使用现有的表格布局,然后在概述页面中选择newsDate作为年和月,并按其进行分组(如果要显示每个月有多少篇文章,可以进行计数),然后为您进行遍历线。 Should be fairly basic to do 应该做的很基本

TEST Full Example 测试完整示例

CSS 的CSS

#Decor 
{
    width:200px;
}

#Decor, #Decor ul {
    list-style-type: none;
    margin-left:25px;
    margin-bottom:5px;
    padding-left:20px;
    cursor:pointer;
}

.handle { 
    background: transparent url( /images/tree-handle.png ) no-repeat left top; 
    display:block; 
    float:left;
    width:10px; 
    height:10px;
    cursor:pointer;
}

.closed { background-position: left top; }
.opened { background-position: left -10px; }

PHP File PHP文件

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link href="CSS/treeview.css" rel="stylesheet" />
    <script src="scripts/jquery-1.11.1.min.js"></script>
    <script>
        $(function () {
            //start the tree in an autocollapsed state
            $('#Decor ul').hide(400);

            $('#Decor li').on('click', function (e) {
                e.stopPropagation(); // prevent links from toggling the nodes
                $(this).children('ul').slideToggle();
            });

            // This code opens all hyperlinks in a new window 
            // and avoids anchors
            $('#Decor a').not('[href="#"]').attr('target', '_blank');
        });
    </script>
</head>

<?php
define("DB_USER","");
define("DB_PASS","");
define("DB_HOST","");
define("DB_NAME","");

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(mysqli_errno());
$s="SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
$sql_result=mysqli_query($link,$s);
?>
<ul id="Decor">
<?php
while($row=mysqli_fetch_array($sql_result))
    {
    $datetime=strtotime($row['date_upload']);
    $tday = date("Y", $datetime);
    $count = $row['itemCount'];
    ?>
     <li class="level1"><?php echo "<u><strong>{$tday} ({$count})</strong></u><br>"; ?>
    <?php
        $s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";
        $q=mysqli_query($link,$s1);
        while($month=mysqli_fetch_row($q))
        {
        ?>
            <ul>
               <li><a href="test.php?date=<?php echo $month[5]; ?>"><?php echo date("F",strtotime($month[5])); ?></a></li>
            </ul>
            <?php
        }
    echo "<br>";
    }
?>
</ul>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM