On Fri, Feb 13, 2009 at 2:54 PM, Deepak Shrestha <[email protected]> wrote:
> Let me explain a bit so that it will be clear about
> 1. what I am trying to do
> 2. where I am doing wrong and
> 3. where I need to fill up the missing pieces.
>
>
> My front page is where all the records are shown so I wanted to use
> the paginator. For the simplicity I want to fetch all the records from
> database and paginate.
>
> My concept of doing that:
> ====================
> 1. IndexController will delegate the work to Model (which have access
> to DBTable)
> 2. Model instantiates the Paginator and returns to controller
> 3. Controller then assign the paginator to view.
> 4. View will loop through the probided paginator to render the records
> plus show paginator control.
> ====================
>
> Now some code
>
> My controller:
> ==============
> public function indexAction()
> {
> $this->showAllEntries();
> }
>
>
> private function showAllEntries()
> {
> $newsmodel = $this->_getNewsModel();
> $this->view->paginator = $newsmodel->fetchAllEntries();
> }
>
> =============
>
> My Model:
> ==============
> public function fetchAllEntries()
> {
> $table = $this->getTable();
> $select = $table->select()->order('newsdate desc');
> $pgadapter = new Zend_Paginator_Adapter_DbTableSelect($select);
> $paginator = new Zend_Paginator($pgadapter);
> $paginator->setItemCountPerPage(5);
> //$paginator->setCurrentPageNumber(1); //should be dynamic I guess
> return $paginator;
> }
>
> public function getTable()
> {
> if (null === $this->_table) {
> require_once APPLICATION_PATH . '/models/DbTable/NewsTable.php';
> $this->_table = new Model_DbTable_NewsTable;
> }
> return $this->_table;
> }
>
> ====================
>
> My index view script and paginator control script is in the same directory
> ===================
> views
> scripts
> index
> index.phtml
> mypaginator.phtml
> ==================
>
>
> My view script (index.phtml)
> ======================
> <div id="newslist">
> <? if ($this->paginator ): ?>
> <? foreach ($this->paginator as $entry): ?>
> <div id="ads">
> <ul>
> <li id="adtitle"><?=
> $this->escape($entry['newstitle']) ?></li>
> <li id="addetails"><?=
> $this->escape($entry['newsdetails']) ?></li>
> </ul>
> </div>
> <? endforeach ?>
> <? endif ?>
> </div>
>
> <div id="pgcontrol">
> <?= $this->paginationControl($this->paginator, 'Sliding',
> 'index/mypaginator.phtml'); ?> //<<<<<<<<<<please take note of this if
> this is ok
> </div>
> ====================
>
> my Paginator script (same as provided in documentation search style)
> =======================
>
> <!--
> See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
> -->
>
> <?php if ($this->pageCount): ?>
> <div class="paginationControl">
> <!-- Previous page link -->
> <?php if (isset($this->previous)): ?>
> <a href="<?= $this->url(array('page' => $this->previous)); ?>">
> < Previous
> </a> |
> <?php else: ?>
> <span class="disabled">< Previous</span> |
> <?php endif; ?>
>
> <!-- Numbered page links -->
> <?php foreach ($this->pagesInRange as $page): ?>
> <?php if ($page != $this->current): ?>
> <a href="<?= $this->url(array('page' => $page)); ?>">
> <?= $page; ?>
> </a> |
> <?php else: ?>
> <?= $page; ?> |
> <?php endif; ?>
> <?php endforeach; ?>
>
> <!-- Next page link -->
> <?php if (isset($this->next)): ?>
> <a href="<?= $this->url(array('page' => $this->next)); ?>">
> Next >
> </a>
> <?php else: ?>
> <span class="disabled">Next ></span>
> <?php endif; ?>
> </div>
> <?php endif; ?>
> =============================
>
>
> When I open the site, it shows the first five entries together with
> paginator control. However when I click next, my url get changed from
> http://myweb/ to http://myweb/index/index/page/2 and doesn't go to
> next five records. (why there is two index in my url?)
>
> I know now that I need some kind to routing here. So my question is
> how to do that with my current setting?
>
>
> My bootstrap.php
> =================
>
> previously
> ----------------------------------
> $frontController = Zend_Controller_Front::getInstance();
> $frontController->setControllerDirectory(APPLICATION_PATH . '/controllers');
> $frontController->setParam('env', APPLICATION_ENVIRONMENT);
>
> now I added but not sure it this should be here. anyway this is not working
> ----------------------------------
> $router = $frontController->getRouter();
> $router->addRoute('index', new
> Zend_Controller_Router_Route('index/index/:page',
> array('module' => 'index', 'page' => 1,
> 'Controller' => 'index',
> 'Action' => 'index')));
> =======================================
>
> I am lost here. What else I need to know to get this thing done?
>
> Please look at what I have provided and guide me on filling the missing part.
>
> thanks
>
Ok I got some crash course on Zend_Controller_Router_Route and I am
beginning to understand what I should do.
By the way my current implementation is in the virtual host context
and the first page I see is the index page where all the records are
displayed by default. What is not clear to me is do I need to specify
router for the page numbers? currently I removed the router part from
bootstrap.php because index controllers index action is default in ZF.
And now modifications are
controller
==========
public function indexAction()
{
$request = $this->getRequest();
$pageno = $request->getParam('page');
$this->showAllEntries($pageno);
}
private function showAllEntries()
{
$newsmodel = $this->_getNewsModel();
$this->view->paginator = $newsmodel->fetchAllEntries();
}
============
and
Model:
==============
public function fetchAllEntries($page=null)
{
$table = $this->getTable();
$select = $table->select()->order('newsdate desc');
$pgadapter = new Zend_Paginator_Adapter_DbTableSelect($select);
$paginator = new Zend_Paginator($pgadapter);
$paginator->setItemCountPerPage(5);
if ($page == null)
{
$paginator->setCurrentPageNumber(1);
}
else
{
$paginator->setCurrentPageNumber($page);
}
return $paginator;
}
public function getTable()
{
if (null === $this->_table) {
require_once APPLICATION_PATH . '/models/DbTable/NewsTable.php';
$this->_table = new Model_DbTable_NewsTable;
}
return $this->_table;
}
====================
IFAIK I am correct in this part but it still doesn't work.
Is this because of two index in my url? why I am getting two index in
my url in the first place? I have no idea.
Need some advice
Thanks
--
=======================
Registered Linux User #460714
Currently Using Fedora 8, 10
=======================