Академический Документы
Профессиональный Документы
Культура Документы
Interprtation d'une
page PHP
client
serveur web
ORequte (URL)
OEnvoi de la page HTML
OInterprtation et affichage
de la page par le navigateur
OInterprtation
Interprteur PHP
Page HTML avec
code PHP intgr
Page HTML complte
du rsultat de
lexcution du code PHP
Base de
donnes
PHP - Introduction (2) PHP - Introduction (2)
6
PHP - Introduction (4) PHP - Introduction (4)
Codage
4
1 seul fichier ".php" = code HTML + code PHP
4
inclusion dans le code HTML par les balises
<?php
?>
Variables
4
introduites par $
4
ni dclaration (seulement initialisation), ni type
4
affichage
instruction print ou echo (nombres et chanes)
$ch = "Bonjour tout le monde";
print $ch;
fonction print_r (toutes variables, dont tableaux)
$tab = array(3,2,1);
print_r ($tab);
+ code HTML : Array
(
[0] => 3
[1] => 2 + page web :
[2] => 1 Array ( [0] => 3 [1] => 2 [2] => 1 )
)
16
Spcificits de PHP (3) Spcificits de PHP (3)
Chanes
4
Guillemets simples ou doubles ?
simples : pas d'interprtation des variables internes (VI)
doubles : interprtation des VI et squences d'chappement
4
Exemples
code PHP code HTML page web
$age = 23;
$nom = 'toto'; //ou : $nom="toto";
mon nom est toto mon nom est toto
mon nom est $nom mon nom est $nom
mon nom est toto mon nom est toto
mon nom est <b>toto</b>
mon nom est <b>$nom</b>
print "mon nom est $nom";
print 'mon nom est $nom';
print 'mon nom est '.$nom;
print "mon nom est <b>$nom</b>"; mon nom est toto
print 'mon nom est <b>$nom</b>'; mon nom est $nom
print "Je m'appelle\n \"$nom\"
<br>et j'ai ".$age.' ans';
je m'appelle
"toto" <br>et j'ai 23 ans
je m'appelle "toto"
et j'ai 23 ans
17
Spcificits de PHP (4) Spcificits de PHP (4)
Tableaux associatifs
4
Gnralits
ensemble d'associations (cl => valeur)
tableau "classique" = tableau associatif cls numriques
4
Oprations
initialisation
$tab = array('cl1'=>'val1', 'cl2'=>'val2', ...);
Tableaux
4
Parcours
Utilisation des valeurs seulement (tableaux numriques)
foreach ($tab as $valeur)
print "valeur = $valeur<br>";
Utilisation des cls et des valeurs (tableaux associatifs)
foreach ($tab as $cle=>$valeur)
print "cl = $cle, valeur = $valeur<br>";
4
Fonctions (dans l'ordre d'utilit)
fusionner tableau en chane : $ch = join($sep, $tab)
scinder chane en tableau : $tab = split($sep, $ch)
nb lments : count()
trier : sort(), rsort() ; chercher : in_array(), array_search()
form.html
<FORM action='traiter.php' method='POST'>
Prénom <INPUT name='prenom' type='text'>
<INPUT type='submit'>
</FORM>
21
PHP et pages web dynamiques (1) PHP et pages web dynamiques (1)
form.html
<FORM action='traiter.php' method='POST'>
Prénom <INPUT name='prenom' type='text'>
<INPUT type='submit'>
</FORM>
traiter.php
<?php
print_r($_POST);
?>
Olivier| Array ( [prenom] => Olivier )
29
PHP - Interfaage avec un SGBD (1) PHP - Interfaage avec un SGBD (1)
Exemple
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//FR">
<html>
<head><title>Promotion</title></head>
<body>
<table border="1" cellpadding="0" cellspacing="0">
<tr><th>Nom</th><th>Adresse mail</th></tr>
<?php
//Paramtres de connexion
$host="localhost"; //machine hbergeant le SGBD
$user="lo"; //votre login
$password="cmoi"; //mot de passe
$bdd="etudiants"; //nom de la base
mysql_connect($host, $user,$password) or
die("erreur de connexion au serveur");
mysql_selectdb($bdd) or
die("erreur de connexion la base");
31
PHP - Interfaage avec un SGBD (3) PHP - Interfaage avec un SGBD (3)
// Cration et envoi de la requte
$ch_req ="SELECT nom,mail FROM Promo ORDER BY nom";
$result = mysql_query($ch_req);
// Rcupration des rsultats
while($ligne = mysql_fetch_row($result)){
$nom=$ligne[0];
$mail=$ligne[1];
print "<tr>\n<td>";
print "<a href=\"$mail\">$nom</a>";
print "</td>\n<td>$mail</td>\n</tr>\n";
}
// Dconnexion de la base de donnes
mysql_close();
?>
</tr>
</table>
</body>
</html>
2/2