phpzend-frameworkzend-paginator

How to reuse the paginationControl partial while passing different parameters to each instance


I'm using the paginationControl partial, and passing parameters through the 4th parameter to allow me to paginate "filtered" results. That's working fine, but I'd like to be able to use the same partial for all instances of pagination even though they'll use different parameters.

e.g. "properties" are filtered by no. bedrooms

giving a 4th paramater in the instance of the pagination control in the view script of...

array('beds' => '$beds')

and used in the partial...

$this->beds

whereas "clients" are filtered by location

giving a 4th paramater in the instance of the pagination control in the view script of...

array('location' => '$location')

and used in the partial...

$this->location

How to best accomplish this? I could access the 4th parameter as an array of keys and values, and loop over the array and build the arguments for the url view helper within the paginationControl partial IF I could work out how to access the array. But but perhaps that's not the approach to take anyway..

Thanks.

EDIT:

Here's the partial I'm using to output the pagination controls as requested in the comments.

    <div class="pagination">
      <div class="pages">

          <!-- First page link -->
             <?php if (isset($this->previous)): ?>
                <a href="<?= $this->url() . "?" . http_build_query(array('group_id' => $this->group_id, 'page' => $this->first)); ?>">Start</a>
          <?php else: ?>
                  <span class="disabled">Start</span>
          <?php endif; ?>

          <!-- Previous page link -->
          <?php if (isset($this->previous)): ?>
                <a href="<?= $this->url() . "?" . http_build_query(array('group_id' => $this->group_id, '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() . "?" . http_build_query(array('group_id' => $this->group_id, 'page' => $page)); ?>"><?= $page; ?></a>
              <?php else: ?>
                  <span class="current"><?= $page; ?></span>
              <?php endif; ?>
          <?php endforeach; ?>

          <!-- Next page link -->
          <?php if (isset($this->next)): ?>
                <a href="<?= $this->url() . "?" . http_build_query(array('group_id' => $this->group_id, 'page' => $this->next)); ?>">Next</a>
          <?php else: ?>
               <span class="disabled">Next</span>
          <?php endif; ?>

          <!-- Last page link -->
          <?php if (isset($this->next)): ?>
                <a href="<?= $this->url() . "?" . http_build_query(array('group_id' => $this->group_id, 'page' => $this->last)); ?>">End</a>
          <?php else: ?>
              <span class="disabled">End</span>
          <?php endif; ?>
      </div>
    </div>

UPDATE:

I have accepted RockyFords solution because it answers the basic question of accessing the parameters passed through the 4th parameter in the instantiation of the pagination controls partial. However, I include my complete fina partial here in case another reader wants the resultant parameters to generate a query string.

<div class="pagination">
  <div class="pages">
 <?php 
   $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
   unset($params['controller']);
   unset($params['action']);
 ?>


  <?= $this->first ?> 
  <!-- First page link -->
  <?php if (isset($this->previous)): ?>
        <a href="<?= $this->url() . "?" . http_build_query(array_merge($params , array('page' => $this->first))); ?>">Start</a>
  <?php else: ?>
        <span class="disabled">Start</span>
  <?php endif; ?>

  <!-- Previous page link -->
  <?php if (isset($this->previous)): ?>
        <a href="<?= $this->url() . "?" . http_build_query(array_merge($params , 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() . "?" . http_build_query(array_merge($params , array('page' => $page))); ?>"><?= $page; ?></a>
      <?php else: ?>
          <span class="current"><?= $page; ?></span>
      <?php endif; ?>
  <?php endforeach; ?>

  <!-- Next page link -->
  <?php if (isset($this->next)): ?>
        <a href="<?= $this->url() . "?" . http_build_query(array_merge($params , array('page' => $this->next))); ?>">Next</a>
  <?php else: ?>
       <span class="disabled">Next</span>
  <?php endif; ?>

  <!-- Last page link -->
  <?php if (isset($this->next)): ?>
        <a href="<?= $this->url() . "?" . http_build_query(array_merge($params , array('page' => $this->last))); ?>">End</a>
  <?php else: ?>
      <span class="disabled">End</span>
  <?php endif; ?>


Solution

  • I think I get it...

    Try something like:

    <?php
    if ($this->pageCount) :
        //you need to add each of the request parameters to url
        $params = Zend_Controller_Front::getInstance()
                        ->getRequest()->getParams();
        //remove the system parameters, $this->url will put them back
        unset($params['module']);
        unset($params['controller']);
        unset($params['action']);
        ?>
        <div class="paginationControl">         
                        <!--First Page Link -->
                        <?php if (isset($this->first)): ?>
                            <a href="<?php
                    echo $this->url(array_merge($params, array('page' => $this->first)))
                            ?>">
                                &lt; First</a>
                        <?php else : ?>
                            <span class="disabled">&lt; First</span>
                        <?php endif ?>
    
                        <!--Previous Page Links-->
                        <?php if (isset($this->previous)) : ?>
                            <a href="<?php
                    echo $this->url(array_merge($params, array('page' => $this->previous)))
                            ?>">
                                &lt; Prev</a>
                        <?php else : ?>
                            <span class="disabled">&lt; Prev</span>
                        <?php endif ?>
                    <!--truncated ...-->
    

    With a pagination control similar to this you'll be able to pass any user set parameters back into the url. The first if() above the is the really important part.