Need to show the first (or last) file in a set

Permalink 1 user found helpful
Have looked at the following post and found a lot of great information -
http://www.concrete5.org/community/forums/customizing_c5/get_files_...

The code presented shows a list of files from a set and works fine. My question is that when the files are listed, how do I show only one file (I am not a PHP programmer)?

Loader::model("file_set");
Loader::model('file_list');
$fs = FileSet::getByName('My File Set');
$fl = new FileList();
$fl->filterBySet($fs);
$fl->sortByFileSetDisplayOrder();
//$fl->sortBy('fvTitle', 'asc');
$files = $fl->get((int)$this->1, 0);
$files = array_reverse($files);
foreach($files as $f) {
    print $f->getFileName()."<br>";
    print $f->getTitle()."<br>"; 
    print $f->getDownloadURL()."<br>";
    print $f->getRelativePath()."<br><br>";
}


Show the first file in the set
Loader::model("file_set");
Loader::model('file_list');
$fs = FileSet::getByName('My File Set');
$fl = new FileList();
$fl->filterBySet($fs);
$fl->sortByFileSetDisplayOrder();
//$fl->sortBy('fvTitle', 'asc');
$files = $fl->get(1);
$files = array_reverse($files);
foreach($files as $f) {
    print $f->getFileName()."<br>";
    print $f->getTitle()."<br>"; 
    print $f->getDownloadURL()."<br>";
    print $f->getRelativePath()."<br><br>";
}


Show the last one in the set
$files = $fl->get(2);


and it does not seem to work to show one file in the set.. What am I missing?

Thanks,

Kent

kdyer
 
kdyer replied on at Permalink Reply
kdyer
Found the solution here -http://stackoverflow.com/questions/1656969/php-limit-foreach-statem...

Loader::model("file_set");
Loader::model('file_list');
$fs = FileSet::getByName('My File Set');
$fl = new FileList();
$fl->filterBySet($fs);
$fl->sortByFileSetDisplayOrder($gs);
$files = $fl->get();
//$fl->get((1)$this->1, 0);
$files = array_reverse($files);
$i=0;
foreach($files as $f) if ($i < 1) {
    print $f->getFileName()."<br>";
    print $f->getTitle()."<br>"; 
    print $f->getDownloadURL()."<br>";
    print $f->getRelativePath()."<br><br>";


Thansk,