Jump to content

Recommended Posts

Hello,

 

I have a PHP news script which I have coded myself.

Everytime when I want to show all the news I´ll first use function array_reverse for the news textfile array.

But, when I add a new news in the file, the news ID changes.

 

Line numbers: <x>

 

So example the textfile:

<0>First message
<1>Second message
<2>Third message

When I add the new news:

<0>First message
<1>Second message
<2>Third message
<3>Fourth message

 

And when I use array_reverse

<0>Fourth message
<1>Third message
<2>Second message
<3>First message

 

But I want that the "First message" always has ID number 0 etc. (line number is ID number)

 

How could I do so? Should I make some changes for the for function?

 

Thank you for help, hopefully you will understand my explaining :D

Link to comment
https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/
Share on other sites

Sorry, but it didn´t worked. Please have look whats wrong:

 

<?php
$filu = array_reverse(file("news.txt"), true);

for($i=0;$i<count($filu);$i++){
$exp = explode("|", $filu[$i]);
$pvm = str_replace("\n", "", $exp[3]);
print'<a href="read.php?a=read&id='.$i.'"><strong>'.$exp[0].'</strong></a> (<span class="day">'.$pvm.'</span>)';
print"<br>";
}
?>

 

And here is the news.txt file:

TITLE|LONGTEXT|SHORTTEXT|DATE
TITLE|LONGTEXT|SHORTTEXT|DATE

 

But now it shows the news from oldest to the latest.

 

And forgot to mention- the file is ordered from oldest to the latest (line0=oldest news,latest line=latest news)

<?php
$lines = array_reverse(file("news.txt",FILE_IGNORE_NEW_LINES), true);
foreach($lines as $key => $line){
list($title,$long_text,$short_text,$date) = explode("|", $line);
echo "<a href='read.php?a=read&id=$key'><strong>$title</strong></a> (<span class='day'>$date</span>)<br>\n";
}
?>

Thank you for help. I tried with this code:

<?php
$filu = array_reverse(file("news.txt"));
$maara=count($filu)+1;
for($i=$maara;$i>0;$i--){
$exp = explode("|", $filu[$i]);
$pvm = str_replace("\n", "", $exp[3]);
print'<a href="read.php?a=read&id='.$i.'"><strong>'.$exp[0].'</strong></a> (<span class="day">'.$pvm.'</span>)';
print"<br>";
}
?>

 

But it only shows me the first line of the file. Others are just ().

<?php
$lines = file("news.txt",FILE_IGNORE_NEW_LINES);
for($i = count($lines) -1; $i >= 0; $i--){
list($title,$long_text,$short_text,$date) = explode("|", $lines[$i]);
echo "<a href='read.php?a=read&id=$i'><strong>$title</strong></a> (<span class='day'>$date</span>)<br>\n";
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.