Partage
  • Partager sur Facebook
  • Partager sur Twitter

Gestion de fichier php

comment passer de fichier en fichier PHP

    13 novembre 2019 à 15:34:06

    Bonjour J'ai compris que pour passer par exemple de l'index à par exemple un fichier frontend on fais comme ça : controller/frontend.php ou encore : view/frontend/postView.php . Mais comment faire pour passer de postView.php à index.php ?
    • Partager sur Facebook
    • Partager sur Twitter

    Vive le numérique

      14 novembre 2019 à 9:21:54

      Bonjour,

      La question à ce poser c'est quel est le but rechercher. Si j'ai bien compris tu voudrai partir de index.php pour aller vers la view puis revenir sur index.php. Et si c'est bien ça, c'est la que je comprends pas pourquoi :)

      Pourquoi ? car de base quand tu fais un require en réalité tu ne quitte pas vraiment le fichier qui require. Exemple :

      // Fichier index.php
      $a = 'test';
      echo $a . '<br>';
      
      require 'update.php';
      echo $a . '<br>';
      
      // Fichier update.php
      $a = 'salut';

      Si on exécute le fichier index.php on verra bien le message 'test' puis à la ligne le message 'salut'.

      • Partager sur Facebook
      • Partager sur Twitter
        14 novembre 2019 à 9:55:57

        Est-ce que tu as (bien) suivi le tuto et l'as-tu terminé ?
        • Partager sur Facebook
        • Partager sur Twitter
        le bienfait n'est jamais perdu
          15 novembre 2019 à 8:35:56

          Bonjour non je n'ai pas terminé le tutto. Donc si j'ai compris dans le cours on quitte jamais index.php c'est pourquoi on à pas besoin d'y revenir. Mon problème c'est qu'il y a des erreurs. Sous le titre voici l'erreur : 

          Notice: Undefined index: creation_data_fr in C:\xampp\htdocs\blog\project\view\frontend\listPostsView.php on line 15

          ensuite quand je clique sur commentaire j'ai plein de message d'erreur : 

          Notice: Undefined variable: post in C:\xampp\htdocs\blog\project\view\frontend\postView.php on line 15
          le
          Notice: Undefined variable: post in C:\xampp\htdocs\blog\project\view\frontend\postView.php on line 16


          Notice: Undefined variable: post in C:\xampp\htdocs\blog\project\view\frontend\postView.php on line 20

          et encore: 

          Notice: Undefined variable: comments in C:\xampp\htdocs\blog\project\view\frontend\postView.php on line 27

          Fatal error: Uncaught Error: Call to a member function fetch() on null in C:\xampp\htdocs\blog\project\view\frontend\postView.php:27 Stack trace: #0 {main} thrown in C:\xampp\htdocs\blog\project\view\frontend\postView.php on line 27


          Et voila mes code: 

          index.php

          <?php
          require('controller/frontend.php');
          
          try { // On essaie de faire des choses
          	if(isset($_GET['action'])) {
          		if ($_GET['action'] == 'listPosts') {
          			listPosts();
          		}
          		elseif ($_GET['action'] == 'post') {
          			if (isset($_GET['id']) && $_GET['id'] > 0) {
          				post();
          			}
          			else {
          				// Erreur ! On arrête tout, on envoie une exception, donc au saute directement au catch
          				throw new Exception('Aucun identifiant de billet envoyé');
          			}
          		}
          		elseif ($_GET['action'] == 'addComment') {
          			if (isset($_GET['id']) && $_GET['id'] > 0) {
          				if (!empty($_POST['author']) && !empty($_POST['comment'])) {
          					addComment($_GET['id'], $_POST['author'], $_POST['comment']);
          				}
          				else {
          					// Autre exception
          					throw new Exception('Tous les champs ne sont pas remplis !');
          				}
          			}
          			else {
          				// Autre exception
          				throw new Exception('Aucun identifiant de billet envoyé');
          			}
          		}
          	}
          	else {
          		listPosts();
          	}
          }
          catch(Exception $e) { // s'il y a eu une erreur, alors...
          	echo 'Erreur : ' .$e->getMessage();
          }

          frontend.php/controller

          <?php
          
          require('model/frontend.php');
          
          function listPosts()
          {
          	$posts = getPosts();
          
          	require('view/frontend/listPostsView.php');
          }
          
          function post()
          {
          	$post = getPost($_GET['id']);
          	$comments = getComments($_GET['id']);
          
          	require('view/frontend/postView.php');
          }
          function addComment($postId, $author, $comment)
          {
          	$affectedLines = postComment($postId, $author, $comment);
          
          	if($affectedLines === false) {
          		// Erreur gérée. Elle sera remontée jusqu'au bloc try du routeur !
          		throw new Exception('Impossible d\'ajouter le commentaire !');
          	}
          	else {
          		header('Location: index.php?action=post&id=' . $postId);
          	}
          }

          frontend/model.php

          <?php
          function getPosts()
          {
          	$db = dbConnect();
          	$req = $db->query('SELECT id, title, content, DATE_FORMAT(creation_date, \'%d/%m/%Y à %Hh%imin%ss\') AS creation_date_fr FROM posts ORDER BY creation_date DESC LIMIT 0, 5');
          
          	return $req;
          }
          
          function getPost($postId)
          {
          	$db = dbConnect();
          	$req = $db->prepare('SELECT id, title, content, DATE_FORMAT(creation_date, \'%d/%m/%Y à %Hh%imin%ss\') AS creation_date_fr FROM posts where id = ?');
          	$req->execute(array($postId));
          	$post = $req->fetch();
          
          	return $post;
          
          }
          function getComments($postId)
          {
          	$db = dbConnect();
          	$comments = $db->prepare('SELECT id, author, comment, DATE_FORMAT(comment_date, \'%d/%m/%Y à %Hh%imin%ss\') AS comment_date_fr FROM comments WHERE post_id = ? ORDER BY comment_date DESC');
          	$comments->execute(array($postId));
          
          	return $comments;
          }
          
          function dbConnect()
          {
          	$db = new PDO('mysql:host=localhost;dbname=blog;charset=utf8', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
          
          		return $db;
          }
          function postComment($postId, $author, $comment)
          {
          	$db = dbConnect();
          	$comments = $db->prepare('INSERT INTO comments(post_id, author, comment, comment_date) VALUES(?, ?, ?, NOW())');
          	$affectedlines = $comments->execute(array($postId, $author, $comment));
          
          	return $affectedLines;
          }

          listPostsView.php

          <?php $title = 'Mon blog'; ?>
          
          <?php ob_start(); ?>
          <h1>Mon super blog !</h1>
          <p>Derniers billets du blog :</p>
          
          
          <?php
          while ($data = $posts->fetch())
          {
          ?>
          	<div class="news">
          		<h3>
          			<?= htmlspecialchars($data['title']) ?>
          			<em>le <?= $data['creation_data_fr'] ?></em>
          		</h3>
          
          		<p>
          			<?= nl2br(htmlspecialchars($data['content'])) ?>
          			<br />
          			<em><a href="view/frontend/postView.php?id=<?= $data['id'] ?>">Commentaires</a></em>
          		</p>
          	</div>
          <?php
          }
          $posts->closeCursor();
          ?>
          <?php $content = ob_get_clean(); ?>
          
          <?php require('view/frontend/template.php'); ?>

          postView.php

          <!DOCTYPE html>
          <html>
          	<head>
          		<meta charset="utf-8" />
          		<title>Mon blog</title>
          		<link href="public/css/style.css" rel="stylesheet" />
          	</head>
          
          	<body>
          		<h1>Mon super blog !</h1>
          		<p><a href='index.php'>Retour à la liste des billets</a></p>
          
          		<div class="news">
          			<h3>
          				<?= htmlspecialchars($post['title']) ?>
          				<em>le <?= $post['creation_date_fr'] ?></em>
          			</h3>
          
          			<p>
          				<?= nl2br(htmlspecialchars($post['content'])) ?>
          			</p>
          		</div>
          
          		<h2>Commentaires</h2>
          
          		<?php
          		while ($comment = $comments->fetch())
          		{
          		?>
          			<p><strong><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date_fr'] ?></p>
          			<p><?= nl2br(htmlspecialchars($comment['comment'])) ?></p>
          		<?php
          		}
          		?>
          
          		<form action="index.php?action=addComment&amp;id=<?= $post['id'] ?>" method="post">
          			<div>
          				<label for="author">Auteur</label><br />
          				<input type="text" id="author" name="author" />
          			</div>
          			<div>
          				<label for="comment">Commentaire</label><br />
          				<textarea id="comment" name="comment"></textarea>
          			</div>
          			<div>
          				<input type="submit" />
          			</div>
          		</form>
          	</body>
          </html>

          template.php

          <!DOCTYPE html>
          <html>
          	<head>
          		<meta charset="utf-8" />
          		<title><?= $title ?></title>
          		<link href="public/css/style.css" rel="stylesheet" />
          	</head>
          
          	<body>
          		<?= $content ?>
          	</body>
          </html>







          -
          Edité par FrançoisPied 15 novembre 2019 à 8:36:23

          • Partager sur Facebook
          • Partager sur Twitter

          Vive le numérique

            19 novembre 2019 à 11:17:39

            Je ne comprend pas
            • Partager sur Facebook
            • Partager sur Twitter

            Vive le numérique

            Gestion de fichier php

            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
            × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
            • Editeur
            • Markdown