I have almost everything working but I am stuck on the pagination. I had everything working before I ported to the MVC model, I am getting lost in the function and arrays. I am practicing with a simple concept of pulling memes from a database and displaying on index page.
I am getting unknown variable warnings. I've tried returning the variables, using echo and vardump. I think it is the way I am calling or passing $pages and $page through.
Index View:
<?php foreach($this->memes as $meme): ?>
<div class="memes">
<div class="panel">
<div class="panel-heading">
<h3 class="panel-title pull-left">
<?php echo date('F d, Y',strtotime($meme['dt'])); ?>
</h3>
<button type="submit" name="rate" class="pull-right" value="1"><span class="glyphicon glyphicon-thumbs-up"></span> </button>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<a href="meme.php?id=<?php echo $meme['id']; ?>">
<img src="<?php echo $meme['img']; ?>" class="img-responsive">
</a>
</div>
</div>
</div>
<?php endforeach; ?>
<ul class="pagination">
<li class="page-item <?php if(
$page <=1) { echo "disabled"; } ?>"><a class="page-link" <?php if($page > 1) { echo "href='?page=".($page-1)."'"; } ?>>Previous</a></li>
<?php for($x = 1; $x <= $pages; $x++): ?>
<li <?php if($page === $x) { echo 'class="active"';} ?>><a href="?page=<?php echo $x; ?>&per-page=<?php echo $perPage ?>"><?php echo $x; ?></a></li>
<?php endfor; ?>
<li class="page-item <?php if($page >= $pages) { echo "disabled"; } ?>"><a class="page-link" <?php if($page < $pages) { echo "href='?page=".($page+1)."'"; } ?>>Next</a></li>
</ul>
Controller:
public function index()
{
$this->View->render('index/index', array(
'memes' => MemeModel::getMemes())
);
}
Model:
public static function getMemes()
{
$database = DatabaseFactory::getFactory()->getConnection();
// User Input
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 20 ? (int)$_GET['per-page'] : 5;
// Positioning
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
// Query Memes
$sql = "SELECT SQL_CALC_FOUND_ROWS id, img, dt FROM memes ORDER BY id DESC LIMIT {$start}, {$perPage}";
$query = $database->prepare($sql);
$query->execute();
$query = $query->fetchAll(PDO::FETCH_ASSOC);
// Pages
$total = count($query);
$pages = ceil($total / $perPage);
// Query Ads
$ads = $database->prepare("SELECT id, img, link FROM ads");
$ads->execute();
$ads = $ads->fetchAll(PDO::FETCH_ASSOC);
return $query;
return $ads;
return $pages
return $page;
}
return $query; and return $ads; are working fine.
It must have to do with the way I am accessing the variables, I am not using the $this->XXX - how can I pass the variable through to the controller to call the var without $this->memes (get get var) or something of this nature?
via Evan