我最近在我的网站上发现新闻页面正在重复数据库中所有“May”的新闻报道(你会看到这里:www.darlingtontowntwinning.co.uk/news_&_events)
我知道编码很乱并且可能已经过时了,但是,网站是为我们构建的,我没有技能(但我正在学习!)此时更改整个网站。
有没有办法阻止这种情况发生 - 因为我相信我已经限制了每条记录中的一条显示:
<div id="right" class="news">
<h3>Archive</h3>
<? $news=$session->getNews("","","",1);?>
<? while($article=mysql_fetch_array($news)){?>
<?
$date = $article['thedate'];
$year = date('Y', $date);
$month = date('F', $date);
?>
<h4><?=$month." - ".$year;?></h4>
<nav class="small">
<? $innernews=$session->getNews("",$month,$year);?>
<? while($innerarticle=mysql_fetch_array($innernews)){?>
<a href="/news/<?=$innerarticle['ftitle']?>" <? if($title==$innerarticle['ftitle']){?> class="active"<? }?>><?=$innerarticle['title']?></a>
<? }?>
</nav>
<? }?>
</div>
获取新闻功能是:
function getNews($title,$month,$year,$group){
global $database;
return $database->getNews($title,$month,$year,$group);}
$ database-&gt; getNews函数是:
//get news
function getNews($title,$month,$year,$group){
if($title){
$q=$this->query("SELECT * FROM ".TBL_NEWS." WHERE ftitle = '$title'" );
return mysql_fetch_array($q);
}else if($year && $month){
$q=mysql_query("SELECT * FROM ".TBL_NEWS." WHERE (FROM_UNIXTIME(thedate, '%Y') = '$year') AND (FROM_UNIXTIME(thedate, '%M') = '$month') ORDER BY thedate DESC");
return $q;
}else if($group){
$q=$this->query("SELECT * FROM ".TBL_NEWS." GROUP BY (FROM_UNIXTIME(thedate, '%Y')),(FROM_UNIXTIME(thedate, '%M')) ORDER BY thedate DESC" );
return $q;
}else{
$q=$this->query("SELECT * FROM ".TBL_NEWS." ORDER BY thedate DESC" );
return $q;
}
}
答案 0 :(得分:0)
代码似乎正在运作
//get news from group 1
$news=$session->getNews("","","",1);
// for each article work out the date
while($article=mysql_fetch_array($news))
$date = $article['thedate'];
$year = date('Y', $date);
$month = date('F', $date);
...
// then select everything again after working out the date (odd way of doing it)
$innernews=$session->getNews("",$month,$year);
// and output each
所以,因为5月份有两个事件,它会输出两次标题。让我回顾一下......
这个SELECT * FROM ".TBL_NEWS." GROUP BY (FROM_UNIXTIME(thedate, '%Y')),(FROM_UNIXTIME(thedate, '%M')) ORDER BY thedate DESC
组的行为不符合预期,并且会为May返回两个结果
试试此代码
<div id="right" class="news">
<h3>Archive</h3>
<?
$news=$session->getNews();
$bydate=array(); // articles by date
while($article=mysql_fetch_array($news)){
$k=date('YF', $date);
if (!isset($bydate[$k])) $bydate[$k]=array(); // create sub array
$bydate[$k][]=$article; // push article to this sub array
}
foreach ($bydate as $date->$articles){ // run through top array
?><h4><?= substr($date,4) . " - " . substr($date,0,4); ?></h4><nav class="small"><?
foreach ($articles as $innerarticle){ // now each article within this date
?><a href="/news/<?=$innerarticle['ftitle']?>" <? if($title==$innerarticle['ftitle']){?> class="active"<? }?>><?=$innerarticle['title']?></a><?
}
?></nav><?
}
?></div>
请更改
function getNews($title,$month,$year,$group){
到
function getNews($title=NULL, $month=NULL, $year=NULL, $group=NULL){
答案 1 :(得分:-2)
韦尔普。这是你的问题。
你的功能说,如果($ title)。由于$ title作为函数中的必需参数出现,我认为PHP正在注册它,是的,这是一个设置的变量。那么,基本上会发生什么,那么,你是否正在返回一个mysql_fetch_array结果,然后你再次运行mysql_fetch_array。
尝试:
//in your function getNews()
if($title){
$q=$this->query("SELECT * FROM ".TBL_NEWS." WHERE ftitle = '$title'" );
return $q;
}
//rest of function down here
这可行。我看到的问题是,这样做,你会在任何地方引起任何问题。所以要小心!上面的修复是确保您将代码置于更好状态的修复程序。如果你想要一个黑客,试试这个:
<? $innernews=$session->getNews("",$month,$year);?>
<? foreach($innernews as $innerarticle) {?>
<a href="/news/<?=$innerarticle['ftitle']?>" <? if($title==$innerarticle['ftitle']){?> class="active"<? }?>><?=$innerarticle['title']?></a>
<? }?>
foreach循环应该给你你想要的东西!