0

我正在实现 Wordpress 功能,使用 PHP 每年和每月查看帖子

例如:

新闻稿

2013(4)
十月(1)
公告1

但我得到的每条记录都在结果中重复。代码如下所示:

$query = "SELECT * FROM edu_announcements WHERE status = '1' ORDER BY add_time asc";
$resultSet = mysql_query($query);
if (mysql_num_rows($resultSet))
{
    $newsArray = array();

    echo '<ul>' . PHP_EOL;
    echo '<li><strong>Press releases:</strong></li>' . PHP_EOL;                                            

    while ($newsResult = mysql_fetch_array($resultSet))
    { 
        $newDate =  $newsResult['add_time'] ;  
       $timePeriod =date("F Y", $newDate); 
        //$timePeriod = date('F  Y ',strtotime($newDate));
        $timePeriodY = date('Y',$newDate);
        $timePeriodM = date('F',$newDate);                                          

        if (!isset($newsArray[$timePeriod]))
        {
              $newsArray[$timePeriod] = array();
        }           
           $newsArray[$timePeriod][] = $newsResult;  

    }       

    foreach ($newsArray as $timePeriod => $newsItems)
    {
        $timePeriodY = date('Y',strtotime($timePeriod));
        echo '<li><strong>' . $timePeriodY . '</strong>' . PHP_EOL;  
        echo '<ul>' . PHP_EOL;

        //by month
        foreach ($newsArray as $timePeriod => $newsItems)
        {
            echo '<li><strong>' . $timePeriod . '</strong>' . PHP_EOL;  
            echo '<ul>' . PHP_EOL;                                

            //news items
            foreach ($newsItems as $item)
            {
                echo '<li>';
                echo '<a href="'.$wwwUrl.'press-releases/'.$item["id"].'/'.$item["title"].'.php">'.$item["title"].'</a>';
                echo '</li>' . PHP_EOL;
            }     

            //end by month
            echo '</ul>' . PHP_EOL; 
            echo '</li>' . PHP_EOL;                   
        }

        //end by year
        echo '</ul>' . PHP_EOL; 
        echo '</li>' . PHP_EOL;                   
    }

    echo '<li>&nbsp;</li>' . PHP_EOL;   
    echo '</ul>' . PHP_EOL; 
} 
else {
    echo 'No announcements';
}

我得到的结果如下所示:

Array ( [July 2013] => Array ( [0] => Array ( [0] => 99 [id] => 99 [1] => sdfsdf [title] => sdfsdf [2] =>
 sdfsdfsdfc
[description] =>
 sdfsdfsdfc
[3] => [documents] => [4] => [photo1] => [5] => [photo2] => [6] => [photo3] => [7] => [photo4] => [8] => 1 [public_visibility] => 1 [9] => 0 [dept_visibility] => 0 [10] => 0 [depatment] => 0 [11] => 1373913000 [add_time] => 1373913000 [12] => 1 [status] => 1 [13] => sdfsdf [small_description] => sdfsdf ) [1] => Array ( [0] => 100 [id] => 100 [1] => sefsdfvsxdf [title] => sefsdfvsxdf [2] =>
 sdfsdfsd
[description] =>
 sdfsdfsd
[3] => [documents] => [4] => [photo1] => [5] => [photo2] => [6] => [photo3] => [7] => [photo4] => [8] => 1 [public_visibility] => 1 [9] => 0 [dept_visibility] => 0 [10] => 0 [depatment] => 0 [11] => 1374604200 [add_time] => 1374604200 [12] => 1 [status] => 1 [13] => sdfsefs [small_description] => sdfsefs ) )

我想要表中不同的记录。谁能帮我解决这个问题。

4

2 回答 2

0

我通过使用如下所示的代码解决了答案:

       echo '<div id="news_events_section_wrapper">
            <div class="news_events_section">';

            $CURRENT_TIMESTAMP=time();
            $sqlstr=mysql_query("select * from nesote_edu_announcements where status=1");
            $count=mysql_num_rows($sqlstr);
            if($count!=0)
            {
            while($article=mysql_fetch_row($sqlstr))
            {
            $id=$article[0];
            $details= $article[2];
            $postedon=date("F j, Y",$article[11]);

            $olderpost=date("Y",$article[11]);

            $yearnoquery=mysql_query("select count(*) from nesote_edu_announcements where add_time <='$CURRENT_TIMESTAMP' and status=1 ");
            $resultq=mysql_fetch_row($yearnoquery);
            $numberofposts=$resultq[0];
            }

            echo "<a href=javascript:showmonths(".$id.") style=".'"'."text-decoration:none".'"><div>';
                echo $olderpost;echo '(';echo $numberofposts;echo ')';
                echo '</div>
                </a>';

                $monthqry=mysql_query("SELECT add_time,COUNT(id),id FROM nesote_edu_announcements WHERE status=1  AND (add_time <='$CURRENT_TIMESTAMP') AND FROM_UNIXTIME(add_time, '%Y')='$olderpost' GROUP BY FROM_UNIXTIME(add_time, '%M') ASC");
                while($month=mysql_fetch_row($monthqry))
                {
                    $monthsfordisplay=date("F",$month[0]);  

                    $months=date("m",$month[0]);

                    $monthnos=$month[1];
                    $artid=$month[2];

        echo  '<div id='.'"'."month_art".'"'." class=".'"'."month_"; echo $id.'">';

                    echo  '<a href="javascript:showdetails'.'('; echo $artid; echo ')">'; echo $monthsfordisplay;
           echo '('; echo $monthnos; echo ')</a></div>';
                    $smalldisquery=mysql_query("SELECT * FROM  nesote_edu_announcements WHERE FROM_UNIXTIME(add_time, '%m')='$months' AND add_time <='$CURRENT_TIMESTAMP' and status=1");
                    while($smalldisc=mysql_fetch_row($smalldisquery))
                    {
                        $articlemonthid=$smalldisc[0];
                        $disc=$smalldisc[1];

                echo '  <div style="display:none;" id="sub_str" class="smalldiscription_';echo $artid; echo '">
                    <a href='.'"index.php?page=events/announcementdetails/'; echo $articlemonthid; echo '"'.' style='.'"text-decoration:none;'.'">
                    <font color="#80000">'; 
                $message=substr($disc,0,30);

$message=$message."...";


                echo "* ".$message; echo "</font></a>";
                    echo "</div>";
           }
                }
            }
                echo "</div></div>";

这将在年月基础上显示公告。

于 2013-10-25T04:37:55.433 回答
0

如果您的意思是为什么要重复每条记录的部分,例如:

[0] => 99
[id] => 99 

这是因为mysql_fetch_array有第二个选项(超出你的 $resultset),默认为 int $result_type = MYSQL_BOTH.

The type of array that is to be fetched. It's a constant and can 
take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.

如果您必须使用原始(和旧)mysql 函数(正如其他人所说,这不再是好的做法),那么您还可能希望添加, MYSQL_ASSOC到函数调用中,然后只处理列的名称 - 例如作为“id”、“标题”和“描述”。也可能有与其他 DB 访问调用类似的参数,但大多数情况下它们将使用 MYSQL_ASSOC 样式的返回来获得默认的列名。

于 2013-10-10T12:28:37.070 回答