Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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_Loops_Pagination_Twig - Fatal编程技术网

Php 用细枝简单分页

Php 用细枝简单分页,php,loops,pagination,twig,Php,Loops,Pagination,Twig,我正在尝试创建一个带有细枝视图的简单分页。 我没有用Symfony 以下是我的经理给我的方法: public function getAllPosts() { if(isset($_GET['p']) && (!isset($_GET['page']))){ $currentPage = 1; } else { $currentPage = $_GET['page']; }

我正在尝试创建一个带有细枝视图的简单分页。 我没有用Symfony

以下是我的经理给我的方法:

  public function getAllPosts()
  {
      if(isset($_GET['p']) && (!isset($_GET['page']))){

          $currentPage =  1;
      }
      else {
          $currentPage = $_GET['page'];
      }
    $q= $this->_db->query('SELECT COUNT(id) AS numberposts FROM posts');
    $data = $q->fetch(PDO::FETCH_ASSOC);

    $number_posts= $data['numberposts'];
    $perPage = 1;
    $numberPages = ceil($number_posts/$perPage);

    $q = $this->_db->query("SELECT * FROM posts ORDER BY date DESC LIMIT ".(($currentPage-1)*$perPage).",$perPage");

    while($data = $q->fetch(PDO::FETCH_ASSOC))
    {
      $datas[] = new Post($data);
    }


    return $datas;

  }
我想在我的视图中创建一个循环,这就是我正在做的

            {% for posts in allPosts %}
               {% for i in 1..numberPages %}
                    <a href="index.php?p=blog&page={{ i }}">{{ i }}</a>
                 {% endfor %}
            {% endfor %}
在我看来:

{% for i in 1.. allPosts.numberPages %}
                    <li><a href="index.php?p=blog&page{{ loop.index }}">{{ loop.index}}</a></li>
                {% endfor %}
{%for i in 1..allPosts.numberpage%}
  • {%endfor%}
    但现在我有另一个问题。我只在所有的页面上得到相同的帖子

    编辑

    我忘记了页面=在我的页面链接上

     <li><a href="index.php?p=blog&page={{ loop.index }}">{{ loop.index}}</a></li>
    
  • 现在开始工作了


    谢谢

    您还需要返回页数。 可能是这样的

    public function getAllPosts()  {
        /** ... code .. **/
        return array(
            'records'     => $data,
            'numberPages' => $numberPages,
        );
    }
    
    
    
    {% for posts in allPosts.records %}
        {% for i in 1.. allPosts.numberPages %}
             <a href="index.php?p=blog&page={{ i }}">{{ i }}</a>
        {% endfor %}
    {% endfor %}
    
    公共函数getAllPosts(){ /**…代码**/ 返回数组( “记录”=>$data, “numberPages”=>$numberPages, ); } {allPosts.records%} {1..allPosts.numberpage%中的i的% {%endfor%} {%endfor%}
    您还需要返回页数。 可能是这样的

    public function getAllPosts()  {
        /** ... code .. **/
        return array(
            'records'     => $data,
            'numberPages' => $numberPages,
        );
    }
    
    
    
    {% for posts in allPosts.records %}
        {% for i in 1.. allPosts.numberPages %}
             <a href="index.php?p=blog&page={{ i }}">{{ i }}</a>
        {% endfor %}
    {% endfor %}
    
    公共函数getAllPosts(){ /**…代码**/ 返回数组( “记录”=>$data, “numberPages”=>$numberPages, ); } {allPosts.records%} {1..allPosts.numberpage%中的i的% {%endfor%} {%endfor%}
    无处返回
    numberPages
    Ok,所以我不知道如何使用Twig返回变量。。。我认为当你用一个方法渲染一个视图时,你可以访问该方法内部的变量。在任何地方你都不能返回
    numberPages
    Ok,所以我不知道如何用细枝返回变量。。。我认为当你用一个方法渲染一个视图时,你可以访问这个方法内部的变量。好的,谢谢,我现在了解了它的工作原理。现在没有显示任何内容,但我正在查找1的原因。。在1。。allPosts.numberpage。我不懂这个密码。你能解释一下吗???@Zakirhussain这是
    的替代品好的,谢谢,我现在明白它的工作原理了。现在没有显示任何内容,但我正在查找1的原因。。在1。。allPosts.numberpage。我不懂这个密码。你能解释一下吗???@Zakirhussain这是