Ollifi Posted June 7, 2011 Share Posted June 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/ Share on other sites More sharing options...
dragon_sa Posted June 7, 2011 Share Posted June 7, 2011 post some code so we can see what you are doing then we may be able to help with the issue Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/#findComment-1226303 Share on other sites More sharing options...
PFMaBiSmAd Posted June 7, 2011 Share Posted June 7, 2011 It might be worth reading about the preserve_keys parameter of the array_reverse function. Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/#findComment-1226304 Share on other sites More sharing options...
Ollifi Posted June 7, 2011 Author Share Posted June 7, 2011 Thank you very much, I will try it! Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/#findComment-1226315 Share on other sites More sharing options...
Ollifi Posted June 9, 2011 Author Share Posted June 9, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/#findComment-1227281 Share on other sites More sharing options...
PFMaBiSmAd Posted June 9, 2011 Share Posted June 9, 2011 <?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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/#findComment-1227295 Share on other sites More sharing options...
Ollifi Posted June 9, 2011 Author Share Posted June 9, 2011 thank you for the script. Is it possible to do the same with for function? Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/#findComment-1227358 Share on other sites More sharing options...
PFMaBiSmAd Posted June 9, 2011 Share Posted June 9, 2011 You would have your for(){} loop start with the highest key number and decrement it until it reaches zero. Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/#findComment-1227527 Share on other sites More sharing options...
Ollifi Posted June 10, 2011 Author Share Posted June 10, 2011 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 (). Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/#findComment-1227793 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2011 Share Posted June 10, 2011 <?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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/#findComment-1227797 Share on other sites More sharing options...
Ollifi Posted June 10, 2011 Author Share Posted June 10, 2011 Thank you so much!It works just as excepted! Quote Link to comment https://forums.phpfreaks.com/topic/238622-sort-news-from-latest-to-the-oldest/#findComment-1227798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.