【问题标题】:Adding an if statement into PHP/HTML在 PHP/HTML 中添加 if 语句
【发布时间】:2013-01-03 01:16:32
【问题描述】:

想问一下如何在下面的代码中添加if语句。

它目前所做的是将数据库中的所有新闻文章列在其各自月/年的标题下,并将文章分组在此: IE。 2012 年 5 月 - 新闻 A,新闻 B 2012 年 6 月 - 新闻 C 2012 年 7 月 - 新闻 D

由于使用的是 unix 日期系统,我们在 1970 年之前的故事中遇到了问题。 我不希望更改我们目前使用的系统,因为它“工作”,但是,如果可以找到解决方案(我尝试过负数......它不起作用),或者如果可以将语句添加到下面的代码中,以阻止显示文章/为 1970 年之前的文章创建组,这样会很好:)

<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>
<? }?>
<h4>Pre-1970</h4>
<nav class="small">
<a href="#">See More - Currently Unavailable</a>
</nav>
</div>

【问题讨论】:

  • @MathewFoscarini 你不应该使用 w3schools
  • w3school 链接在 SO 上的接收效果不是很好,尤其是在 PHP 方面,大部分都是废话:w3fools.com
  • @MathewFoscarini 您是如何/在哪里学会编写这样的代码的?
  • 当你用谷歌搜索“php if”时,这只是第一件事。这与我想提出这个问题的努力一样多。所以谢谢你把我拉回来并浪费我更多的时间。

标签: php html mysql unix


【解决方案1】:

好的,现在是严肃的事情。

首先,您似乎不必要地进出 PHP。写多行PHP也没关系!其次,您使用的是短标签。虽然这可能有效,但可能无效。最好始终使用 &lt;?php(除非您使用的是 PHP 5.4 或更高版本,在这种情况下,您可以使用 &lt;?=$something?&gt; 来回显某些内容而不管设置如何)。

现在,进入实际问题。你可以做的是:

$year = ...;
if($year == 1970) echo "Sorry, news is not available for this date.";
else {
    // rest of your code to show news here
}

【讨论】:

  • @DarylGill XKCD 有一个适合所有东西的条带,但他仍然推出了新鲜的东西......他太棒了 XD
【解决方案2】:

正如马修·哈灵顿所要求的那样“粗鲁”:

<h3>Archive</h3>
<? $news=$session->getNews("","","",1);?>
<? while($article=mysql_fetch_array($news)){?>
<? 
$date = $article['thedate'];
$year = date('Y', $date);
$month = date('F', $date);
if ($year<1970) continue;
?>
<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>
<? }?>
<h4>Pre-1970</h4>
<nav class="small">
<a href="#">See More - Currently Unavailable</a>
</nav>
</div>

【讨论】:

  • 非常感谢斯穆夫!我还有其他问题 - 出于某种原因,它显示重复的月份(但是只有五月?)我需要把它放到一个新帖子中吗?
【解决方案3】:

在您的&lt;? while($innerarticle=mysql_fetch_array($innernews)){?&gt; 代码中,修改您对$innernews 的查询,使用&gt; 语法在您希望停止显示后选择日期

最好提供您的$innernews$news 变量的设置方式

【讨论】:

    【解决方案4】:

    您可以将其从 date() 更改为 php 的 object-oriented DateTime class
    注意 - 这需要 php v. >= 5.2.0

    <?
    $date = $article['thedate'];
    $dt = new DateTime($date);
    $year = $dt->format("Y");
    $month = $dt->format("F");
    ?>
    

    【讨论】:

      猜你喜欢
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2019-05-01
      相关资源
      最近更新 更多