zend paginator 创建其他链接并调用与 URL 连接的操作

发布于 2024-12-26 17:56:55 字数 2780 浏览 2 评论 0原文

我是 ZF 新手。我在我的第一个项目中使用了 zend paginator。它工作正常,可以以正确的结果切换黑白页面,但问题是我在该视图中也有其他链接,请查看我的视图,

<?php include "header.phtml"; ?>
<h1><?php echo  $this->escape($this->title);?></h1>
<h2><?php echo $this->escape($this->description);?></h2>
<a href="register">Register</a>
<table border="1" align="center">
<tr>
  <th>User Name</th>      
  <th>First Name</th>     
  <th>Last Name</th>      
  <th>Action</th>     
</tr>
 <?php 
 foreach($this->paginator as $record){?>  
<tr>
  <td><?php echo $record->user_name;?></td>
  <td><?php echo $record->first_name;?></td>      
  <td><?php echo $record->last_name;?></td>   
  <td>
    <a  href="edit/id/<?php echo $record->id;?>">Edit</a>
    |
    <a  href="del/id/<?php echo $record->id;?>">Delete</a>      
  </td>   
</tr> 
  <?php } ?>
 </table>

<?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?>

 <?php include "footer.phtml"; ?>

正如我所说分页呈现并工作正常,但是当我单击这些链接时,

 <a  id="edit_link" href="edit/id/<?php echo $record->id;?>">Edit</a>
                      or
 <a id="delete_link" href="del/id/<?php echo $record->id;?>">Delete</a>
                      or
 <a href="register">Register</a>

使我的网址像

(initial link) http://localhost/zend_login/web_root/index.php/task/list

它不会调用所需的操作,而是在单击上述任何链接后

 http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/8
 http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/23
 http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/register        http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/del/id/12

这样注意,当页面第一次呈现,但是当我单击任何分页链接,这样做最初会转到所需的操作并显示视图...任何帮助,这里是操作

 public function listAction(){

      $registry = Zend_Registry::getInstance();  
      $DB = $registry['DB'];
      $sql = "SELECT * FROM task ORDER BY task_name ASC";
      $result = $DB->fetchAll($sql);
      $page=$this->_getParam('page',1);
      $paginator = Zend_Paginator::factory($result);
      $paginator->setItemCountPerPage(3);
      $paginator->setCurrentPageNumber($page);
      $this->view->assign('title','Task List');
      $this->view->assign('description','Below, are the  Task:');
      $this->view->paginator=$paginator;
     }

I am NEW to ZF .i used zend paginator in my first project .its working fine that is switching b/w pages with right result but the problem is that i have other links too in that view have a look to my view

<?php include "header.phtml"; ?>
<h1><?php echo  $this->escape($this->title);?></h1>
<h2><?php echo $this->escape($this->description);?></h2>
<a href="register">Register</a>
<table border="1" align="center">
<tr>
  <th>User Name</th>      
  <th>First Name</th>     
  <th>Last Name</th>      
  <th>Action</th>     
</tr>
 <?php 
 foreach($this->paginator as $record){?>  
<tr>
  <td><?php echo $record->user_name;?></td>
  <td><?php echo $record->first_name;?></td>      
  <td><?php echo $record->last_name;?></td>   
  <td>
    <a  href="edit/id/<?php echo $record->id;?>">Edit</a>
    |
    <a  href="del/id/<?php echo $record->id;?>">Delete</a>      
  </td>   
</tr> 
  <?php } ?>
 </table>

<?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?>

 <?php include "footer.phtml"; ?>

as i said the pagination renders and working fine but when i click on these links

 <a  id="edit_link" href="edit/id/<?php echo $record->id;?>">Edit</a>
                      or
 <a id="delete_link" href="del/id/<?php echo $record->id;?>">Delete</a>
                      or
 <a href="register">Register</a>

it is not calling the required action instead it make my url like this

(initial link) http://localhost/zend_login/web_root/index.php/task/list

after clicking any of the above link its like this

 http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/8
 http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/23
 http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/register        http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/del/id/12

note its not happening when the page renders first time but when i click on any pagination link its doing so initialy its going to the reguired action and displaying a view...any help HERE IS THE ACTION

 public function listAction(){

      $registry = Zend_Registry::getInstance();  
      $DB = $registry['DB'];
      $sql = "SELECT * FROM task ORDER BY task_name ASC";
      $result = $DB->fetchAll($sql);
      $page=$this->_getParam('page',1);
      $paginator = Zend_Paginator::factory($result);
      $paginator->setItemCountPerPage(3);
      $paginator->setCurrentPageNumber($page);
      $this->view->assign('title','Task List');
      $this->view->assign('description','Below, are the  Task:');
      $this->view->paginator=$paginator;
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如歌彻婉言 2025-01-02 17:56:55

尝试:

// controller
$this->view->controllerName = $this->getRequest()->getControllerName();

// view script
<a href="<?php echo $this->controllerName . '/edit/id/' . $record->id);?>">Edit</a> 
| 
<a href="<?php echo $this->controllerName . '/del/id/' . $record->id);?>">Delete</a> 

<a href="<?php echo $this->baseUrl($controllerName . '/edit/id/' . $record->id);?>">Edit</a> 
| 
<a href="<?php echo $this->baseUrl($controllerName . '/del/id/' . $record->id);?>">Delete</a> 

第二个示例使用使用前端控制器的 baseUrl 设置的 baseUrl() 视图助手。如果你没有在 frontController 中设置 baseUrl 它会尝试猜测。由于您没有使用引导程序功能来设置 baseUrl,因此您可以在 index.php 中执行以下操作(不是必需的):

$frontController = Zend_Controller_Front::getInstance(); 
$frontController->setBaseUrl('/');

使用 url() 视图助手的第三种可能性:

<a href="<?php echo $this->url(array(
    'controller' => $controllerName,
    'action'     => 'edit',
    'id'         => $record_->id
)); ?>">Edit</a> 
| 
<a href="<?php echo $this->url(array(
    'controller' => $controllerName,
    'action'     => 'del',
    'id'         => $record_->id
));?>">Delete</a> 

Try:

// controller
$this->view->controllerName = $this->getRequest()->getControllerName();

// view script
<a href="<?php echo $this->controllerName . '/edit/id/' . $record->id);?>">Edit</a> 
| 
<a href="<?php echo $this->controllerName . '/del/id/' . $record->id);?>">Delete</a> 

or

<a href="<?php echo $this->baseUrl($controllerName . '/edit/id/' . $record->id);?>">Edit</a> 
| 
<a href="<?php echo $this->baseUrl($controllerName . '/del/id/' . $record->id);?>">Delete</a> 

Second example uses baseUrl() view helper that's using front controller's baseUrl setting. If you don't set baseUrl in your frontController it's trying to guess. As you're not using bootstrap functionality to set baseUrl you may do the following in index.php (not required):

$frontController = Zend_Controller_Front::getInstance(); 
$frontController->setBaseUrl('/');

Third possibility using url() view helper:

<a href="<?php echo $this->url(array(
    'controller' => $controllerName,
    'action'     => 'edit',
    'id'         => $record_->id
)); ?>">Edit</a> 
| 
<a href="<?php echo $this->url(array(
    'controller' => $controllerName,
    'action'     => 'del',
    'id'         => $record_->id
));?>">Delete</a> 
可爱暴击 2025-01-02 17:56:55

将其添加到您的操作中

$request = $this->getRequest();
$this->view->assign('url', $request->getBaseURL());

,并将视图中的链接替换为此

<a href="<?php echo $this->url."/task/register"?>">Add a Task</a>
<a href="<?php echo $this->url.'/task/edit/id/'.$record->id;?>">Edit</a>
<a href="<?php echo $this->url.'/task/del/id/'.$record->id;?>">Delete</a>

add this in your action

$request = $this->getRequest();
$this->view->assign('url', $request->getBaseURL());

and replace your links in view with this

<a href="<?php echo $this->url."/task/register"?>">Add a Task</a>
<a href="<?php echo $this->url.'/task/edit/id/'.$record->id;?>">Edit</a>
<a href="<?php echo $this->url.'/task/del/id/'.$record->id;?>">Delete</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文