Limit File Output Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maddenuk
Forum Commoner
Posts: 27
Joined: Fri May 18, 2007 3:07 am

Limit File Output Question

Post by maddenuk »

My question regards limiting the number of items returned when outputing data from a file. For example i have a list of podcasts that are held in a file and i only want to return the latest 4 podcasts.
I think i would some sort of loop function but i am a little unclear how and where in the code i would put it in? Could anyone give me any advice?

Thanks

Code: Select all

<?php


include("config.php");


$handle = opendir ($upload_dir);
while (($filename = readdir ($handle)) !== false)
{
if ($filename != '..' && $filename != '.' && $filename != 'index.htm')
{


$file_array[$filename] = filemtime ($upload_dir.$filename);

}
}

if (!empty($file_array)) { //se la variabile, quindi la directory, non è vuota

# asort ($file_array); // older above
arsort ($file_array); //invert order (most recent above)

//echo "<h2>$L_all $L_podcast</h2><br>";

//echo '
//<table border=0 cellspacing=0 cellpadding=5>
//';

foreach ($file_array as $key => $value)

{


$file_multimediale=explode(".",$key); //divide filename from extension

$fileData = checkFileType($file_multimediale[1],$podcast_filetypes,$filemimetypes);
$podcast_filetype=$fileData[0];

if ($file_multimediale[1]=="$podcast_filetype") { // if the extension is the same as specified in config.php

$file_size = filesize("$upload_dir$file_multimediale[0].$podcast_filetype");
$file_size = $file_size/1048576;
$file_size = round($file_size, 2);

############
$filedescr = "$upload_dir$file_multimediale[0].desc"; //description file

if (file_exists("$filedescr")) { //if description file exists 

$file_contents=NULL; //reset memory

//open description file
$fs1 = fopen( $filedescr, "r" ) or die("$L_opendesc_error"); 

while (!feof($fs1)) { 
   $file_contents .= fgets($fs1, 1024); 
} 
fclose($fs1);
} 
############

$fields = explode("|||",$file_contents); //divide title from description

echo "<p><a name=\"$file_multimediale[0]\"></a><a href=\"$url$upload_dir$file_multimediale[0].$podcast_filetype\"><img src=\"podcast.gif\" alt=\"$L_downloadfile $fields[0]\" title=\"$L_downloadfile $fields[0]\" border=\"0\"></a><br /><b> $fields[0]</b> - " .date('l dS F', $value). " <i>($file_size $L_bytes)</i><br>$fields[1] <a href=\"p.php?file=$file_multimediale[0].$podcast_filetype\"><br>$L_details</a></p><br></td></tr>";



}

}

echo '
</tr>
</table>
';
} else { 
echo "$L_dir <b>$upload_dir</b> $L_empty";
}
?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You could always not use a file. By using a database, it'd be as simple as setting a LIMIT. With files, I think you'd have to fread() each line you're after.
maddenuk
Forum Commoner
Posts: 27
Joined: Fri May 18, 2007 3:07 am

Post by maddenuk »

thanks for the reply. Yeah i agree but i'm using software already written and tailoring it to meet my needs. Any other ideas?

Cheers
Tarquinius999
Forum Newbie
Posts: 2
Joined: Mon Jul 02, 2007 8:32 am

Agree

Post by Tarquinius999 »

I agree, a database would be the best way to do it. You can create a script to edit information quickly (like a admin panel) and then have many variables to make the output run dynamically based on the news post (eg if the type is news then font color can be red or if its a new feature it can be green)

And its very easy, you just add "LIMIT # DESC"* to your SQL syntax.

*replace # with the number of articles to output
*ASC = 1-9 (oldest first)
*DESC = 9-1 (newest first)

EDIT: thats great, you replied 2 mins before i finished typing.
Post Reply