0

The following code displays the news article date, title/header, and article.

<h2><?php echo(LATEST_NEWS_ARTICLE_DATE_10);?>&nbsp;&nbsp;&nbsp;
<?php echo(LATEST_NEWS_ARTICLE_HEADER_10);?></h2>
<p><?php echo(LATEST_NEWS_ARTICLE_10);?>

....articles 9 through to 2.....

<h2><?php echo(LATEST_NEWS_ARTICLE_DATE_1);?>&nbsp;&nbsp;&nbsp;
<?php echo(LATEST_NEWS_ARTICLE_HEADER_1);?></h2>
<p><?php echo(LATEST_NEWS_ARTICLE_1);?>

displays:-

article 10:-

31st March 2021 PHP Loop still issues

Whats going on with this loop still

....articles 9 to 2....

article 1:- 31st March 2020 PHP Loop

Whats going on with this loop

but rather than type the code out for each article (eventually 20 articles per page, multi pages), thought I would use a loop.

<?php
for ($x = 10; $x > 0; $x--) {
echo (LATEST_NEWS_ARTICLE_DATE_.$x)."&nbsp;&nbsp;&nbsp;&nbsp;";
echo (LATEST_NEWS_ARTICLE_HEADER_.$x)."<br>";
echo (LATEST_NEWS_ARTICLE_.$x)."<br><br>";
}
?>

This should display the latest article 10, to the oldest article 1, but instead it displays

LATEST_NEWS_ARTICLE_DATE_10 LATEST_NEWS_ARTICLE_HEADER_10 LATEST_NEWS_ARTICLE_10

.....articles 9 through to 2.....

LATEST_NEWS_ARTICLE_DATE_1 LATEST_NEWS_ARTICLE_HEADER_1 LATEST_NEWS_ARTICLE_1

I must be missing something as its so close. Any help including tutorials to try would be appreciated, I haven't been able to find something yet.

4
  • 1
    Where does LATEST_NEWS_ARTICLE_DATE_ come from? Are you implementing new code for every new post??? Commented Mar 31, 2021 at 11:02
  • LATEST_NEWS_ARTICLE_DATE_.$x - That will try and concatenate the value of the constant LATEST_NEWS_ARTICLE_DATE_ and the value of the variable $x. Constants are not the correct tool for what you're trying to do. Define a multidimensional array with all the news and iterate through that instead. Commented Mar 31, 2021 at 11:26
  • @B001ᛦ I guess I am in that I would define the next article when written. Commented Mar 31, 2021 at 12:09
  • @ Magnus Eriksson thanks. looking at it the wrong way, I will look at the array option. Commented Mar 31, 2021 at 12:11

1 Answer 1

1

This won't work the way you expect it to work - PHP will always evaluate your construct to the value of a constant named LATEST_NEWS_ARTICLE_ plus the value of the variable $x.

You're on the right track using a loop to iterate over a data set, though, but to store the data, you should definitely use a variable, not a constant (cf. DEFINE vs Variable in PHP).

The most obvious choice would be an array, e.g.:

$latest_articles = [

    ['date' => '...', 'header' => '...', 'body' => '...'],
    ['date' => '...', 'header' => '...', 'body' => '...']
];

Later in your code, you could simply loop over it with foreach:

foreach ($latest_articles as $article) {

    echo($article['date']);
    echo($article['header']);
    ...    
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I guess I am looking at it the wrong way as looking to use what i have got rather than rearrange what i have. I will have a look at this and it makes some sense at least.

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.