PHP pagination set pages on view custom PHP MVC

[亡魂溺海] 提交于 2021-02-04 16:46:57

问题


Here is my method im getting all posts correctly and i can switch over them from url exemple simply posts/1 or posts/2 etc.. How can i set set a paginator on my view in this case ?

Here is the controller :

namespace fordev\app\controllers;

use fordev\app\core\Controller;

class Home extends Controller
{
    public function manage($page = NULL)
    {
        \Session::checkNotIsset('logged','/pfe/login');
        $model = $this->model('home')->getAllPosts($page);
        $this->view()->renderWithAdditionalHeaderAndFooter('home',['Manage'], 'Home', [$model]);
    }
}

Here is the Model:

 namespace fordev\app\models;
 use fordev\app\core\Model;


class Home extends Model 
{
    /**
     * Get All posts method
     * @param $page = current page
     */
    public function getAllPosts($page)
    {
        $con  = $this->getInstance();

        $page  = (isset($page) && $page < 100000)? (int) $page : 1;
        $perPage = 5;
        $start = $perPage * ($page - 1);
        $total = \Database::countRows($con, '*','annonce');
        $totalPages = ceil($total / $perPage);

        $next = $page+1;
        $prev = $page-1;

        $sql  = "SELECT * FROM annonce INNER JOIN users ON users.ID =  annonce.a_publisher ORDER BY a_id DESC LIMIT $start, $perPage";
        $stmt = $con->prepare($sql);
        $stmt->execute();

        return ($stmt->rowCount() > 0) ? $stmt->fetchAll() : false;
    }


}

And here is the view :

<div class="container">
    <div class="col-md-12">
        <div class="blog-header">
            <h1 class="blog-title">Les Annonce De Kisoun</h1>
            <p class="lead blog-description">Tous les annonces de village Kissoun se trouve ICI !.</p>
        </div>
    </div>

    <div class="col-md-8">
        <?php if(is_array($modelData[0])): // if there is data in the database ?>
    <?php foreach ($modelData[0] as $value) {  // foreach loop ?>

        <div class="panel panel-default">
      <div class="panel-heading">
        <h3><?=$value->a_title ?></h3>
      </div>

            <div class="panel-body annonce">

                <div class="annonce-image pull-left">
                    <img src="<?=Rooter::get('UP_IMG').$value->a_image ?>" alt="">
                </div>

        <p class="pull-right"><?=$value->a_content ?></p>
        <div class="clear"></div>
        <hr>
          <a class="btn btn-danger pull-right">Details</a>
          <span class="label pull-left"><?= \Helper::elapsedTime($value->a_time) ?> by <?=$value->name ?></span>

            </div>
        </div>

   <?php   } // end of foreach loop ?>

  <?php endif; ?>
  <?php if($modelData[0] === false): ?>
      <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Error</strong> Pas D'annonce encore ...
      </div>
  <?php endif; ?>
    </div>


</div>

回答1:


solved !!

just return pagination info as an array alongside with posts array and use these info on the view like so

class Home extends Model 
{
    /**
     * Get All posts method
     * @param $page = current page
     */
    public function getAllPosts($page)
    {
        $con  = $this->getInstance();

        $page  = (isset($page) && $page < 100000)? (int) $page : 1;
        $perPage = 5;
        $start = $perPage * ($page - 1);
        $total = \Database::countRows($con, '*','annonce');
        $totalPages = ceil($total / $perPage);

        $next = $page+1;
        $prev = $page-1;

        $sql  = "SELECT * FROM annonce INNER JOIN users ON users.ID =  annonce.a_publisher ORDER BY a_id DESC LIMIT $start, $perPage";
        $stmt = $con->prepare($sql);
        $stmt->execute();

        $paginatoinInfo = [
            "page"          => $page,
            "start"         => $start,
            "totalPages"    => $totalPages,
            "next"          => $next,
            "prev"          => $prev
        ];

        $res = [];
        $res['posts'] = ($stmt->rowCount() > 0) ? $stmt->fetchAll() : false;
        $res['paginator'] = $paginatoinInfo;

        return $res;
    }


}

on the view I was able to use this array like so

<?php
$paginator  = $modelData[0]['paginator'];
$page       = $paginator['page'];
$start      = $paginator['start'];
$totalPages = $paginator['totalPages'];
$next       = $paginator['next'];
$prev       = $paginator['prev'];
?>


来源:https://stackoverflow.com/questions/43778746/php-pagination-set-pages-on-view-custom-php-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!