Bonjour,
j'aurais besoin d'un petit coup de main, svp
j'ai rajouter un système de commentaire au mvc(a à z) avec le nombre de commentaire par post dans la fonction view
et j'ai crée une vars 'totalcomments' dans ma fonction view qui me donne le nombre de commentaire par post.
jusque la tous fonctionne
PostController.php
function index(){
$perPage = 3;
$this->loadModel('Post');
$condition = array('online' => 1,'type'=>'post');
$d'posts'] = $this->Post->find(array(
'conditions' => $condition,
'fields' => 'Post.id,Post.name,Post.slug,Post.created,Category.name as catname,Post.content,Category.slug as catslug',
'order' => 'created DESC',
'limit' => ($perPage*($this->request->page-1)).','.$perPage,
'join' => array('categories as Category'=>'Category.id=Post.category_id')
));
$d'total'] = $this->Post->findCount($condition);
$d'page'] = ceil($d'total'] / $perPage);
$this->set($d);
}
function view($id, $slug) {
// Pour l'affichage du post voulu
$this->loadModel('Post');
$d'post'] = $this->Post->findFirst(array(
'fields' => 'Post.id,Post.content,Post.name,Post.slug,Post.created,Category.name as catname,Category.slug as catslug',
'conditions' => array('Post.online' => 1,'Post.id'=>$id,'Post.type'=>'post'),
'join' => array('categories as Category'=>'Category.id=Post.category_id')
));
// Pour l'affichage des commentaires
$this->loadModel('Comment');
$d'comments'] = $this->Comment->find(array(
'conditions' => array('posts_id' => $id),
'fields' => 'Comment.id,Comment.pseudo,Comment.mail,Comment.content,Comment.created',
'order' => 'Comment.created DESC',
'join' => array('posts as Post' => 'Post.id=Comment.posts_id')
));
$condition = array('posts_id' => $id);
$d'totalcomments'] = $this->Comment->findCount($condition);
if(empty($d'post'])){
$this->e404('Page introuvable');
}
if($slug != $d'post']->slug){
$this->redirect("posts/view/id:$id/slug:".$d'post']->slug,301);
}
if($this->request->data){
if($this->Comment->validates($this->request->data)){
$this->request->data->posts_id = $id;
$this->request->data->created = date('Y-m-d H:i:s');
$this->Comment->save($this->request->data);
$this->Session->setFlash('Le commentaire a bien été créé');
$this->redirect("posts/view/id:$id/slug:".$slug);
}else{
$this->Session->setFlash('Merci de corriger vos informations','error');
}
}
$this->set($d);
}
pour le recupere dans ma view.php dans view/posts/ j'ai juste a ajouter la vars 'totalcomments'
et j'obtien le nombre de commentaire.
<?php echo $totalcomments; ?>
Mais le truc est que je voudrai aussi pouvoir utiliser cette vars 'totalcomments' dans ma view/index.php
mais ici je coince....
comment faire pour pouvoir utiliser la vars 'totalcomments'?
j'ai essayer quelque truc du genre mais sans succes :(
<?php echo $request->Comments->totalcomments; ?>
ou
<?php echo $comments->totalcomments; ?>
ou
<?php echo $this->request->totalcomments; ?>
est ce que quelqu'un aurais une idée svp ?