Weitere ähnliche Inhalte Mehr von Jean Michel (18) PHP #3 : tableaux & formulaires3. 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
5. Valeur VS Index
« Frodon » « Gandalf » « Aragorn » « Legolas » Valeurs
Index 0 1 2 3
7. 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
?>
8. Ajouter une valeur
<?php
$characters = array('Frodon', 'Gandalf', 'Aragorn', 'Legolas');
$characters[] = 'Gimli';
echo $characters[4]; // Gimli
$characters[5] = 'Boromir';
echo $characters[5]; // Boromir
?>
11. Valeur VS Index
« Frodon » « Gandalf » « Aragorn » « Legolas » Valeurs
Index Elijah Wood Ian McKellen Viggo Mortensen Orlando Bloom
13. Lire une valeur
<?php
$leSeigneurDesAnneaux = array(
'Elijah Wood' => 'Frodon',
'Ian McKellen' => 'Gandalf',
'Viggo Mortensen' => 'Aragorn',
'Orlando Bloom' => 'Legolas'
);
echo $leSeigneurDesAnneaux['Ian McKellen']; //Gandalf
?>
14. 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
?>
15. 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
?>
17. 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
18. 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')
);
?>
19. 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
?>
20. 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
?>
21. 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
?>
23. 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;
}
?>
24. 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/>';
}
?>
25. 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];
}
?>
26. Compter les éléments d’un tableau
<?php
$tab = array('truc', 'machin');
echo count($tab); // 2
?>
28. Le code html
<form action="test.php" method="POST">
<input type="text" name="toto" />
<input type="submit" />
</form>
29. Action & method
Action : page de traitement du formulaire
Method : GET ou POST
GET : transfert des données par l’url
POST : transfert «invisible» des données
30. Récupérer les données envoyées
<?php
echo $_GET['nom_du_champ_de_formulaire'];
echo $_POST['nom_du_champ_de_formulaire'];
?>
33. 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'];
?>
35. 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.