faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 12 people (8%) answered Yes
Recently 1 of 10 people (10%) answered Yes

Entry

How do I make a news script that only saves/prints the last X entries with an admin section/flatfile

Feb 28th, 2008 00:06
dman, Gerhardt, James Royce, http://www.ttnr.org


The best way would probably be to save the file in the correct order, 
with the newest items on top.
If that's not an option, one way is to read all items into an array, 
perhaps using the file(). If the file get's too big, and your site 
get's a lot of visitors per minute, you might want to consider using a 
database to generate the pages more quickly.
You can then use the following code to read the last 10 items;
<?php
/*
 This code assumes that you have stored the news items
 in an array called "news"
*/
$stop = 10; // if you want to output 10 items
$max = size($news); 
// Need to get size in case array has less than 10 elements
if($max<$stop)
   $stop = $max;
for($i = 0; $<=$stop; $i++)
   $item = array_pop($news); // removes last item from array
?>