0

Would like to ask how to add an if statement into the following code.

What it presently does is list all of the news articles from the database under a title of their respective months/years and groups the articles under this: i.e. May 2012 - News A, News B June 2012 - News C July 2012 - News D

We are having an issue with stories prior to 1970, due to the unix date system being used. I don't wish to change the system that we presently use due to it "working", however, if a fix could be found for this (i've tried minus figures....it doesnt work), OR if an if statement could be added into this code below, that stops articles from being displayed/groups being created for articles pre-1970 then this would be good :)

<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>
5
  • w3schools.com/php/php_if_else.asp Commented Jan 3, 2013 at 1:19
  • 3
    @MathewFoscarini You should not use w3schools Commented Jan 3, 2013 at 1:21
  • 2
    w3school links are not very well recieved on SO, especially when it comes to PHP, there is mostly crap to be found there: w3fools.com Commented Jan 3, 2013 at 1:21
  • 1
    @MathewFoscarini How/where did you learn to write such code? Commented Jan 3, 2013 at 1:22
  • It was simply the first thing when you google "php if". Which was about as much effort that I wanted to give this question. So thanks for drawing me back and wasting more of my time. Commented Jan 3, 2013 at 1:36

4 Answers 4

1

Epoch Fail

Okay, now for serious stuff.

Firstly, you seem to be dropping in and out of PHP needlessly. It's okay to mave more than one line of PHP! Second, you're using short-tags. While this may work, it may not. It's best to always use <?php (unless you're using PHP 5.4 or newer, in which case you can use <?=$something?> to echo something regardless of settings).

Now, on to the actual problem.What you could do is:

$year = ...;
if($year == 1970) echo "Sorry, news is not available for this date.";
else {
    // rest of your code to show news here
}
Sign up to request clarification or add additional context in comments.

1 Comment

@DarylGill XKCD has a strip for everything, and yet he still comes out with fresh new stuff... He's amazing XD
0

As "crude" as Matthew Harrington requested:

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

1 Comment

Thanks so much Smuuf! I do have another questions - for some reason its showing duplicate months (only May's however?) do i need to put this into a new post?
0

In your <? while($innerarticle=mysql_fetch_array($innernews)){?> Code, modify your query on $innernews Select Dates after you wish to stop showing by using the syntax >

it would also be better to provide how your $innernews And $news Variable is being set

Comments

0

You could change it from date() to php's object-oriented DateTime class.
note - This requires php v. >= 5.2.0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.