[ASK] Pagination Data in Array with Zend Framework ?

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
zipcoi
Forum Newbie
Posts: 10
Joined: Tue May 06, 2008 2:55 am

[ASK] Pagination Data in Array with Zend Framework ?

Post 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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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 :)
zipcoi
Forum Newbie
Posts: 10
Joined: Tue May 06, 2008 2:55 am

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

Post 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/";
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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>";
zipcoi
Forum Newbie
Posts: 10
Joined: Tue May 06, 2008 2:55 am

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

Post by zipcoi »

the problem is fix now..

thanks for the help.. :D
Post Reply