Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用细枝分页_Php_Pagination_Templating_Twig - Fatal编程技术网

Php 用细枝分页

Php 用细枝分页,php,pagination,templating,twig,Php,Pagination,Templating,Twig,我一直在尝试Twig,它在我的小网站上运行得很好 这是使用的教程: 然而,我在网上查了一下,找不到任何可以分页的东西 这是我的代码: <html> <head> <style type="text/css"> table { border-collapse: collapse; } tr.heading { font-weight: bolde

我一直在尝试Twig,它在我的小网站上运行得很好

这是使用的教程:

然而,我在网上查了一下,找不到任何可以分页的东西

这是我的代码:

    <html>
  <head>
    <style type="text/css">
      table {
        border-collapse: collapse;
      }        
      tr.heading {      
        font-weight: bolder;
      }        
      td {
        border: 0.5px solid black;
        padding: 0 0.5em;
      }    
    </style>  
  </head>
  <body>
    <h2>Automobiles</h2>
    <table>
      <tr class="heading">
        <td>Vehicle</td>
        <td>Model</td>
        <td>Price</td>
      </tr> 
      {% for d in data %}
      <tr>
        <td>{{ d.manufacturer|escape }}</td>
        <td>{{ d.model|escape }}</td>
        <td>{{ d.price|raw }}</td>
      </tr> 
      {% endfor %}
    </table>
  </body>
</html>

桌子{
边界塌陷:塌陷;
}        
标题{
字体大小:粗体;
}        
运输署{
边框:0.5px纯黑;
填充:0.5em;
}    
汽车
车辆
模型
价格
{数据%中d的%s}
{{d.manufacturer | escape}}
{d.model | escape}
{{d.price | raw}}
{%endfor%}
这是它的PHP代码:

<?php
// include and register Twig auto-loader
include 'Twig/Autoloader.php';
Twig_Autoloader::register();

// attempt a connection
try {
  $dbh = new PDO('mysql:dbname=world;host=localhost', 'root', 'mypass');
} catch (PDOException $e) {
  echo "Error: Could not connect. " . $e->getMessage();
}

// set error mode
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// attempt some queries
try {
  // execute SELECT query
  // store each row as an object
  $sql = "SELECT manufacturer, model, price FROM automobiles";
  $sth = $dbh->query($sql);
  while ($row = $sth->fetchObject()) {
    $data[] = $row;
  }

  // close connection, clean up
  unset($dbh); 

  // define template directory location
  $loader = new Twig_Loader_Filesystem('templates');

  // initialize Twig environment
  $twig = new Twig_Environment($loader);

  // load template
  $template = $twig->loadTemplate('automobiles.tpl');

  // set template variables
  // render template
  echo $template->render(array (
    'data' => $data
  ));

} catch (Exception $e) {
  die ('ERROR: ' . $e->getMessage());
}
?>

我需要做什么才能在Twig中对结果进行分页? 否则我的网站工作得很好


谢谢,JC

因为Twig只是一个模板引擎,所以没有任何东西(至少在核心中)可以添加分页。您必须自己分割内容并对其分页(例如使用JavaScript)。请记住,在当前的实现中,完整的内容插入到模板中,您只会隐藏/显示其中的某些部分


然而,首选的方法是在模型中也包括分页(您执行查询的部分),以便仅加载这些记录,这些记录当前显示给用户。这显然超出了模板引擎的范围。

互联网上已经有了一些例子。你可以参考


@XFS:请记住,这些只是显示页面浏览器的示例。页面限制和偏移量的逻辑仍然需要注意。在apfelbox编写时,您仍然需要调整模型、控制器等。