Top Banner
62

HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

Jun 25, 2018

Download

Documents

trinhcong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

Plan

1 HTML

2 CSS

3 PHP

4 (My)SQL

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 2 / 63

Page 2: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 3 / 63

Page 3: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Introduction

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 4 / 63

Page 4: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Introduction

Généralités sur l'HTML

Language descriptif

Pas de séquences de contrôleDescription de la sémantique du document

Balises

Balise ouvrante : <TAG>Balise fermante : </TAG>Les deux en une : <TAG />

Standardisé

W3C : http ://www.w3c.orgDernière version : HTML 4.01

Strict

Transitional

Frameset

Validation automatique : http ://validator.w3.orgBalise DOCTYPE, sur la première ligne du �chier

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http ://www.w3.org/TR/html4/loose.dtd">

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 5 / 63

Page 5: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Introduction

Généralités sur l'HTML

Encapsulation de type "pile"

(Mal)formation

<A> <B> </A> </B> i n c o r r e c t

<A> <B> </B> </A> c o r r e c t

Structure arborescente

Arbre minimal

<HTML><HEAD>

<TITLE /></HEAD><BODY />

</HTML>

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 6 / 63

Page 6: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Introduction

Généralités sur l'HTML

Commentaires entre <!−− et −−>

HTML + XML => XHTML

Balises toujours ferméesTransformation en d'autres formats

HTML donne un découpage selon la sémantique du document

Mise en forme visuelle

en HTML : moins lisible, plus de codeen CSS : plus lisible, regroupement et généralisation

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 7 / 63

Page 7: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Texte en HTML

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 8 / 63

Page 8: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Texte en HTML

Espaces

Tous les espaces blancs (y compris \t et \n) sont ignorés

Cas particulier : l'espace seul => reproduit tel quel

Exemple

Mon t e x t e avec des e spac e se t p l u s i e u r s l i g n e s

n ' a p p a r a i t pas comme c e c i

Résultat

Mon t e x t e avec des e spac e s e t p l u s i e u r s l i g n e s n ' a p p a r a i t pas comme c e c i

Il faut utiliser des balises

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 9 / 63

Page 9: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Texte en HTML

Texte

Mise en forme au niveau paragraphe

<BR> : aller à la ligne

&nbsp; : insérer un espace

<P> : commencer un nouveau paragraphe

entre <PRE> et </PRE>, tout est reproduit tel quel

Mise en forme au niveau phrase

<EM> à </EM> : Mise en valeur (italique)

<STRONG> à </STRONG> : Mise en valeur forte (gras)

<CITE> à </CITE> : Citation courte (gras italique)

<Q> à </Q> : Citation courte (entre guillements)

<BLOCKQUOTE à </BLOCKQUOTE> : Citation longue (gras)

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 10 / 63

Page 10: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Texte en HTML

Texte

Mise en forme au niveau phrase

<CODE> à </CODE> : Extrait de code source (gras italique petit)

<ABBR> à </ABBR> : Abbréviation (gras italique)

<ACRONYM> à </ACRONYM> : Acronyme (gras italique)

<SUP> à </SUP> : Mettre en exposant

<SUB> à </SUB> : Mettre en indice

<BIG> à </BIG> : Plus gros

<SMALL> à </SMALL> : Plus petit

Beaucoup d'autres : <DFN>, <SAMP>, <KBD>, <VAR>, <INS>, <DEL>, . . .

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 11 / 63

Page 11: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Texte en HTML

Texte

Liens hypertexte

Entre <A> et </A>

Choisir le(s) bon(s) mot(s) pour servir de lien

Liens internes (<A NAME=...>) ou externes (<A HREF=...>)

Possibilité de lien sur une image

Lier les pages de manière cohérente

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 12 / 63

Page 12: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Listes en HTML

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 13 / 63

Page 13: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Listes en HTML

Listes non-ordonnées

<UL> marque le début de la liste et </UL> sa �n

<LI> permet de commencer un nouvel item dans la liste

Exemple

<UL><LI>1<SUP>er</SUP> element<LI>2<SUP>eme</SUP> element<LI>3<SUP>eme</SUP> element

</UL>

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 14 / 63

Page 14: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Listes en HTML

Listes ordonnées

1 <OL> marque le début de la liste et </OL> sa �n

2 <LI> permet de commencer un nouvel item dans la liste

Exemple

<OL><LI>1<SUP>er</SUP> element<LI>2<SUP>eme</SUP> element<LI>3<SUP>eme</SUP> element

</OL>

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 15 / 63

Page 15: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Listes en HTML

Listes de dé�nitions

<DL> marque le début de la liste et </DL> sa �n

<DT> permet de dé�nir un titre pour une dé�nition

<DD> permet de dé�nir le contenu d'une dé�nition

Exemple

<DL><DT><STRONG>Cout</STRONG><DD>Pr i x de r e v i e n t<DT><STRONG>Bene f i c e</STRONG><DD>Gain r e a l i s e par une pe r sonne ou une c o l l e c t i v i t e

</DL>

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 16 / 63

Page 16: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Tableau en HTML

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 17 / 63

Page 17: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Tableau en HTML

Forme du tableau

Un tableau commence par <TABLE> et �nit par </TABLE>

Une ligne dans un tableau commence par <TR> et �nit par </TR>

Une cellule dans une ligne commence par <TD> et �nit par </TD>

Une cellule d'en-tête dans une ligne commence par <TH> et �nit par</TH>

Exemple : 2 lignes x 3 colonnes

<TABLE><TR>

<TD>C e l l u l e 1 ,1</TD><TD>C e l l u l e 1 ,2</TD><TD>C e l l u l e 1 ,3</TD>

</TR><TR>

<TD>C e l l u l e 2 ,1</TD><TD>C e l l u l e 2 ,2</TD><TD>C e l l u l e 2 ,3</TD>

</TR></TABLE>

Cellule 1,1 Cellule 1,2 Cellule 1,3

Cellule 2,1 Cellule 2,2 Cellule 2,3

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 18 / 63

Page 18: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Tableau en HTML

Forme du tableau

On peut donner un "titre" au tableau entre <CAPTION> et </CAPTION>, endehors des dé�nitions de lignes ou de cellules

On peut ranger les lignes entre <THEAD> et </THEAD> pour indiquerqu'elles font partie de l'en-tête

On peut ranger les lignes entre <TFOOT> et </TFOOT> pour indiquerqu'elles font partie du pied du tableau

On peut ranger les lignes entre <TBODY> et </TBODY> pour indiquerqu'elles font partie des données

Attention : <THEAD> et <TFOOT> doivent apparaître avant <TBODY>

Il peut y avoir plusieurs <TBODY>

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 19 / 63

Page 19: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Tableau en HTML

Forme du tableau

On peut donner des indications de taille sur les colonnes entre<COLGROUP> et </COLGROUP>

Entre ces balises, on indique avec <COL width=...> la taille d'une colonne

On peut aussi utiliser <COLGROUP span=XX width=YY> pour spéci�er XXcolonnes de taille YY

Les balises <TD> et <TH> ont les attributes :

rowspan pour indiquer sur combien de lignes s'étant la cellulecolspan pour indiquer sur combien de colonnes s'étant la cellulealign pour indiquer l'alignement horizontal dans la cellule (left, right oucenter)valign pour indiquer l'alignement vertical dans la cellule (top, middle,bottom)

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 20 / 63

Page 20: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

HTML Tableau en HTML

Bordures du tableau

<TABLE> a les attributs :

border pour indiquer l'épaisseur des bordures

rules pour indique le type de bordure entre les cellules :

none : pas de borduregroups : bordures entre les groupes de lignes (<THEAD>, <TFOOT>,<TBODY> et les groupes de colonnes (<COLGROUP>, <COL>)rows : bordures entre les lignes uniquementcols : bordures entre les colonnes uniquementall : bordures autour de toutes les cellules

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 21 / 63

Page 21: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

CSS

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 22 / 63

Page 22: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

CSS Utilité

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 23 / 63

Page 23: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

CSS Utilité

Mise en forme

Séparation de la forme et du fond

HTML décrit le fondCSS décrit la forme

Centralisation de l'aspect visuel

On insère du CSS entre :<STYLE type=" t e x t / c s s "><!−−

du CSS i c i−−></STYLE>

Ou on lie un �chier CSS avec :<LINK r e l=" s t y l e s h e e t " type=" t e x t / c s s " h r e f=" . . . ">

Ou on ajoute du CSS à une balise :

<BALISE s t y l e=" . . . ">

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 24 / 63

Page 24: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

CSS Syntaxe

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 25 / 63

Page 25: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

CSS Syntaxe

Format d'une classe CSS

Le CSS est formé d'un ensemble de classes.

Une classe s'écrit de cette façon :nom {

a t t r i b u t : v a l e u r ;. . .

}

nom peut être :

Un nom de balise : les attributs s'appliquent à toutes ces balisesUn nom générique (commençant par un point) : les attributss'appliquent aux balises utilisant class="nom" (sans le point)Un mélange des deux, séparés par des virgules : les attributss'appliquent suivant les deux points précédents

attribut désigne quel élément visuel est modi�é (couleur, bordure,fond, marges . . ..

valeur désigne par quelle valeur est remplacée l'attribut désigné

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 26 / 63

Page 26: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

CSS Syntaxe

Exemple de CSS

style.css

body {font−f am i l y : A r i a l ;

}p {

background−c o l o r :#F0C0C0 ;bo rde r : t h i n s o l i d b l a c k ;

}. t i t r e {

c o l o r : y e l l ow ;}

index.html

<HTML><HEAD>

<LINK r e l=" s t y l e s h e e t " type=" t e x t / c s s " h r e f=" s t y l e . c s s "><TITLE>Exemple CSS</TITLE>

</HEAD><BODY>

<H1 c l a s s=" t i t r e ">Exemple</H1><P>Un parag raphe avec bo rdu re e t c o u l e u r de f o n t

</BODY></HTML>

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 27 / 63

Page 27: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 28 / 63

Page 28: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Introduction

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 29 / 63

Page 29: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Introduction

Pages dynamiques

Exemple statique

<HTML><HEAD><TITLE>Page s t a t i q u e</TITLE></HEAD><BODY>

Nous sommes l e 28/03/2007</BODY>

</HTML>

Problème : A�cher une page di�érente en fonction de l'utilisateur, del'environnement, . . .

Solution : Utiliser un langage de programmation évolué, par exemplePHP.

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 30 / 63

Page 30: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Introduction

Présentation

Langage récent (crée en 1994)

Versions utilisée :

4.3 (plus répandue)5.0 (avec une couche objet)

Langage de script

Langage interprétéPrésence d'un interpréteur côté serveur

Intégré au code HTML

Syntaxe proche du C et du Java

Interface simple avec beaucoup de SGBD

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 31 / 63

Page 31: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Introduction

Modèle d'exécution

1 Le client demande une page PHP2 Le serveur web exécute le code de la page

1 Lancement de l'interpréteur2 Exécution du code

3 Le serveur web renvoie le résultat de l'exécution

4 Le client a�che le résultat

Pour le client, il est impossible de voir le code PHP

Seul le résultat de l'exécution est récupéré par le client

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 32 / 63

Page 32: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Introduction

Premier exemple

Code côté serveur

<HTML><HEAD><TITLE>Page dynamique</TITLE></HEAD><BODY><?php

echo ( "Nous sommes l e " ) ;echo ( date ( " j /m/Y" ) ) ;

?></BODY>

</HTML>

Résultat côté client<HTML>

<HEAD><TITLE>Page statique</TITLE></HEAD>

<BODY>Nous sommes le 12 mars 2008

</BODY></HTML>

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 33 / 63

Page 33: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 34 / 63

Page 34: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Mélange HTML/PHP

PHP s'intègre dans l'HTML entre <?php et ?>

Les instructions se �nissent par ;

Les commentaires sont soit entre /*et */, soit après //

Manuel complet en français : http ://www.php.net/manual/fr

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 35 / 63

Page 35: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Les variables

Les variables sont pré�xées par $

Leur nom suit les règles classiques

Exemple : $my_var_03

Les noms sont sensibles à la casse : $var 6= $Var

Pas de déclaration, typage implicite

Exemple :

$my_var_03 = 54 ; // Maintenant , c ' e s t un e n t i e r$my_var_03 = " p i f " ; // Maintenant , c ' e s t une cha i n e

Attention aux fautes de frappes dans les noms de variables

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 36 / 63

Page 36: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Les types

Entiers : 54

Flottants : 54.3

Chaînes : "54" ou '54'

Booléens : false ou true

Tableaux

Fonctions de test :

isset ($var) : renvoie true si $var existeunset($var) : détruit $var

is_integer ($var), is_string ($var), . . . : renvoie true si $var est un entier, unechaîne, . . .

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 37 / 63

Page 37: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Les constantes et l'a�chage

Constantes

On les dé�nit à l'aide de la commande de�ne

Exemples : de�ne ("PI", 3.14)

On les utilise directement (sans $) : echo(PI)

A�chage

On peut a�cher avec la commande echo (avec ou sans parenthèses)

print est équivalente à echo

On peut faire un a�chage comme en C avec printf

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 38 / 63

Page 38: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Opérateurs

Arithmétiques : + − ∗ / % ++ −−

A�ectation : = .= += −= ∗= /= %=

Comparaison : == < != > === <= !== >=

Logiques : and && or || xor !

Conditionnel : ... ? ... : ...

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 39 / 63

Page 39: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Conditionnelles et boucles

i f ( cond ) {. . .}e l s e i f ( cond ) {. . .}e l s e {. . .}

sw i t c h ( exp r ) {ca se VALEUR_1:

. . .b reak ;

ca se VALEUR_2:. . .b reak ;

d e f a u l t :. . .b reak ;

}

f o r ( i n i t ; cond ; modi f ) {. . .}

wh i l e ( cond ) {. . .}

do {. . .} wh i l e ( cond ) ;

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 40 / 63

Page 40: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Les tableaux

Chaque élément du tableau a une clé et une valeur

Pas de déclaration du tableau

Les valeurs des éléments ne sont pas forcément toutes du même type

Exemple de remplissage à la volée :

$tab [ 0 ] = 54 ;$tab [ 1 ] = " p i f " ;$tab [ " pa f " ] = f a l s e ;

Exemple de remplissage direct :

$tab = a r r a y (54 , " p i f " ) ;$tab = a r r a y ( " pa f " => f a l s e ) ;

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 41 / 63

Page 41: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Parcours de tableaux

Parcours "classique" avec for

Parcours spéci�que :

f o r e a c h ( $tab as $va l u e ) {. . .}

f o r e a c h ( $tab as $key => $va l u e ) {. . .}

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 42 / 63

Page 42: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Les chaînes de caractères

Délimitées par ' : contenu non interprété

Délimitées par " : contenu interprété

Les unes peuvent contenir les autres

Concaténation avec .

Exemple :

$ p i f = " to to " ; // Con t i en t " to t o "$pa f = " comme $ p i f " ; // Con t i e n t " comme to to "$pouf = ' pas comme $ p i f ' ; // Con t i e n t " pas comme $ p i f "$bim = $ p i f . $pa f ; // Con t i en t " to t o comme to to "

Accès à un caractère : $bim[0]

strlen ( $str ) : longueur de $str

substr ( $str , start [, len ]) : sous-chaîne de $str commençant au caractère start,et faisant éventuellement len caractères de long

Comparaison avec ==, === ou strcmp

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 43 / 63

Page 43: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Les fonctions

f u n c t i o n ma_fonc ( $param1 , $param2 , . . . ) {. . .r e t u r n . . . ;

}

Pas de type pour les paramètres ou la valeur de retour

Nombre �xé de paramètres

Le nom ne commence pas par $

Le nom est insensible à la casse

Le résultat est renvoyé avec la commande return

Une seule valeur de retour

Passage des paramètres par valeur (par défaut)

Passage par référence : &$param

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 44 / 63

Page 44: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Les variables utilisées à l'intérieur d'une fonctions sont détruites à la�n, sauf :

si on les dé�nit avec static

si on les dé�nit avec global

f u n c t i o n ma_fonc ( ) {s t a t i c $ appe l s = 0 ;$ appe l s++;echo ( "J ' a i e t e a p p e l l e e $appe l s f o i s " ) ;

}

f u n c t i o n ma_fonc2 ( ) {g l o b a l $va r ;$va r = 54 ;

}

$va r = 0 ;ma_fonc2 ( ) ;echo ( $va r ) ;

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 45 / 63

Page 45: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP Eléments du langage

Inclusion de �chiers

On utilise require (" �chier "), include (" �chier "), require_once(" �chier "),include_once(" �chier ")

Les variantes include provoquent des warnings au lieu d'erreurs en cas deproblème

Les variantes _once n'incluent le �chier que si celui n'a pas déjà été inclu

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 46 / 63

Page 46: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP PHP avec HTML

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 47 / 63

Page 47: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP PHP avec HTML

Utilisation des formulaires

On peut "dialoguer" avec le visiteur en utilisant les formulaires

En méthode GET : données encodées dans l'URLindex .php?var=value&var2=value2...

En méthode POST : données cachées mais pas de navigation avecPrécédent/Suivant

Exemple de formulaire

<FORM ac t i o n=" t r a i t emen t . php" method=" pos t ">prenom : <INPUT type=" t e x t " name="prenom">age : <INPUT type=" t e x t " name="age"><INPUT type=" submit " v a l u e="Envoyer ">

</FORM>

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 48 / 63

Page 48: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP PHP avec HTML

Eléments de formulaire

Il existe di�érents type pour les balises INPUT :

text : une zone de texte sur une seule ligne

password : idem, mais avec a�chage d'étoiles

�le : permet la selection d'un �chier

checkbox : une case à cocher

button : un bouton simple (pas d'action sans javascript)

hidden : un champ "texte" caché

radio : un bouton d'option

reset : un bouton de remise à zéro

submit : un bouton de soumission

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 49 / 63

Page 49: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP PHP avec HTML

PHP avec formulaires

Le script de traitement des formulaires reçoit un tableau pré-rempli

$_GET pour la méthode GET

$_POST pour la méthode POST

Il contient les données du formulaire

Exemple de traitement

<?php$prenom = $_POST[ "prenom" ] ;$age = $_POST[ "age" ] ;echo ( "Bonjour $prenom , vous avez $age ans " ) ;

?>

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 50 / 63

Page 50: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP PHP avec HTML

Persistence des données

On veut parfois garder de l'information entre plusieurs pages :

Login / PasswordPréférences de navigationSélection de produits à acheter (panier, ...)

On utilise donc les sessions PHP.

Les sessions permettent de stocker des informations côté serveurElles sont identi�ées par un numéro qui reste valide tant que le visiteurreste connectéLe numéro est transmis au serveur soit dans l'URL, soit dans un cookie

Les données se placent et se récupèrent dans $_SESSION, comme pourles formulaires

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 51 / 63

Page 51: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

PHP PHP avec HTML

Utilisation des sessions

Les sessions utilisent les cookies : il faut donc ouvrir la session avant

d'a�cher quoi que ce soit (voir fonction setcookie)

Note : Les valeurs des cookies sont dans le tableau pré-rempli $_COOKIE

Le cookie utilisé (ou la variable dans $_GET à defaut) s'appellePHPSESSID

La session existe dès qu'elle est crée et jusqu'à ce qu'elle soit détruite

Création (et réouverture) : session_start ()

Destruction : session_destroy ()

Note : les session s'autodétruisent après un certain temps(généralement 30 min)

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 52 / 63

Page 52: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 53 / 63

Page 53: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL Introduction à SQL

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 54 / 63

Page 54: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL Introduction à SQL

Dé�nition

SQL est un langage puissant de requête

Il permet de faire une demande complexe à une base de données dansun langage proche de l'anglais

On l'utilise pour récupérer, ajouter, supprimer et créer des donnéesdans une base de données

Les bases de données utilisent des tables :

chaque ligne est un enregistrement de champs avec des valeursles requêtes se font sur ces champs

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 55 / 63

Page 55: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL Introduction à SQL

Exemple

Table 'PersosXVI'

Nom Prenom Age Activite

CARTIER Jacques 44 Explorateur

CALVIN Jean 26 Réformateur

CHASTEL Jean 19 Assassin

PARE Ambroise 44 Chirurgien

Requête interrogativeSELECT ∗ FROM PersosXVI;

Requête sélectiveSELECT Nom, Prenom FROM PersosXVI;

Requête restrictiveSELECT ∗ FROM PersosXVI WHERE Prenom = 'Jean';

On peut complètement mélanger les types de requêtes

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 56 / 63

Page 56: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL Introduction à SQL

Exemple complexe

Table 'PersosXVI'

Nom Prenom Age Activite Roi

CARTIER Jacques 44 Explorateur 3

CALVIN Jean 26 Réformateur 1

CHASTEL Jean 19 Assassin 4

PARE Ambroise 44 Chirurgien 2

Table 'RoiXVI'

Id Nom

1 Henri II

2 Charles IX

3 Francois Ier

4 Henri IV

Requête croiséeSELECT p.Nom, p.Activite FROM PersosXVI p, RoiXVI r WHERE p.Roi = r.Id AND r.Nom = 'Francois Ier'

ORDER BY p.Nom DESC;

Résultat

CARTIER Explorateur

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 57 / 63

Page 57: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL Introduction à SQL

Exemple de fonctions

Table 'PersosXVI'

Nom Prenom Age Activite

CARTIER Jacques 44 Explorateur

CALVIN Jean 26 Réformateur

CHASTEL Jean 19 Assassin

PARE Ambroise 44 Chirurgien

DécompteSELECT count(∗) FROM PersosXVI;

SommeSELECT sum(Age) FROM PersosXVI;

Ce type de requête peut simpli�er le code de traitement

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 58 / 63

Page 58: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL Introduction à SQL

Manipulation des données

Table 'PersosXVI'

Nom Prenom Age Activite

CARTIER Jacques 44 Explorateur

CALVIN Jean 26 Réformateur

CHASTEL Jean 19 Assassin

Requête d'insertionINSERT INTO PersosXVI(Nom, Prenom, Age, Activite) VALUES('PARE', 'Ambroise', 44, 'Chirurgien');

Requête de mise à jourUPDATE PersosXVI SET Age = 43 WHERE Nom = 'CARTIER';

Requête de suppressionDELETE FROM PersosXVI WHERE Prenom = 'Jean';

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 59 / 63

Page 59: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL MySQL avec PHP

Plan

1 HTMLIntroductionTexte en HTMLListes en HTMLTableau en HTML

2 CSSUtilitéSyntaxe

3 PHPIntroductionEléments du langagePHP avec HTML

4 (My)SQLIntroduction à SQLMySQL avec PHP

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 60 / 63

Page 60: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL MySQL avec PHP

Accès au serveur

Connexion$c = mysql_connect("localhost", "login", "mdp");

$c est false en cas d'erreur de connexion

Choix de la base$s = mysql_select_db("nom", $c);

$s est false en cas d'erreur

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 61 / 63

Page 61: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL MySQL avec PHP

Requêtes

Exécution$res = mysql_query("une requete", $c);

$res est false en cas d'erreur

Récupération de(s) résultat(s)

$row = mysql_fetch_row($res);

$tab = mysql_fetch_array($res);

$obj = mysql_fetch_object($res);

$row[0]; $row[1]; ...

$tab["champ"]; ... ou $tab [0]; ...

obj−>champ; ...

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 62 / 63

Page 62: HTML CSS PHP (My)SQL - academiepro.com · HTML Plan 1 HTML Introduction exteT en HTML Listes en HTML ableauT en HTML 2 CSS Utilité Syntaxe 3 PHP Introduction Eléments du langage

(My)SQL MySQL avec PHP

Fonctions annexes

mysql_num_rows($res) : retourne le nombre de résultats d'un SELECT

mysql_a�ected_rows($res) : retourne le nombre de ligne a�ectées par unINSERT, un UPDATE ou un DELETE

mysql_insert_id($c) : retourne le dernier incrément d'un champAUTO_INCREMENT

Attention aux injections SQL !

[email protected] (LORIA) Cours HTML / CSS / PHP / MySQL 12 mars 2008 63 / 63