ZF2 pagination Passing Value From Route - php

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

Related

My route somehow keep redirecting to wrong url

so
i try make a pos(point of sale) program using ci3
im trying add a feature where admin can make new menu and submenu
but something went wrong
when i goto controller "sistem"
my route show "sitename/sistem/sistem/menu" this route supposed to "sitename/sistem/menu". another controller also effected when i goto "dashboard" controller the route show
"sistem/dashboard"
this is sistem controller
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Sistem extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('M_menu');
$this->load->model('M_user');
if (!$this->session->userdata('email')) {
redirect('auth');
}
}
public function index()
{
$this->menu();
}
public function menu()
{
$data['judulpage'] = 'Manajemen Menu';
$data['datamenu'] = $this->M_menu->getmenus()->result_array();
$data['user'] = $this->M_user->getuserdata()->row_array();
$this->form_validation->set_rules('menutambah', 'Menu', 'required');
// $this->form_validation->set_message([
// 'required' => 'Field menu add harus diisi'
// ]);
if ($this->form_validation->run() == false) {
$this->load->view('template/header', $data);
$this->load->view('template/sidebar', $data);
$this->load->view('sistem/menu', $data);
$this->load->view('template/footer');
} else {
$menu['menu'] = $this->input->post('menutambah');
$this->M_menu->insertmenu($menu);
$this->session->set_flashdata('flash','Sukses');
redirect('sistem');
}
}
this is the sidebar.php i think this is the problem but i cant find the solution
<?php
$role_id = $this->session->userdata('role_id');
$queryMenu = "SELECT `user_menu`.`id`, `menu`, `icon_menu`
FROM `user_menu`
JOIN `user_access_menu` ON `user_menu`.`id` = `user_access_menu`.`menu_id`
WHERE `user_access_menu`.`role_id` = $role_id";
$menus = $this->db->query($queryMenu)->result_array();
?>
<!-- BEGIN: Main Menu-->
<div class="main-menu menu-fixed menu-dark menu-accordion menu-shadow" data-scroll-to-active="true">
<div class="main-menu-content">
<ul class="navigation navigation-main" id="main-menu-navigation" data-menu="menu-navigation">
<li class=" navigation-header"><span>MENU</span><i class=" feather icon-minus" data-toggle="tooltip" data-placement="right" data-original-title="Apps"></i>
</li>
<?php foreach($menus as $menu) : ?>
<?php
$menuid = $menu['id'];
$querysubmenu = "SELECT * FROM `user_sub_menu` WHERE `menu_id` = $menuid AND `is_active` = 1";
$submenus = $this->db->query($querysubmenu)->result_array();
?>
<li class=" nav-item"><i class="<?php echo $menu['icon_menu']; ?>"></i><span class="menu-title"><?php echo strtoupper($menu['menu']); ?></span>
<ul class="menu-content">
<?php foreach($submenus as $submenu) : ?>
<li>
<a class="menu-item" href="<?php echo $submenu['url']; ?>"><i class="<?php echo $submenu['icon']; ?>"></i><span><?php echo $submenu['title']; ?></span></a>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
i take the url from database.
u can see from inspect mode, the href redirecting to sistem/menu which is coorect url but when i click it, it goto sistem/sistem/menu
You do not need to call double <?php ?> tag.
just do this
<?php echo base_url($submenu['url']); ?>
or simplify the echo syntax with
<?= base_url($submenu['url']); ?>
fixed href="<?php echo base_url(); ?> i add / this before php tag but somehow it worked but still directing to wrong url localhost/sistem/menu it supposed to localhost/site/sistem/menu, so i add base_url to add my sitename and it work, thx
<li>
<a class="menu-item" href="<?php echo base_url(); ?><?php echo $submenu['url']; ?>">
<i class="<?php echo $submenu['icon']; ?>"></i><span><?php echo $submenu['title']; ?></span></a>
</li>

sending data view to controller in codeigniter

my view
<div class="dropdown1">
<button class="dropbtn1">Asia</button>
<div class="dropdown1-content">
<li>
<ul style="height:520px; overflow: auto">
<?php foreach ($result as $row) {?>
<li>
<a href="<?php echo base_url(); ?>index.php/MyWeb_Controller/countriesView/<?php echo $row['count_name']?>">
<?php echo $row['count_name'];?>
</a>
</li>
<?php } ?>
</ul>
</li>
</div>
</div>
my controller
public function countriesView($a){
$this->load->helper('url');
$this->load->view('Country_View');
var_dump($a);die();
}
the result is show like this
string(12) "%20%20BRUNEI"
i want to show only the country name 'BRUNEI'
You can use - (dash) instant of space because space value change in url like %20%. Try the following code i have replace space to - in view and revert it on controller
View :
<div class="dropdown1">
<button class="dropbtn1">Asia</button>
<div class="dropdown1-content">
<li>
<ul style="height:520px; overflow: auto">
<?php foreach ($result as $row) {?>
<li>
<?php $county_name = str_replace(' ','-',$row['count_name']); ?>
<a href="<?php echo base_url(); ?>index.php/MyWeb_Controller/countriesView/<?php echo $county_name; ?>">
<?php echo $row['count_name'];?>
</a>
</li>
<?php } ?>
</ul>
</li>
</div>
</div>
Controller :
public function countriesView($a){
$this->load->helper('url');
$this->load->view('Country_View');
$a = str_replace('-',' ',$a);
var_dump($a);die():
}

How to assign html code to variable in codeigniter?

I want to assign some html code to variable and that variable is in controller.
headerview.php
<header id="header" class="header">
<div class="container">
<!-- LOGO -->
<div class="logo"><img src="<?php echo base_url()?>public/images/logo.png" alt=""></div>
<!-- END / LOGO -->
<!-- NAVIGATION -->
<nav class="navigation">
<div class="open-menu">
<span class="item item-1"></span>
<span class="item item-2"></span>
<span class="item item-3"></span>
</div>
<!-- MENU -->
<ul class="menu">
<?php if($isAdmin ){ ?>
<?php
if($title=='Dashboard'){
?>
<li class="current-menu-item">Admin Dashboard</li>
<?php
}else{
?>
<li>Admin Dashboard</li>
<?php
}
?>
<?php } else { ?>
<?php
if($title=='Dashboard'){
?>
<li class="current-menu-item">User Dashboard</li>
<?php
}else{
?>
<li>User Dashboard</li>
<?php
}
?>
<?php } ?>
<?php
if($title=='Courses' || $title=='Add/Edit Categories' || $title=='Upload Manager'){
?>
<li class="current-menu-item">
Courses
<ul class="sub-menu">
<li>Categories</li>
<li>Courses</li>
<li>Upload Manager</li>
</ul>
</li>
<?php
}else{
?>
<li>
Courses
<ul class="sub-menu">
<li>Categories</li>
<li>Courses</li>
<li>Upload Manager</li>
</ul>
</li>
<?php
}
?>
<?php
if($title=='Users' || $title=='User Roles' || $title=='Subscription'){
?>
<li class="current-menu-item">
Users
<ul class="sub-menu">
<li>Roles</li>
<li>Subscription</li>
</ul>
</li>
<?php
}else{
?>
<li>
Users
<ul class="sub-menu">
<li>Roles</li>
<li>Subscription</li>
</ul>
</li>
<?php
}
?>
<?php
if($title=='Sales' || $title=='Payout Details'){
?>
<li class="current-menu-item">
Sales
<ul class="sub-menu">
<li>Transactions</li>
<li>Payouts</li>
</ul>
</li>
<?php
}else{
?>
<li>
Sales
<ul class="sub-menu">
<li>Transactions</li>
<li>Payouts</li>
</ul>
</li>
<?php
}
?>
</ul>
<!-- END / MENU -->
<!-- LIST ACCOUNT INFO -->
<ul class="list-account-info">
<!-- MESSAGE INFO -->
<!-- END / MESSAGE INFO -->
<!-- NOTIFICATION -->
<!-- END / NOTIFICATION -->
<li class="list-item account">
<?php
$userId=$this->session->userdata('cp_adminid');
$profilePic="";
$userQ=$this->Adminmodel->getuser();
foreach ($userQ->result() as $rowuser){
$profilePic=$rowuser->profilePic;
}
?>
<div class="account-info item-click">
<?php if($profilePic!=''){ ?>
<img alt="" src="<?php echo base_url();?>private/<?php echo $userId;?>/<?php echo $profilePic;?>">
<?php }else{?>
<img src="<?php echo base_url()?>public/images/default-profile_pic.png" alt="">
<?php }?>
</div>
<div class="toggle-account toggle-list">
<ul class="list-account">
<li><i class="icon md-config"></i>Setting</li>
<li><i class="icon md-arrow-right"></i>Sign Out</li>
</ul>
</div>
</li>
</ul>
<!-- END / LIST ACCOUNT INFO -->
</nav>
<!-- END / NAVIGATION -->
</div>
</header>
This header is come to all pages in application. So I want it to assign some variable called headerContent and it is located in Admin_controller. That render function called from any another controller that extends Admin_controller and then it loads the headeview to that pages.
Admin_controller
class Admin_controller extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model("Adminmodel","",true);
$headerview = 'headerview';
$this->render($headerview); # calling render() function in same class
}
protected $headerview = 'headerview';
protected function render($headerContent) {
$view_data = array( 'headerContent' => $headerContent);
$this->load->view($this->headerview);
}
}
Another controller below that extends Admin_controller called this render function.
Adminhome_controller.php
<?php
require APPPATH . '/controllers/admin/Admin_controller.php';
class Adminhome extends Admin_Controller{
public function __construct(){
parent::__construct();
$this->load->model("Adminmodel","",true);
public function index(){
$data['content']=$this->load->view("admin/adminhomeview",'',true);
$data['title']='Dashboard';
$adminId=$this->session->userdata('cp_adminid');
$permissions=$this->Adminmodel->getpermissions($adminId);
$row = $permissions->row();
$data['isAdmin'] = 0;
if($row->view == 1 or $row->add == 1 or $row->edit == 1 or $row->deleteRole == 1 ){
$data['isAdmin'] = 1;
}
$this->render($headerContent);
// $this->load->view("admin/headerview",$data);
}
}
?>
But it showing error like Message: Undefined variable: headerContent in Adminhome_controller.php.
you culd create a template view file containing
template view:
<html>
<?php $data = array('permission' => $permission); ?>
<?php $this->load->view('templateincludes/header',$data); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('templateincludes/footer'); ?>
</html>
controler:
$data = array('permission' => '1', 'main_content' => 'folder_to_your_view/view_file_you_want_to_load');
$this->load->view('template', $data);
header view:
<?= $permission ?>
But it showing error like Message: Undefined variable: headerContent
in Adminhome_controller.php
There is no variable with the name $headerContent inside extended class. Seems like you want access the parent/base class's variable.
To overcome this, you can declare protected field inside Parent Controller (Admin_controller.php), and access it inside child controller :
class Admin_controller extends CI_Controller{
protected $headerContent; // declare protected field
protected $headerview = 'headerview';
function __construct()
{
parent::__construct();
$this->load->model("Adminmodel","",true);
$headerview = 'headerview';
$this->render($headerview); # calling render() function in same class
}
protected function render($headerContent) {
$view_data = array( 'headerContent' => $headerContent);
$this->headerContent = $headerContent; // assign value into $headerContent field
$this->load->view($this->headerview);
}
}
And you can access it directly inside child class (Adminhome_controller.php) by using :
$this->render($this->headerContent);

Zend_Paginator + Query Paramenter (ZF1)

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');

ZF2 Breadcrumbs Helper - How to render it as an unordered list?

I'm new to Zend Framework2 and I´m asking for advice for the following situation:
I´m using the ZF2 Breadcrumbs Helper to create breadcrums on my project and this code:
//breadcrumbs.phtml
echo $this->navigation('Navigation')
->breadcrumbs()
->setLinkLast(false) // link last page
->setMaxDepth(7) // stop at level 7
->setMinDepth(0) // start at level 0
->setSeparator(' »' . PHP_EOL); // separator with newline
when rendered looks like this:
Home » Lorem Ipsum1 » Lorem Ipsum2 » Current Crumb
Exploring the source code:
<div id="breadcrumbs">
Home
»
Lorem Ipsum1
»
Lorem Ipsum2
» Current Crumb
</div>
So, my question is: using the Breadcrumb Helper how can I get the following source code to be able to style the Breadcrumbs the way I want to?
<div id="breadcrumbs">
<ul id="breadcrumbs">
<li>Home</li>
<li>Lorem Ipsum1</li>
<li>Lorem Ipsum2</li>
<li>Current Crumb</li>
</ul>
</div>
You can use the view helper to set a partial to use:
<?php $partial = array('application/navigation/breadcrumbs.phtml', 'default') ?>
<?php $this->navigation('navigation')->breadcrumbs()->setPartial($partial) ?>
<?php echo $this->navigation('navigation')->breadcrumbs()->render() ?>
And then your partial would be something like this (application/navigation/breadcrumbs.phtml):
<?php $container = $this->navigation()->breadcrumbs() ?>
<?php /* #var $container \Zend\View\Helper\Navigation\Breadcrumbs */ ?>
<?php $navigation = $container->getContainer() ?>
<?php /* #var $navigation \Zend\Navigation\Navigation */ ?>
<ul class="breadcrumb">
<li>Home <span class="divider">/</span></li>
<?php foreach($this->pages as $page): ?>
<?php /* #var $page \Zend\Navigation\Page\Mvc */ ?>
<?php if( ! $page->isActive()): ?>
<li>
<?php echo $page->getLabel() ?>
<span class="divider">/</span>
</li>
<?php else: ?>
<li class="active">
<?php if($container->getLinkLast()): ?><a href="<?php echo $page->getHref() ?>"><?php endif ?>
<?php echo $page->getLabel() ?>
<?php if($container->getLinkLast()): ?></a><?php endif ?>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
I'm not sure if this will help you but the progress should be similar like: Here
More info: zf2 documentation Rendering breadcrumbs using a partial view script
I just create a Twitter Bootstrap breadcrumb on my ZF2
breadcrumb.phtml
<?php
if (count($this->pages) > 0) :
?>
<ol class="breadcrumb">
<?php
for ($i = 0, $iend = count($this->pages); $i < $iend; $i++) :
$a=$this->pages[$i];
$label = $this->translate($a->getLabel());
?>
<li <?php if ($i + 1 == $iend) : ?> class="active" <?php endif ?> >
<?php if ($i + 1 != $iend) : ?>
<?php echo $label ?>
<?php else : ?>
<?php echo $label ?>
<?php endif ?>
</li>
<?php endfor ?>
</ol>
<?php endif ?>
And print on my template
<?php echo $this->navigation()->breadcrumbs('Navigation')->setPartial('partial/breadcrumb.phtml'); ?>
I know this is an old question, but if someone wants a simple solution for render ul-li menu, you can use this code in your breadcrumbs.phtml:
<?php $breadcrumbs = $this->navigation()->breadcrumbs()->setMinDepth(0); ?>
<?php $items = array_filter(explode($breadcrumbs->getSeparator(), $breadcrumbs->render())); ?>
<?php if ($items) : ?>
<ul class="breadcrumbs">
<?php foreach ($items as $item) : ?>
<li><?= $item ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
For ZF3
file: partial/breadcrumbs.phtml
<?php
/**
* Zend breadcrumb partial
*
* #date 12.09.2018 16:47
* #var $this \Zend\View\Renderer\PhpRenderer
* #var $page \Zend\Navigation\Page\AbstractPage
*/
if (0 == count($this->pages)) {
return;
}
?>
<ul class="breadcrumb">
<?php foreach ($this->pages as $page) :?>
<?php if ($page->isActive()): ?>
<li class="active"><?= $page->getLabel() ?></li>
<?php else : ?>
<li><?= $page->getLabel() ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
file: ./config/autoload/navigation.global.php
<?php
return [
'navigation' => [
'breadcrumb' => [
[
'label' => 'Home',
'route' => 'home',
'pages' => [
[
'label' => 'Page - 1',
'route' => 'doc',
'pages' => [
[
'label' => 'Sub page 2',
'route' => 'doc/page',
'params' => ['page' => 'control-panel'],
],
]
],
]
],
],
],
];
On the view
<?= $this->navigation('Zend\Navigation\Breadcrumb')
->breadcrumbs()
->setPartial('partial/breadcrumbs.phtml')
?>

Categories