gpt4 book ai didi

php - 重复文章出现在网站上

转载 作者:行者123 更新时间:2023-11-30 23:53:35 24 4
gpt4 key购买 nike

我最近在我的网站上注意到一个新闻页面正在重复数据库中所有“五月”的新闻文章(您会在这里看到: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->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;
}

最佳答案

代码似乎可以正常工作

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

所以,因为五月有两个事件,所以它输出了两次标题。让我回顾一下...

  • 获取按日期分组的新闻(据称)
    • 6 月,仅 1 篇文章
    • 1:
      • 输出标题
      • 选择文章
    • 输出文章
    • 5 月,2 篇文章
    • 1:
      • 输出标题
      • 选择文章
      • 输出文章
    • 2:
      • 输出标题
      • 选择文章
      • 输出文章

这个组由 SELECT * FROM ".TBL_NEWS."GROUP BY (FROM_UNIXTIME(thedate, '%Y')),(FROM_UNIXTIME(thedate, '%M')) ORDER BY thedate DESC 未按预期运行,返回了 5 月的两个结果

试试这段代码

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

关于php - 重复文章出现在网站上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14132141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com