简体   繁体   English

基于学年的PHP和MySQL存档

[英]PHP & MySQL Archive based on School Year


I need help creating an archive of posts based on school year. 我需要帮助,可以根据学年创建帖子存档。 I've managed to create a normal archive with the help of this website . 我已设法借助该网站创建了一个普通存档。 I'm still a PHP newbie and feel like I'm missing something that might be obvious to the average back end developer. 我仍然是PHP的新手,觉得我缺少一些对于一般后端开发人员来说显而易见的东西。

Here is a tree view of what my archive currently looks like: 这是我的存档当前外观的树状视图:

  2013

  April
      Post Title
  March
      Post Title
  February
      Post Title
      Post Title
      Post Title
  January
      Post Title
      Post Title
      Post Title

  2012

  December
      Post Title
      Post Title
      Post Title
  November
      Post Title
      Post Title
      Post Title

I can't help but feel like it's almost there, but I don't know how to get the site to know which date ranges to show. 我忍不住想快要到达了,但是我不知道如何让网站知道要显示哪个日期范围。 One thing I tried was adding this query: 我尝试的一件事是添加此查询:

$result = mysql_query("SELECT * FROM table WHERE table_date BETWEEN '2012-08' and '2013-07'") or die(mysql_error());

It worked and it's almost what I want... but I don't want to add queries for every school year til the end of time! 它奏效了,这几乎是我想要的...但是我不想在每个学年之前添加查询,直到时间结束! There has got to be a better way to add these ranges than manually doing it for every year. 与每年手动进行添加相比,必须有一种更好的方法来添加这些范围。

Edit : Here is my current code. 编辑 :这是我当前的代码。 It's giving me the error "Undefined index: hound_date" 它给我错误“未定义的索引:hound_date”

$result = mysql_query("SELECT CONCAT(   YEAR(hound_date-INTERVAL 7 MONTH),'-', 
                1+YEAR(hound_date-INTERVAL 7 MONTH))  AS 'academic year',
        COUNT(*) AS 'number of events'
   FROM hound
  GROUP BY CONCAT(   YEAR(hound_date-INTERVAL 7 MONTH),'-', 
                   1+YEAR(hound_date-INTERVAL 7 MONTH))") or die(mysql_error());




// An array to store the data in a more managable order.
$data = array();

// Add each entry to the $data array, sorted by Year and Month
while($row = mysql_fetch_array($result))
    {
    $hound_date=my_date($row['hound_date']);
    list($year,$month)=explode(" ", $hound_date);

    $data[$year][$month][] = $row;




    }   


    // Go through each Year and Month and print a list of entries, sorted by month.
    foreach($data as $_year => $_months)
        {
        echo '<hr/>';
        echo "<h2>{$_year}</h2>";
        foreach($_months as $_month => $_entries)
            {
            echo '<hr/>';
            echo "<h3>{$_month}</h3>";
            echo "<ul>";
            foreach($_entries as $_entry)
                {
                echo '<li><a href="/?story='.$_entry["hound_id"].'">'.$_entry["hound_name"].'</a> </li>';

                }
            echo "</ul>";

            }
        }

Any help will be greatly appreciated! 任何帮助将不胜感激!

The first thing to do is decide how your school years are defined. 首先要做的是确定如何定义您的学年。 They seem to be defined as follows: 它们似乎定义如下:

1-August-2012 to 31-July-2013 is the 2012-2013 school year 2012年8月1日至2013年7月31日是2012-2013学年

That is, the academic year runs from the beginning of the seventh month. 也就是说,该学年从第七个月开始。

If that's the case then you can use this MySQL expression to convert any particular date column to a text string like '2012-2013' denoting the academic year in which it occurs. 如果是这种情况,则可以使用此MySQL表达式将任何特定的日期列转换为文本字符串(例如“ 2012-2013”​​),表示该日期发生的学年。

CONCAT(YEAR(datecol-INTERVAL 7 MONTH),'-', 1+YEAR(datecol-INTERVAL 7 MONTH))

For example, you could write a query like this to count the number of rows in each academic year. 例如,您可以编写这样的查询来计算每个学年的行数。

 SELECT CONCAT(   YEAR(datecol-INTERVAL 7 MONTH),'-', 
                1+YEAR(datecol-INTERVAL 7 MONTH))  AS 'academic year',
        COUNT(*) AS 'number of events'
   FROM table
  GROUP BY CONCAT(   YEAR(datecol-INTERVAL 7 MONTH),'-', 
                   1+YEAR(datecol-INTERVAL 7 MONTH))

The point is, a little date arithmetic, in which you use - INTERVAL 7 MONTH , should convert calendar years to academic years quite nicely. 关键是,您可以使用- INTERVAL 7 MONTH进行一点日期运算,以很好地将日历年转换为学年。

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

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