English 中文(简体)
CakePHPP: 家庭元素中的边緣。 ctp
原标题:CakePHP: Pagination in an Element on home.ctp

I m 使用 CakePHPP 为 PowerDNS 创建前端界面界面, 使用 MySQL 后端。 在应用程序的首页, 我想要有几块部件( 快速添加记录, 快速添加域名等) 。 我想要的部件之一是现有域名的识别列表 。

域控制器.php 中的索引函数看起来是这样 :

public $paginate = array(
   fields      => array( id ,  name ),
   limit       => 25,
   order       => array(  name  =>  asc  ),
   conditions  => array( "NOT" => array( "name LIKE" => "%.arpa" ) )
);

public function index() {
  $domains = $this->paginate();
  if ( $this->request->is( requested )) {
    return $domains;
  } else {
    $this->set( domains , $domains);
  }
}

我创造了一个像这样的元素:

<?php $domains = $this->requestAction( Domains/index ); ?>

<ol>
  <?php foreach( $domains as $domain) :?>
    <li>echo $domains[ domain ][ name ]</li>
  <?php endforeach; ?>
</ol>

<?php echo $paginator->numbers(); ?>

当我访问首页时, 我得到一个未定义的变量: paginator 错误。 我尝试过使用 $this- gt; Paginator- gt; numbers (), 但却给了我未定义的属性 : 视图 : $Paginator 。 将 Paginator 助手添加到 Pages currentral. php 也帮不上忙 - $this- gt; paginator 已经可用, 但我得到了未定义的索引: pageCount 。

是否可以从家庭.ctp上的一个元素上做这种剖析, 或者我必须做一些定制的 JavaScript 的东西?

<强 > EDIT

现在我要说一点:我把我的“域控制器”指数函数换成了这个功能:

public function index() {
 $domains = $this->paginate();
 $paginator = $this->params;
 if ( $this->request->is( requested )) {
  return compact(  domains ,  paginator  );
 } else {
  $this->set( domains , $domains);
 }
}

在域名 List.ctp 元素中添加了以下内容:

<?php 
$result = $this->requestAction( Domains/index );
$domains = $result[ domains ];
$this->Paginator->request = $result[ paginator ];
?>

<ol>
  <?php foreach( $domains as $domain) :?>
    <li>echo $domains[ domain ][ name ]</li>
  <?php endforeach; ?>
</ol>

<?php echo $paginator->numbers(); ?>

$This- gt; 显示器正在正常运行, 我可以访问它的所有方法和属性, 并按常态进行。 我现在的问题是, 如果我点击, 比如 2, 浏览器浏览到 / page/ home/ page: 2, 但域列表仍然显示第 1 页 。 只要想清楚如何通过 page: 2 到 元素 。 AJAX 将整件事拼起来, 这样我不需要刷新整页 。

最佳回答

OK,我解决了这个问题, 尽管我的解决方案可能不是非常优雅。

域控制器.php 具有列表Domains () 函数, 看起来像 :

public function listDomains() {
 $domains = $this->paginate(); 
 $paginator = $this->params;
 if ( $this->request->is( ajax ) ) {
  $this->set(  domains , $domains );
 }
 if ( $this->request->is( requested )) {
  return array(  domains  => $domains,  paginator  => $paginator,  paging  => $this->params[ paging ] );
 } else {
  $this->set(  domains , $domains );
 }
}

homectp 引用下面称为域名List.ctp. 域名List.ctp 的元素, 转而使用请求 Action () - 我知道, 我知道 - 调用上面的域名 List () 函数。 要求保留请求, 将包含 $domains 和 $paginator 等值的数组发回元素 。

域列表. ctp 包含此代码 :

<?php
$result = $this->requestAction( Domains/listDomains , array( updateId  =>  domainList ) );
$domains = $result[ domains ];
$paginator = $result[ paginator ];
$this->Paginator->request = $paginator;

$this->Paginator->options(array(
  update  =>  #domainList ,
  evalScripts  => true,
  url  => array( controller  =>  Domains ,  action  =>  listDomains ,  updateId  =>  domainList  ),
));

?>

我在这里做的基本上是手动重新配置 $This- gt; Paginator- gt; 请求, 并加上参数, 这些参数最初被发送到 Domain 控制器的域 List () 函数。 这样可以让我正确访问各种 pagnator 函数, 如数字 () 、 prev () 和下一个 () 。 它有点乱, 但猜怎么着呢? 它会变得有点乱 。

当您单击由这些 pagner 函数创建的链接时, 是否执行( $this- gt; request- gt; is( ajax) ) 部分, 而页面上的 div 对象将更新为 View/ Domains/ domainList. ctp 的内容, 而不是 View/ Emincles/ domainList. ctp 。 查看/ Domains/ domainList. ctp 的内容与相应元素大致相同, 两者必须多或少同步化 kep 。 区别在于我们不需要手动打印 $this- gt; Paginator :

<?php
$this->Paginator->options(array(
  update  =>  #domainList ,
  evalScripts  => true,
  url  => array( controller  =>  Domains ,  action  =>  listDomains ,  updateId  =>  domainList  ),
));

?>

就像我说的,它乱七八糟,无缘无故,但对我有用。 如果有人有这么少的花样,我会很高兴的。

问题回答

首先,不要这样做:

$domains = $this->requestAction( Domains/index ); It s expensive and not good practice and I m not sure why you need to be doing it from your example.

第二,这样称呼你的长相:

$domain = $this- & gt; Paginate (域) = $this- & gt; Paginate (域);





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签