douchi0028 2013-01-03 02:06
浏览 12

网站上出现重复的文章

I have recently noticed on my website that a news page is repeating news articles from all "May"'s in the database (you will notice by looking here: www.darlingtontowntwinning.co.uk/news_&_events)

I know that the coding is messy and possibly out of date, however, the website was built for us, and I don't have the skills (yet - I am learning!) to change the entire website at this time.

Is there a way to stop this from occuring - since I believe I have already got a limit of one of each record to display:

<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>

Get news function is:

function getNews($title,$month,$year,$group){
global $database;
return $database->getNews($title,$month,$year,$group);}

$database->getNews function is:

//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;
   }

}

  • 写回答

2条回答 默认 最新

  • dsjk3214 2013-01-03 03:44
    关注

    Welp. There's your problem.

    Your function says, if($title). Since $title comes in as a required parameter in the function, I think PHP is registering it as, yes, that's a variable that is set. So, basically what happens, then, is you're getting back a mysql_fetch_array result, which you are then running mysql_fetch_array for a second time.

    Try:

    //in your function getNews()
    if($title){
       $q=$this->query("SELECT * FROM ".TBL_NEWS." WHERE ftitle = '$title'" );
       return $q;
    }
    //rest of function down here
    

    That could work. The issue I see is, doing that, you WILL CAUSE ISSUES ANYWHERE ELSE THAT FUNCTION IS CALLED. So be careful! The fix above is the fix to ensure you get code into a better state. If you want a hack, try this:

    <? $innernews=$session->getNews("",$month,$year);?>
    <? foreach($innernews as $innerarticle) {?>
    <a href="/news/<?=$innerarticle['ftitle']?>" <? if($title==$innerarticle['ftitle']){?> class="active"<? }?>><?=$innerarticle['title']?></a>
    <? }?>
    

    The foreach loop should give you what you want!

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line