Page 1 of 1

Limit File Output Question

Posted: Mon Jul 02, 2007 6:00 am
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";
}
?>

Posted: Mon Jul 02, 2007 6:49 am
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.

Posted: Mon Jul 02, 2007 8:37 am
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

Agree

Posted: Mon Jul 02, 2007 8:39 am
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.