这段代码有什么问题?

时间:2010-11-17 11:01:26

标签: php

为什么当我查看2010年11月的情况时,它还会看到2009年?任何人都可以帮助我吗?

<?php include("db.php");
$query = mysql_query("SELECT distinct YEAR(local_date) as year FROM tbl_localnews order by local_date desc");
while ($row = mysql_fetch_array($query)) {
    //$row = array_unique($r);
    $unique_year = $row['year'];
    echo($unique_year)."<br>";

    $query2 = mysql_query("select distinct monthname(local_date) as month from tbl_localnews where local_date like '$unique_year%' order by local_date desc");
    while ($r2 = mysql_fetch_array($query2)) {
        //$row2 = array_unique($r2);
        $unique_month = $r2['month'];
        print("<a href='news.php?month=$unique_month'&year=$unique_year>News for $unique_month</a><br>");
        //echo($unique_month);
    }
}
?>

这是我的展示

<?php
$year = $_REQUEST['year'];
$last_date = "";
$result = mysql_query("SELECT *,YEAR(local_date) as year FROM tbl_localnews where year(local_date)='".$year."' ORDER BY local_date DESC ");
//echo '<h1>'.$month.'</h1>';
if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)) {
        if ($row['local_date'] != $last_date) {
            print("<h2>News for ".date('F j, Y', strtotime($row['local_date']))."</h2>");
            $last_date = $row['local_date'];
        }
        print("<p><b>".$row['local_title']."</b></p>");
        print("<p>".$row['local_desc']."</p>");
        // print("<p><b>".date('F j, Y',strtotime($row['local_date']))."</b></p>");
        //print("<p><b>".$row['local_title']."</b></p>");
        //print("<p>".$row['local_desc']."</p>");
    }
    mysql_free_result($result);
}
?>

1 个答案:

答案 0 :(得分:0)

我认为你看到像

这样的东西
2010<br><a href='news.php?month=November&year=2010'>News for November</a><br>
2009<br><a href='news.php?month=September&year=2009'>News for September</a><br>

在你的第一个例子的输出中?这是因为你每年都在循环,每年都要检索几个月:

$query = mysql_query("SELECT distinct YEAR(local_date) as year FROM tbl_localnews order by local_date desc");
while($row = mysql_fetch_array($query)) { // loop through each year
    $unique_year = $row['year'];
    echo($unique_year)."<br>";

    /* get the months for current year */
    $query2 = mysql_query("select distinct monthname(local_date) as month from tbl_localnews where local_date like '$unique_year%' order by local_date desc");
    while($r2 = mysql_fetch_array($query2)) { // loop through each month for the current year
        $unique_month = $r2['month'];
        print("<a href='news.php?month=$unique_month'&year=$unique_year>News for $unique_month</a><br>");
    }
}

如果您的tbl_localnews表中有2条具有唯一年份值的记录,那么您的输出中最多会有2行。如果您只想要1条记录,请将LIMIT 1附加到查询的末尾。

我设法让以下内容适用于您的显示页面:

<?php

    include("db.php");

    $year = $_REQUEST['year'];
    $month = $_REQUEST['month'];

    $last_date = "";
    $result = mysql_query("SELECT *, YEAR(local_date) as year FROM tbl_localnews where YEAR(local_date)=".$year." AND monthname(local_date) = '".$month."' ORDER BY local_date DESC ");

    if (mysql_num_rows($result) != 0) {
         while ($row = mysql_fetch_array($result)) {
            if ($row['local_date'] != $last_date) {
                print("<h2>News for ".date('F j, Y', strtotime($row['local_date']))."</h2>");
                $last_date = $row['local_date'];
            }

            print("<p><b>".$row['local_title']."</b></p>");
            print("<p>".$row['local_desc']."</p>");

         }
    }
    mysql_free_result($result); 

?>