Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在归档循环中仅显示一次年份_Php_Mysql_Date_Loops_Blogs - Fatal编程技术网

Php 在归档循环中仅显示一次年份

Php 在归档循环中仅显示一次年份,php,mysql,date,loops,blogs,Php,Mysql,Date,Loops,Blogs,我刚开始使用PHP和mySQL,我正在创建一些博客。当时我认为一切进展顺利——我只是在存档代码方面遇到了一些问题 我认为解决办法会很简单,但我太盲目了,我自己就是找不到重点 这是我的实际代码,一切正常,还有链接: $sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_month ORDER BY dat

我刚开始使用PHP和mySQL,我正在创建一些博客。当时我认为一切进展顺利——我只是在存档代码方面遇到了一些问题

我认为解决办法会很简单,但我太盲目了,我自己就是找不到重点

这是我的实际代码,一切正常,还有链接:

$sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_month ORDER BY date ASC") or die('No response from database.');


while ($row = mysql_fetch_array($sql)) {

$get_year = $row["get_year"];
$get_month = $row["get_month"];
$entries = $row["entries"];

    // get month name
$this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );



echo '<dl>';
echo '<dt>Entries from &nbsp;'. $get_year . '</dt>';

echo '<dd><a href="archives.php?month='. $get_month .'">Entries from &nbsp;'. $this_month . '&nbsp;</a>(' . $entries . ')</dd>';

echo '</dl>';  

}
将其更改(这是逻辑,但对我这个新手来说,这需要一段时间)为:

现在一切都好了。这正是我所期望的。这就是最终的工作代码:

    $sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_year, get_month ORDER BY date ASC") or die('No response from database.');


    $entries = array();
    while ($row = mysql_fetch_assoc($sql)) {
    $entries[$row['get_year']][] = $row;
}

    foreach($entries as $year => $months) {
  echo '<dl>';
  echo '<dt>Entries from &nbsp;'. $year . '</dt>';


    foreach($months as $month) {
    $this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );

  echo '<dd><a href="archives.php?month='. $month['get_month'] .'">Entries from &nbsp;'. $this_month . '&nbsp;</a>(' . $month['entries'] . ')</dd>';

  }

  echo '</dl>';


}
$sql=mysql\u查询(“选择年(日期)作为get\u年,选择月(日期)作为get\u月,按get\u年计算(*)作为blogdata组的条目,按日期ASC计算(*)或die(“数据库无响应”);
$entries=array();
while($row=mysql\u fetch\u assoc($sql)){
$entries[$row['get_year'][]=$row;
}
foreach($year=>$months){
回声';
回显“来自“$year”的条目”;
foreach(月份为$month){
$this_month=日期('F',mktime(0,0,0,$month['get_month']);
回显“(”.$month['entries'])”;
}
回声';
}

再次感谢大家

我发现最简单的方法是这样的:

$entries = array();
while ($row = mysql_fetch_assoc($sql)) {
  $entries[$row['get_year']][] = $row;
}

foreach($entries as $year => $months) {
  echo '<dl>';
  echo '<dt>Entries from &nbsp;'. $year . '</dt>';

  echo '<dd>';
  foreach($months as $month) {\
    $this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );

    echo '<a href="archives.php?month='. $month['get_month'] .'">Entries from &nbsp;'. $this_month . '&nbsp;</a>(' . $month['entries'] . ')';
  }
  echo '</dd></dl>';


}
$entries=array();
while($row=mysql\u fetch\u assoc($sql)){
$entries[$row['get_year'][]=$row;
}
foreach($year=>$months){
回声';
回显“来自“$year”的条目”;
回声';
foreach(月份为$month){\
$this_month=日期('F',mktime(0,0,0,$row[“get_month”]);
回显“(”.$month['entries'])”;
}
回声';
}

HTML标记并不理想,但你应该明白这一点。

我发现最简单的方法是这样的:

$entries = array();
while ($row = mysql_fetch_assoc($sql)) {
  $entries[$row['get_year']][] = $row;
}

foreach($entries as $year => $months) {
  echo '<dl>';
  echo '<dt>Entries from &nbsp;'. $year . '</dt>';

  echo '<dd>';
  foreach($months as $month) {\
    $this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );

    echo '<a href="archives.php?month='. $month['get_month'] .'">Entries from &nbsp;'. $this_month . '&nbsp;</a>(' . $month['entries'] . ')';
  }
  echo '</dd></dl>';


}
$entries=array();
while($row=mysql\u fetch\u assoc($sql)){
$entries[$row['get_year'][]=$row;
}
foreach($year=>$months){
回声';
回显“来自“$year”的条目”;
回声';
foreach(月份为$month){\
$this_month=日期('F',mktime(0,0,0,$row[“get_month”]);
回显“(”.$month['entries'])”;
}
回声';
}

这种HTML标记并不理想,但您应该了解这一点。

首先,将查询更改为“按年按月分组”,而不仅仅是“按月分组”:

$query = "SELECT 
    YEAR(date) AS get_year, 
    MONTH(date) AS get_month, 
    COUNT(*) AS entries 
FROM blogdata 
GROUP BY get_year, get_month 
ORDER BY date ASC"
我不记得以前的mysql API了,所以这里的代码带有(未经测试,但在修复潜在的打字错误后应该可以工作)

$pdo=newpdo('mysql:host=localhost;dbname=db','user','password');
$stmt=$pdo->prepare($query);
$stmt->execute();
$results=$stmt->fetchAll(PDO::FETCH_ASSOC);
//按年度分组结果*
$groupped=array();
foreach($results as$record){
$groupped[$record['get_year']]=$record;
}
//打印结果
回声“;
foreach($year=>$monthRecords分组){
回音“$年”;
foreach($monthRecords作为$record){
echo“{$record['get_month']}:{$record['entries']}”;
}
} 
回声“;
您还可以尝试使用PDO::FETCH_组简化代码,如中所述
但是我从来没有尝试过这个,所以我不会给你代码。

首先,将你的查询改为按年按月分组,而不仅仅是按月分组:

$query = "SELECT 
    YEAR(date) AS get_year, 
    MONTH(date) AS get_month, 
    COUNT(*) AS entries 
FROM blogdata 
GROUP BY get_year, get_month 
ORDER BY date ASC"
我不记得以前的mysql API了,所以这里的代码带有(未经测试,但在修复潜在的打字错误后应该可以工作)

$pdo=newpdo('mysql:host=localhost;dbname=db','user','password');
$stmt=$pdo->prepare($query);
$stmt->execute();
$results=$stmt->fetchAll(PDO::FETCH_ASSOC);
//按年度分组结果*
$groupped=array();
foreach($results as$record){
$groupped[$record['get_year']]=$record;
}
//打印结果
回声“;
foreach($year=>$monthRecords分组){
回音“$年”;
foreach($monthRecords作为$record){
echo“{$record['get_month']}:{$record['entries']}”;
}
} 
回声“;
您还可以尝试使用PDO::FETCH_组简化代码,如中所述 但是我从来没有试过这个,所以我不会给你代码。

试试这个

$start_year =  date("Y")-10;
$end_year = date("Y");
for ($i=$start_year;$i<$end_year;$i++){
 $sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*)    AS entries FROM blogdata where YEAR(date) = $i GROUP BY get_month ORDER BY date ASC"); or die('No response from database.');
   //execute your query here. get the 
    $result = mysql_query($sql);
      echo '<dl>';
    echo '<dt>Entries from &nbsp;'. $i . '</dt>';

   while ($row = mysql_fetch_array($result )) {

       $get_year = $row["get_year"];
       $get_month = $row["get_month"];
        $entries = $row["entries"];

 // get month name
 $this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );

  echo '<dd><a href="archives.php?month='. $get_month .'">Entries from &nbsp;'.   $this_month .  '&nbsp;</a>(' . $entries . ')</dd>';

 }
   echo '</dl>';
}
$start\u year=日期(“Y”)-10;
$end_year=日期(“Y”);
对于($i=$start\u year;$i试试这个

$start_year =  date("Y")-10;
$end_year = date("Y");
for ($i=$start_year;$i<$end_year;$i++){
 $sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*)    AS entries FROM blogdata where YEAR(date) = $i GROUP BY get_month ORDER BY date ASC"); or die('No response from database.');
   //execute your query here. get the 
    $result = mysql_query($sql);
      echo '<dl>';
    echo '<dt>Entries from &nbsp;'. $i . '</dt>';

   while ($row = mysql_fetch_array($result )) {

       $get_year = $row["get_year"];
       $get_month = $row["get_month"];
        $entries = $row["entries"];

 // get month name
 $this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );

  echo '<dd><a href="archives.php?month='. $get_month .'">Entries from &nbsp;'.   $this_month .  '&nbsp;</a>(' . $entries . ')</dd>';

 }
   echo '</dl>';
}
$start\u year=日期(“Y”)-10;
$end_year=日期(“Y”);

对于($i=$start\u year;$iOMG就是这样:D我自己在foreach周围跌跌撞撞,但我没有找到正确的关联。非常感谢您的快速回答!顺便说一句,Safka是对的,您也需要按年份分组。否则,您会在所有年份中将每个日历月的条目混合在一起。再次感谢!我将检查其他代码a然后尝试找到一个好的解决方案来清理我的代码。非常有用,伙计们!当一个月的第28天之后,date('F',mktime(0,0,0,$row['get_month'])将返回'March',即使$row['get_month']的值是2。要修复它,请将其更改为date('F',mktime(0,0,$month['get_month'],1))天哪,就是这样:D我自己在foreach周围跌跌撞撞,但我没有找到正确的关联。非常感谢你的伟大和快速的回答!顺便说一句,Safka是对的,你也需要按年份分组。否则,你会在所有年份中将每个日历月的条目混合在一起。再次感谢!我会检查其他代码并尝试找到一个good解决方案会稍微清理我的代码。非常有用,伙计们!当一个月的第28天之后,date('F',mktime(0,0,0,$row['get_month'])将返回'March',即使$row['get_month']的值为2。要修复它,请将其更改为date('F',mktime(0,0,$month['get_month'],1))谢谢,我将实现查询更正,并进一步查看您的代码。非常感谢。:)非常感谢您给我的建议,让我也能将get_年分组。我会监督这一点,并想知道明年会发生什么。谢谢:)我最喜欢的是“明年会出现的bug”。)也许除了“只会在闰年出现的bug”之外,我想知道在完成这个项目后我还会发现多少“bug”^^^谢谢,我将实现查询更正并进一步查看您的代码。非常感谢非常感谢您给我们的建议,让我们一起度过今年。我会监督这一点,并想知道明年(谢谢:)