Zend_Paginator + Query Paramenter (ZF1) - php

I am using the ZF1 with Zend_Paginator but if I have a search that brings all data without filter, works perfectly, but if I have a query filter paging works only on the first page.
public function getConsultaTelefones($setor)
{
if ($setor != "") {
$select = $this->select()
->setIntegrityCheck ( false )
->from ( array('t' => 'sca_telefone'))
->joinInner ( array ('p' => 'sca_posto'), 't.idPosto = p.idPosto')
->joinInner ( array ('l' => 'sca_lotacao'), 't.idLotacao = l.idLotacao')
->where("l.idLotacao = ?", $setor) ;
$resultado = $this->fetchAll($select);
} else {
$select = $this->select()
->setIntegrityCheck ( false )
->from ( array('t' => 'sca_telefone'))
->joinInner ( array ('p' => 'sca_posto'), 't.idPosto = p.idPosto')
->joinInner ( array ('l' => 'sca_lotacao'), 't.idLotacao = l.idLotacao');
$resultado = $this->fetchAll($select);
}
return $resultado;
}
In the parameter $ sector it brings all the data as the filter and creates the correct pagination, but only the first page because if I click the next or last page he "recreates" the paging all table data.
public function paginacaoConsulta($dados)
{
$pagina = intval($this->_getParam('pagina', 1));
$paginator = Zend_Paginator::factory($dados);
$paginator->setItemCountPerPage(5);
$paginator->setPageRange(7);
$paginator->setCurrentPageNumber($pagina);
$this->view->paginator = $paginator;
}
public function consultarAction()
{
$setor = $this->_getParam('setor');
$modLotacao = new Sca_Model_Lotacao('sca');
$resultado = $modLotacao->getConsultaTelefones($setor);
$this->paginacaoConsulta($resultado);
}
Already researched several things but nothing has met me. Please I need help.
Follow the rest of the codes:
<?php echo $this->paginationControl($this->paginator, 'Sliding', 'paginacao.phtml'); ?>
<nav>
<ul class="pagination">
<li>
<!-- Link para a primeira página -->
<?php if (isset($this->previous)): ?>
<a title="Primeira Página" href="<?php echo $this->url(array('pagina' => $this->first)); ?>" aria-label="Previous">« Primeira</a>
<?php else: ?>
<a class="current" title="Primeira Página" href="<?php echo $this->url(array('pagina' => $this->first)); ?>" aria-label="Previous">« Primeira</a>
<?php endif; ?>
</li>
<li>
<?php if ($this->pageCount): ?>
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('pagina' => $this->previous)); ?>" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
<?php else: ?>
<span aria-hidden="true">«</span>
<?php endif; ?>
</li>
<li>
<?php foreach ($this->pagesInRange as $pagina): ?>
<?php if ($pagina != $this->current): ?>
<a href="<?php echo $this->url(array('pagina' => $pagina)); ?>">
<?php echo $pagina; ?>
</a>
<?php else: ?>
<?php echo $pagina; ?>
<?php endif; ?>
<?php endforeach; ?>
</li>
<li>
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url(array('pagina' => $this->next)); ?>" aria-label="Next" >
<span aria-hidden="true">»</span>
</a>
<?php else: ?>
<span aria-hidden="true">»</span>
<?php endif; ?>
<?php endif; ?>
</li>
<li>
<!-- Última página -->
<?php if (isset($this->next)): ?>
<a title="Última Página" href="<?php echo $this->url(array('pagina' => $this->last)); ?>" aria-label="Next">Última »</a>
<?php else: ?>
<a class="current" title="Última Página" href="<?php echo $this->url(array('pagina' => $this->last)); ?>" aria-label="Next">Última »</a>
<?php endif; ?>
</li>
</ul>
</nav>

You have to pass to query param in next and previous link. Like this:
<a href="<?php echo $this->url(array('pagina' => $this->previous, 'setor' => $this->setor)); ?>" aria-label="Previous">
and pass it to the view:
$this->view->setor = $this->_getParam('setor');

Related

Amateur trying to fix dropdown menu from SQL

I'm trying to help a friend with a website. I usually don't work with PHP, jquery.
The dropdown has 4 levels. The first level has 4 points. The 4 points have their own sub-levels, different for each other.
I'm trying to find them and then make them display in a dropdown, directly from the database.
And I'm stuck at the second level, with this error
Notice: Trying to get property 'subcategorie' of non-object in .....
What I've managed to do, until now:
<?php $categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategorie = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach($categorii as $categorie) {
?>
<ul>
<li>
<a <?php if(isset($_GET['categorie']) && $categorie->categorie==$_GET['categorie']){ echo "btn-success";}else{ echo "btn-info";};?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode($categorie->categorie); ?>"> <i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie ; ?> </a>
<ul class="">
<li> <?php echo $subcategorie->subcategorie; ?>
</li>
</ul>
</li>
</ul>
</a>
Hi You need to do a foreach loop for $subcategorieas you did for $categorii. Because you are getting an array and not a object as you got for $categorii. Here I set it around the li because it seems to be the best.
<?php
$categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategories = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach( $categorii as $categorie ) {
?>
<ul>
<li>
<a
<?php
#### What are these? Are they classnames? ####
if( isset( $_GET['categorie'] ) && $categorie->categorie==$_GET['categorie'] ){
echo "btn-success";
}else{
echo "btn-info";
};
?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode( $categorie->categorie ); ?>">
<i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie; ?>
</a>
<ul class="">
<?php foreach( $subcategories as $subcategorie ) { ?>
<li>
<a href="<?php echo site_url(); ?>/catalog-rural/?categorie=<?php echo urlencode( $_GET['categorie'] ); ?>&subcategorie=<?php echo $subcategorie->subcategorie; ?>" class="list-group-item <?php if($subcategorie->subcategorie==$_GET['subcategorie']){ echo "active";};?> ">
<?php echo $subcategorie->subcategorie; ?>
</a>
</li>
<?php } ?>
</ul>
</li>
</ul>
Not intended as a solution to your error " Trying to get property 'subcategorie' of non-object" ~ just to illustrate the invalid markup. If you use proper code indentation you would probably have been able to spot this!
<?php
$categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategorie = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach( $categorii as $categorie ) {
?>
<ul>
<li>
<a
<?php
#### What are these? Are they classnames? ####
if( isset( $_GET['categorie'] ) && $categorie->categorie==$_GET['categorie'] ){
echo "btn-success";
}else{
echo "btn-info";
};
?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode( $categorie->categorie ); ?>">
<i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie; ?>
</a>
<ul class="">
<!-- ### Where is the opening LI? ### -->
<a href="<?php echo site_url(); ?>/catalog-rural/?categorie=<?php echo urlencode( $_GET['categorie'] ); ?>&subcategorie=<?php echo $subcategorie->subcategorie; ?>" class="list-group-item <?php if($subcategorie->subcategorie==$_GET['subcategorie']){ echo "active";};?> ">
<?php echo $subcategorie->subcategorie; ?>
</a>
</li>
</ul>
</li>
</ul>
</a><!-- ### what is this closing? ### -->

ZF2 pagination Passing Value From Route

Within my Zend Framework project I have a "base-asset" module. Within this I am trying to create a pagination that will match the following route:
'route' => '/video/search[/:search][/:page_reference]',
The purpose of the value of search is so the same query will run on the database if the user clicks on any of the "next" page links created.
The value for page_reference is working fine. It is the value for search that I am not sure of how to populate for the paginate:
<li>
<a href="<?php echo $this->url(
$this->route,
['page_reference' => $this->previous , 'search' => $this->search]
); ?>"> << </a>
</li>
The following attempts have been unsuccessful:
$this->search
$this->params()->fromRoute('search')
The pagination is called from the view using
{{ paginationControl(result, 'sliding', ['actsministries/asset/searchpaginator', 'AMBase']) }}
Should I be setting the value for search in the Service?
1 - Create a view helper :
use Zend\View\Helper\AbstractHelper;
class Requesthelper extends AbstractHelper
{
protected $request;
//get Request
public function setRequest($request)
{
$this->request = $request;
}
public function getRequest()
{
return $this->request;
}
public function __invoke()
{
return $this->getRequest()->getServer()->get('QUERY_STRING');
}
}
2 - Module.php
public function getViewHelperConfig()
{
return array(
'invokables' => array(
),
'factories' => array(
'Requesthelper' => function($sm){
$helper = new YourModule\View\Helper\Requesthelper; \\the path to creatd view helper
$request = $sm->getServiceLocator()->get('Request');
$helper->setRequest($request);
return $helper;
}
),
);
}
3 - searchpaginator.phtml :
<?php
$parameterGet = $this->Requesthelper();
if ($parameterGet != ""){
$aParams = explode("&", $parameterGet);
foreach($aParams as $key => $value){
if(strpos($value, "page_reference=") !== false){
unset($aParams[$key]);
}
}
$parameterGet = implode("&", $aParams);
if($parameterGet != '')
$parameterGet = "&".$parameterGet;
}
?>
<?php if ($this->pageCount):?>
<div>
<ul class="pagination">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<li>
<a href="<?php echo $this->url($this->route); ?>?page_reference=<?php echo $this->previous.$parameterGet; ?>">
<<
</a>
</li>
<?php else: ?>
<li class="disabled">
<a href="#">
<<
</a>
</li>
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<li>
<a href="<?php echo $this->url($this->route);?>?page_reference=<?php echo $page.$parameterGet; ?>">
<?php echo $page; ?>
</a>
</li>
<?php else: ?>
<li class="active">
<?php echo $page; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<li>
<a href="<?php echo $this->url($this->route); ?>?page_reference=<?php echo $this->next.$parameterGet; ?>">
>>
</a>
</li>
<?php else: ?>
<li class="disabled">
<a href="#">
>>
</a>
</li>
<?php endif; ?>
</ul>
</div>
<?php endif; ?>
4 - index.phtml
<?php
// add at the end of the file after the table
echo $this->paginationControl(
// the paginator object
$this->paginator,
// the scrolling style
'sliding',
// the partial to use to render the control
'partial/searchpaginator.phtml',
// the route to link to when a user clicks a control link
array(
'route' => '/video/search'
)
);
?>
I hope that's what you look for

Pagination in Zend not working

I have an array of items. Items are files paths. I have set up pagination but when I click on next it does not change. I want to display 1 pdf per page. Below is my code
class IndexController extends Zend_Controller_Action
{
public function init(){}
public function indexAction()
{
if($_SERVER['SERVER_NAME']=="portal-dev"){
$location = "/data/scan/invoice";
$listOfFiles = $this->readFiles($location);
$paginator = Zend_Paginator::factory($listOfFiles);
$paginator->setItemCountPerPage(1);
$this->view->paginator = $paginator;
}
}
public function readFiles($location){
$pattern="(\.pdf$)"; //valid image extensions
$thelist = array();
if ($handle = opendir($location)) {
while (false !== ($file = readdir($handle)))
{
if (eregi($pattern, $file)) //if this file is a valid image
{
$thelist[] = $file; //insert files into array
$max = count($thelist) - 1;
$max1 = count($thelist);
}
}
closedir($handle);
}
return $thelist;
}
}
index.phtml
<div id="main">
<?php if(!$this->paginator){
echo "No Files to process";
}else{
?>
<table>
<tr>
<td rowspan=2></td>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
<li><?php echo $item; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php echo $this->paginationControl($this->paginator,'Sliding','partial/my_pagination_control.phtml'); ?>
<?php }?>
</div>
pagination is found in directory partial
<?php if ($this->pageCount): ?>
<div
class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
Previous </a> <span class="bar"> | </span>
<?php else: ?>
<span class="disabled"> Previous</span> <span class="bar"> | </span>
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?php echo $this->url(array('page' => $page)); ?>"> <?php echo $page; ?>
</a> <span class="bar"> | </span>
<?php else: ?>
<?php echo $page; ?>
<span class="bar"> | </span>
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url(array('page' => $this->next)); ?>"> Next
</a>
<?php else: ?>
<span class="disabled">Next </span>
<?php endif; ?>
</div>
<?php endif; ?>
Try after setting the current page number in indexAction
$paginator->setCurrentPageNumber($this->_getParam('page'));
Like this
$paginator = Zend_Paginator::factory($listOfFiles);
$paginator->setItemCountPerPage(1);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$this->view->paginator = $paginator;

Get Magento Categories with Especific Sort of Admin Panel

I wrote the code below to put together a customized menu of categories. Everything works fine, but would like the order of the categories were the same order as defined in the administrator panel where there drap and drop functionality.
<?php
$subCats = Mage::getModel('catalog/category')->load(76)->getChildren();
$dispositosCatIds = explode(',',$subCats);
?>
<ul class="menu">
<?php $controleNum = 0; ?>
<?php foreach($dispositosCatIds as $dispositoCatId): ?>
<?php $aparelhoCat = Mage::getModel('catalog/category')->load($dispositoCatId); ?>
<?php if($aparelhoCat->getIsActive()): ?>
<li class="<?php print $controleNum ? '' : 'submenu first'; ?>"><a class="drop" href="<?php echo $aparelhoCat->getUrl(); ?>"> <span><?php echo $aparelhoCat->getName(); ?></span></a> <!--Begin 6 column Item -->
<div class="dropdown_6columns">
<div class="inner"><span class="title"><?php echo $aparelhoCat->getName(); ?></span>
<div class="col_2">
<div class="col_2 firstcolumn"><img src="<?php echo $aparelhoCat->getImageUrl(); ?>" alt="<?php echo $aparelhoCat->getName(); ?>" /></div>
</div>
<div class="col_4" style="margin-bottom: 20px;">
<?php echo $aparelhoCat->getDescription(); ?>
</div>
<div class="col_2 categorias-super"><span class="title_col">Produtos para <?php echo $aparelhoCat->getName(); ?></span>
<?php $subSubCats = $aparelhoCat->getChildrenCategories();?>
<?php if (count($subSubCats) > 0): ?>
<?php //$controleNumLI = 0; ?>
<ul style="list-style: none; float: none !important;">
<?php foreach($subSubCats as $_subcategory): //Rodando entre as categorias de Um dispositivo ?>
<?php if($_subcategory->getIsActive()): ?>
<li class="level1 <?php //print $controleNumLI ? '' : 'first'; ?>"> <?php echo $_subcategory->getName(); ?></li>
<?php //$controleNumLI += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
</li>
<?php $controleNum += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
I tried to use other modes (based here) can do this, but I could not. The problem that the function returns getChildren() is a string with IDs in ascending order.
Some Ideas?
This is the code I use to display category in a dropdown box in the order of the admin... the key is setOrder('path','ASC')
$categories = array();
$_categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('path','ASC')
->addAttributeToSelect('name')->load();
foreach($_categories as $cat){
$level = $cat->getLevel() - 1;
$pad = str_repeat("----", ($level > 0) ? $level : 0);
$categories[] = array('value' => $cat->getEntityId(), 'label' => $pad . ' ' . $cat->getName());
}
print_r($categories);
You could do something like this: create array tree from array list
I got it:
$dispositovosCategoryId = 76;
$dispositovosCategoryIds = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('parent_id', array('eq'=>$dispositovosCategoryId))
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('position','ASC')
->addAttributeToSelect('name','url','image')
->load();

Create a dynamic XML href in PHP with SimpleXML

I'm trying to create a link based on a URL variable using only the last five digits of its respective line of XML data.
For example, if the XML link is http://events.stanford.edu/events/213/21389 how can I create this link a href="e/?i=21389?
Here's my page, XML and code:
<?php
// Build the XML file path, using URL variable $c (above)
$c = $_GET['c'];
$p ="http://events-prod.stanford.edu/xml/byCategory/";
$e = "/mobile.xml";
$file = "$p$c$e";
$xml = simplexml_load_file($file);
?>
<h1><?php echo $xml->title; ?></h1>
Home
</div><!-- /header -->
<div data-role="content">
<?php // Only display if there are events ?>
<?php if (isset($xml->Event->title)) { ?>
<ul data-role="listview">
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<?php if ($event->Media->url != null) { ?>
<img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
<h3><?php echo $event->title; ?></h3>
<p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
<p><?php echo $event->locationText; ?></p>
</a>
</li>
<?php } ?>
</ul>
<?php } else { ?>
<?php echo '<p>There is currently nothing scheduled for ', $xml->title, '.</p>';?>
<?php } ?>
I'm using short tags, because I think you'll agree it's easier now to read the code as oppose to before.
<?
$controller ="http://events-prod.stanford.edu/xml/byCategory/";
$category_id = $_GET['c'];
$category_id = 0;
$xml = "/mobile.xml";
$url = $controller . $category_id . $xml;
$xml_object = simplexml_load_file($url);
?>
<div>
<h1><?= $xml_object->title ?></h1>
Home
</div>
<div data-role="content">
<? if (!empty($xml_object->Event->title)): ?>
<ul data-role="listview">
<? foreach($xml_object->Event as $event): ?>
<li>
<?
$pattern = '/[0-9]+$/';
$matches = array();
preg_match($pattern, $event->link, $matches);
$my_link = 'e/?i=' . $matches[0];
?>
<a href="<?= $my_link ?>">
<? if (!empty($event->Media->url)): ?>
<img src="<?= $event->Media->url ?>" alt="<?= $event->title ?> thumbnail" />
<? endif; ?>
<h3><?= $event->title ?></h3>
<p><strong><?= $event->beginDate ?> at <?= $event->beginTime ?></strong></p>
<p><?= $event->locationText ?></p>
</a>
</li>
<? endforeach; ?>
</ul>
<? else: ?>
<? echo '<p>There is currently nothing scheduled for ', $xml_object->title, '.</p>'; ?>
<? endif; ?>
</div>

Categories