PB d'affichage MVC

Par nicolastaf, il y a 8 ans


Bonjour a tous,

Je suis sur un projet creer un blog en php procedural vers POO en MVC...

Mon niveau; debutant...

J'ai reussi pour le front mais pour la partie admin je suis bloque depuis ce matin. J'ai donc ma vue mon modele et mon controleur:

mon model :

<?php /** * Created by PhpStorm. * User: mac * Date: 15/08/2017 * Time: 16:27 */ class Dashboard { private $_id; private $_name; private $_email; private $_comment; private $_post_id; private $_date_comment; private $_seen; /** * @return mixed */ public function getName() { return $this->_name; } /** * @param mixed $name */ public function setName($name) { $this->_name = $name; } /** * @return mixed */ public function getEmail() { return $this->_email; } /** * @param mixed $email */ public function setEmail($email) { $this->_email = $email; } /** * @return mixed */ public function getComment() { return $this->_comment; } /** * @param mixed $comment */ public function setComment($comment) { $this->_comment = $comment; } /** * @return mixed */ public function getPostId() { return $this->_post_id; } /** * @param mixed $post_id */ public function setPostId($post_id) { $this->_post_id = $post_id; } /** * @return mixed */ public function getDateComment() { return $this->_date_comment; } /** * @param mixed $date_comment */ public function setDateComment($date_comment) { $this->_date_comment = $date_comment; } /** * @return mixed */ public function getSeen() { return $this->_seen; } /** * @param mixed $seen */ public function setSeen($seen) { $this->_seen = $seen; } public static function get_comments(){ $db = getBdd(); $req = $db->prepare(" SELECT comments.id, comments.name, comments.email, comments.comment, comments.post_id, comments.date_comment, posts.title FROM comments JOIN posts ON comments.post_id = posts.id WHERE comments.seen = '0' ORDER BY comments.date_comment ASC "); $results = []; $req->execute(array()); while($rows = $req->fetchObject()){ $results[] = $rows; } return $results; } public static function inTable($table){ $db = getBdd(); $query = $db->query("SELECT COUNT(id) FROM $table"); return $nombre = $query->fetch(); } }

Mon controleur :

<?php /** * Created by PhpStorm. * User: mac * Date: 15/08/2017 * Time: 16:31 */ require_once '../modele/model.php'; // pour le moment connexion a la bdd require_once 'modele/dashboard.php'; function admin_index(){ //$comments = Dashboard::get_comments(); $comments = new Dashboard(); $comments->get_comments(); require './pages/dashboard.php'; }

mon index.php :

<?php require_once 'controller/controller.php'; ob_start(); if(isset($_GET['page'])){ $page = $_GET['page']; }else{ $page = 'dashboard'; } if($page === 'dashboard'){ admin_index(); } $content = ob_get_clean(); require 'pages/view.php';

resultat une page blanche sans message d'erreur...

Si vou spouviez m'aider ce serait cool Merci d'avance

7 réponses

Lartak, il y a 8 ans

Bonsoir.
Ce qui pourrait nous être utile, c'est la partie qui est censée afficher les données, car dans les trois parties de code qui correspondent à trois fichiers différents, il n'y a rien qui permette d'afficher la moindre donnée, il serait donc normal que tu n'aies aucun affichage et encore moins la moindre erreur.

nicolastaf, il y a 8 ans

Merci de ta reponse voici la partie vue :
Mon menu top s'affiche correctement, c'est la partie comment qui ne s'affiche pas...

<!DOCTYPE html> <html> <head> <!--Import Google Icon Font--> <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <!--Import materialize.css--> <link type="text/css" rel="stylesheet" href="css/style.css" media="screen,projection"/> <title>Administration</title> <!--Let browser know website is optimized for mobile--> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <!-- WYSIWYG --> <script src='./js/tinymce/tinymce.min.js'></script> <script src="./js/tinymce.js"></script> </head> <body> <?php include 'body/topbar.php'; ?> <div class="container"> <?php $content; ?> </div> <!--Import jQuery before materialize.js--> <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script type="text/javascript" src="../content/js/materialize.js"></script> <script type="text/javascript" src="../content/js/script.js"></script> <?php $pages_js = scandir('js/'); if(in_array($page.'_function.js',$pages_js)){ ?> <script type="text/javascript" src="js/<?= $page ?>_function.js"></script> <?php } ?> </body> </html>
Lartak, il y a 8 ans

L'erreur est plutôt simple à voir, tu devrais remplacer :

<div class="container"> <?php $content; ?> </div>

Par :

<div class="container"> <?php echo $content; ?> </div> /* ou */ <div class="container"> <?= $content ?> </div>

Si tu ne fais pas un echo, tu n'auras pas d'affichage de ta variable $content.

nicolastaf, il y a 8 ans

Merci beaucoup... je pensais justement a prendre des vacances...

Jaurais une autre question si c'est possible concernant les appels des class avec parametres ?

ou je dois mettre ce sujet resolu et ouvrir un autre sujet ????

donc Maclass($email, $name, $post_id) comme exemple:
$comments = new Maclass($email, $name, $post_id); ???

Lartak, il y a 8 ans

Non, pour faire ceci, il te faut un constructeur, soit par exemple :

class Maclass { public function __construct($email, $name, $post_id) { /* ... */ } public function getComments() { /* qui retourne quelque chose */ return $variable; /* qui contient les données à récupérer */ } // etc ... }

Ensuite :

$init = new Maclass($email, $name, $post_id); $comments = $init->getComments();

Je te recommande fortement de suivre la formation sur la POO qui est sur le site : Formations PHP » La POO en PHP.

nicolastaf, il y a 8 ans

Merci de m'avoir dépanné...
Bone soirée

yanis-git, il y a 8 ans

Peux tu marquer le topic en résolu stp ?