-2

I have a form to add infos into a text file, but the problem is that when I do that I have one or two line breaks in a text field.

Maybe a \n or br? I don't where or how fix it.

gestionFilms.php

<?php

require("Films.php");

/* On lit le fichier */

$monfichier = fopen ("films.txt","r+") or die ("Impossible d<ouvrir le fichier");
$fichier = file("films.txt");  //Extraire tous le fichier d'un coups

/* On parcourt le contenu du fichier */
$contenu = "<h3>Contenu du fichier</h3><HR>";


if ($fichier)
    foreach ($fichier as $ligne) {

        /* On sépare les informations */
        $data = explode(' ', $ligne);


        /* On met en forme les informations récupérées */
        $numeroFilm = " Numero de film : " . ($data[0] ? $data[0] : null) . "<br />";
        $titre = " Titre : " . ($data[1] ? $data[1] : null) . "<br />";
        $realisateur = " Realisateur : " . ($data[2] ? $data[2] : null) . "<br />";
        $categorie = " Categorie: " . ($data[3] ? $data[3] : null) . " <br />";
        $duree = " Duree : " . ($data[4] ? $data[4] : null) . "<br />";
        $prix = " Prix : " . ($data[5] ? $data[5] : null) . "<br />";
        $nomImage = "Nom de l image : " . ($data[6] ? $data[6] : null) . "<br />";

        /* On passe à la ligne suivante */
        $objet1 = new Films ($numeroFilm, $titre, $realisateur, $categorie, $duree, $prix, $nomImage);
        //print_r($objet1);
       $objet1->information();
    }

//***************************************************************************************************

if (!empty($_POST)) {
    $numeroFilm = isset($_POST['numeroFilm']) ? $_POST['numeroFilm'] : "";
    $titre = isset($_POST['titre']) ? $_POST['titre'] : "";
    $realisateur = isset($_POST['realisateur']) ? $_POST['realisateur'] : "";
    $categorie = isset($_POST['categorie']) ? $_POST['categorie'] : "";
    $duree = isset($_POST['duree']) ? $_POST['duree'] : "";
    $prix = isset($_POST['prix']) ? $_POST['prix'] : "";
    $nomImage = isset($_POST['nomImage']) ? $_POST['nomImage'] : "";

    echo "les champs sont bien remplit <br>";

    $numeroFilm = $_POST['numeroFilm'];
    $titre = $_POST['titre'];
    $realisateur = $_POST['realisateur'];
    $categorie = $_POST['categorie'];
    $duree = $_POST['duree'];
    $prix = $_POST['prix'];
    $nomImage = $_POST['nomImage'];

    $objet2 = new Films ($numeroFilm, $titre, $realisateur, $categorie, $duree, $prix, $nomImage);

    $objet2->ajouterLigne();
}
else {
    echo "les champs sont mal remplit";
}

Films.php

<?php

class Films
{
    public $numeroFilm;
    public $titre;
    public $realisateur;
    public $categorie;
    public $duree;
    public $prix;
    public $nomImage;

    function __construct ($numeroFilm, $titre, $realisateur, $categorie, $duree, $prix, $nomImage)
    {
        $this->numeroFilm = $numeroFilm;
        $this->titre = $titre;
        $this->realisateur = $realisateur;
        $this->categorie = $categorie;
        $this->duree = $duree;
        $this->prix = $prix;
        $this->nomImage = $nomImage;
    }

    public function information()
    {
        echo $this->numeroFilm . $this->titre . $this->realisateur . $this->categorie . $this->duree . $this->prix . $this->nomImage;
        echo"---------------------------------<br>";

    }

    public function ajouterLigne()
    {

        $fichier = 'films.txt';
        // Ouvre un fichier pour lire un contenu existant
        $current = file_get_contents($fichier);
        // Ajoute une personne
        $current .= $this->numeroFilm." " . $this->titre." "  . $this->realisateur." " . $this->categorie." "  . $this->duree." "  . $this->prix." "  . $this->nomImage . "\n";

        // Écrit le résultat dans le fichier
        file_put_contents($fichier, $current);
    }
}
Community
  • 1
  • 1
Cedric
  • 1

1 Answers1

0

trim all your POST data especially those from text field or you can refer to this post Remove new lines from string

zimorok
  • 326
  • 1
  • 10