Page 1 of 1

[ASK] Pagination Data in Array with Zend Framework ?

Posted: Sun Feb 28, 2010 11:52 am
by zipcoi
hii all..

I want to ask.. how to pagination data in array with zend framework ?
If pagination data from tabel in database, that's work fine, but if I create my own data in array, it's not working..
this is my example code:
> in model:

Code: Select all

public function getFolder() {
        if(!($open = opendir($this->folder))) {
            die("Cannot open directory..!!");
        }
        
        $result = array();
        $n = 0;
        while($folder = readdir($open)) {
            if(is_dir($this->folder . $folder) == true) {
                if($folder <> "." and $folder <> "..") {
                    $result[$n]['foldername'] = $folder;
                    $n += 1;
                }
            }
        }
        closedir($open);
        
        return $result;
    }
> in controller:

Code: Select all

public function galleryAction() {
        $currentPage = $this->_request->getParam('page', 1);
        $folder = "pictures/gallery/";
        
        $PicturesManager = new Default_Model_PicturesManager();
        $PicturesManager->folder = $folder;
        $folders = $PicturesManager->getFolder();
        
        //Initialize the Zend_Paginator
        $paginator = Zend_Paginator::factory($folders);
        
        //Set the properties for the pagination
        $paginator->setItemCountPerPage(20);
        $paginator->setPageRange(10);
        $paginator->setCurrentPageNumber($currentPage);
        
        $this->view->folders = $paginator;
    }
> in view:

Code: Select all

if(is_array($this->folders)) {
    foreach($this->folders as $folder) {
        echo $folder[''foldername] ."<br>";
    }
} else {
    echo "empty";
}
can anybody help me ? thanks before.. :D

Re: [ASK] Pagination Data in Array with Zend Framework ?

Posted: Sun Feb 28, 2010 2:12 pm
by Darhazer
Are you sure the $result array is not empty?
By the way you have to use:

Code: Select all

while(($folder = readdir($open)) !== false) {

Edit:
what is the value of $this->folder? Does it have trailing slash? Without / at the end, this won't work :)

Re: [ASK] Pagination Data in Array with Zend Framework ?

Posted: Mon Mar 01, 2010 10:33 am
by zipcoi
Darhazer wrote:Are you sure the $result array is not empty?
I'm sure it's not empty.. the result is the list name of all folder that exist.. if i'm not use pagination control, it work fine.. but when I use pagination control, it doesn't show anything..
Darhazer wrote: By the way you have to use:

Code: Select all

while(($folder = readdir($open)) !== false) {
ok2.. thanks for the advise..
Darhazer wrote: Edit:
what is the value of $this->folder? Does it have trailing slash? Without / at the end, this won't work :)
$this->folder = "pictures/gallery/";

Re: [ASK] Pagination Data in Array with Zend Framework ?

Posted: Mon Mar 01, 2010 12:45 pm
by Darhazer
wow, I just noticed that you are not using the paginator in your viewscript at all. I thought that your paginator works with data from database but not with array. It seems that your viewscript works without paginator, but not with the paginator.

Code: Select all

echo $this->partialLoop('partials/folder.phtml', $this->paginator);
echo $this->paginationControl($this->paginator, 'Sliding', 'partials/pagination.phtml');
 
partials/folders.phtml:

Code: Select all

<?php echo $this->foldername; ?>
Btw I've just noticed another error in your viewscript:
echo $folder[''foldername] ."<br>";
should be
echo $folder['foldername'] ."<br>";

Re: [ASK] Pagination Data in Array with Zend Framework ?

Posted: Sun Mar 14, 2010 5:42 am
by zipcoi
the problem is fix now..

thanks for the help.. :D