PHP #3 : tableaux & formulaires

Preview:

Citation preview

PHP : Tableaux & formulaires

1. Tableau

Définition

En informatique, un tableau (array en anglais) est une structure de données qui consiste en un ensemble d'éléments ordonnés accessibles par leur indice (ou index). C'est une structure de données de base que l'on retrouve dans chaque langage de programmation.

Source : http://fr.wikipedia.org

1.1 Tableau numéroté

Valeur VS Index

« Frodon » « Gandalf » « Aragorn » « Legolas » Valeurs

Index 0 1 2 3

Créer un tableau

<?php $characters = array('Frodon', 'Gandalf', 'Aragorn', 'Legolas'); ?>

Lire une valeur

<?php $characters = array('Frodon', 'Gandalf', 'Aragorn', 'Legolas'); echo $characters[0]; // Frodon echo $characters[1]; // Gandalf echo $characters[2]; // Aragorn echo $characters[3]; // Legolas ?>

Ajouter une valeur

<?php $characters = array('Frodon', 'Gandalf', 'Aragorn', 'Legolas'); $characters[] = 'Gimli'; echo $characters[4]; // Gimli $characters[5] = 'Boromir'; echo $characters[5]; // Boromir ?>

Modifier une valeur

<?php $characters = array('Frodon', 'Gandalf', 'Aragorn', 'Legolas'); $characters[1] = 'Gimli'; echo $characters[1]; // Gimli ?>

1.2 Tableau associatif

Valeur VS Index

« Frodon » « Gandalf » « Aragorn » « Legolas » Valeurs

Index Elijah Wood Ian McKellen Viggo Mortensen Orlando Bloom

Créer un tableau

<?php $leSeigneurDesAnneaux = array( 'Elijah Wood' => 'Frodon', 'Ian McKellen' => 'Gandalf', 'Viggo Mortensen' => 'Aragorn', 'Orlando Bloom' => 'Legolas' ); ?>

Lire une valeur

<?php $leSeigneurDesAnneaux = array( 'Elijah Wood' => 'Frodon', 'Ian McKellen' => 'Gandalf', 'Viggo Mortensen' => 'Aragorn', 'Orlando Bloom' => 'Legolas' );

echo $leSeigneurDesAnneaux['Ian McKellen']; //Gandalf ?>

Ajouter une valeur

<?php $leSeigneurDesAnneaux = array( 'Elijah Wood' => 'Frodon', 'Ian McKellen' => 'Gandalf', 'Viggo Mortensen' => 'Aragorn', 'Orlando Bloom' => 'Legolas' );

$leSeigneurDesAnneaux['Sean Bean'] = 'Boromir' echo $leSeigneurDesAnneaux['Sean Bean']; //Boromir ?>

Modifier une valeur

<?php $leSeigneurDesAnneaux = array( 'Elijah Wood' => 'Frodon', 'Ian McKellen' => 'Gandalf', 'Viggo Mortensen' => 'Aragorn', 'Orlando Bloom' => 'Legolas' );

$leSeigneurDesAnneaux['Viggo Mortensen'] = 'Grands-pas' echo $leSeigneurDesAnneaux['Viggo Mortensen']; // Grands-pas ?>

1.3 Tableau de tableaux de tableaux …

Valeur VS Index

« Frodon » « Gandalf » « Aragorn » « Legolas » Le seigneur des anneaux

Star wars

Matrix

« Yoda » « Anakin » « C-3PO » « R2-D2 » 

« Néo » « Trinity » « Morpheus » « Smith » 

0 1 2 3

Créer un tableau à deux dimensions

<?php $movies = array( 'Le seigneur des anneaux' => array( 'Frodon', 'Gandalf', 'Aragorn', 'Legolas'), 'Star wars' => array( 'Yoda', 'Anakin', 'C-3PO', 'R2-D2'), 'Matrix' => array( 'Néo', 'Trinity', 'Morpheus', 'Smith') ); ?>

Lire une valeur<?php $movies = array( 'Le seigneur des anneaux' => array( 'Frodon', 'Gandalf', 'Aragorn', 'Legolas'), 'Star wars' => array( 'Yoda', 'Anakin', 'C-3PO', 'R2-D2'), 'Matrix' => array( 'Néo', 'Trinity', 'Morpheus', 'Smith') );

echo $movies['Star wars'][2]; // C-3PO echo $movies['Matrix'][0]; // Néo ?>

Ajouter une valeur<?php $movies = array( 'Le seigneur des anneaux' => array( 'Frodon', 'Gandalf', 'Aragorn', 'Legolas'), 'Star wars' => array( 'Yoda', 'Anakin', 'C-3PO', 'R2-D2'), 'Matrix' => array( 'Néo', 'Trinity', 'Morpheus', 'Smith') );

$movies['Mad Max'] = array('Max Rockatansky', 'Jim Goose'); echo $movies['Mad Max'][1]; // Jim Goose ?>

Modifier une valeur<?php $movies = array( 'Le seigneur des anneaux' => array( 'Frodon', 'Gandalf', 'Aragorn', 'Legolas'), 'Star wars' => array( 'Yoda', 'Anakin', 'C-3PO', 'R2-D2'), 'Matrix' => array( 'Néo', 'Trinity', 'Morpheus', 'Smith') ); echo $movies['Matrix'][1]; // Trinity $movies['Matrix'][1] = 'Cypher'; echo $movies['Matrix'][1]; // Cypher ?>

1.4 Utilitaires

La boucle foreach (1)

<?php $leSeigneurDesAnneaux = array( 'Elijah Wood' => 'Frodon', 'Ian McKellen' => 'Gandalf', 'Viggo Mortensen' => 'Aragorn', 'Orlando Bloom' => 'Legolas' );

foreach ($leSeigneurDesAnneaux as $key => $value) { echo $key . ' : ' . $value; } ?>

La boucle foreach (2)

<?php $movies = array( 'Le seigneur des anneaux' => array( 'Frodon', 'Gandalf', 'Aragorn', 'Legolas'), 'Star wars' => array( 'Yoda', 'Anakin', 'C-3PO', 'R2-D2'), 'Matrix' => array( 'Néo', 'Trinity', 'Morpheus', 'Smith') );

foreach ($movies as $key => $value) { echo $key . ' : '; foreach ($value as $movieKey => $movie) { echo $movie. ', '; } echo '<br/>'; } ?>

Tester l’existence d’une variable

<?php $tab = array('truc', 'machin');

if(isset($tab[0])){ echo $tab[0]; // truc } if(isset($tab[2])){ echo $tab[2]; } ?>

Compter les éléments d’un tableau

<?php $tab = array('truc', 'machin');

echo count($tab); // 2 ?>

2. Formulaires

Le code html

<form action="test.php" method="POST"> <input type="text" name="toto" /> <input type="submit" /> </form>

Action & method

Action : page de traitement du formulaire

Method : GET ou POST

GET : transfert des données par l’urlPOST : transfert «invisible» des données

Récupérer les données envoyées

<?php echo $_GET['nom_du_champ_de_formulaire']; echo $_POST['nom_du_champ_de_formulaire']; ?>

Exemple (1)

<form action="test.php" method="POST"> <input type="text" name="toto" /> <input type="submit" /> </form>

index.php test.php

<?php echo $_POST[‘toto']; ?>

Exemple (2)

<form action="page.php" method="GET"> <input type="text" name="prenom" /> <input type="submit" /> </form>

index.php page.php

<?php echo $_GET[‘prenom']; ?>

Envoyer un paramètre sans formulaire

<a href="test.php?nom=michel&prenom=jean"> Mon super lien </a>

index.php

test.php

<?php echo $_GET['prenom']; echo $_GET['nom']; ?>

4. Mise en pratique

Exercice : En route pour l’école !

Créer la page « réponse » indiquant si l’utilisateur a entré la bonne réponse et la page « début de quizz ».

Ecrans disponibles ici : https://www.dropbox.com/sh/som5j63lbp6xbph/AAA1XV3kIht2x-MfBPfIho1Ia?dl=0

L’utilisateur doit pouvoir enchainer les questions / réponses à l’infinie.

Merci pour votre attention.

Recommended