Lundi 2 mars 2009
Le switch pour tester les égalités
<?php
$lg="es";
switch($lg){
case "fr": echo"Bonjour";
break;
case "en": echo"Hello";
break;
case "es": echo"ola";
}
?>
compteur de boucle "i" "cp" (cp =compteur de boucles)
<?php
$cp="3";
while($cp>0)
//corps de boucle
{echo "<br> boucle n° ".$cp;
$cp--;
}
?>
$cp="3";
for($cp=3; $cp>0;$cp--)
//corps de boucle
{echo "<br> boucle n° ".$cp;
}
?>
<body>
<table width="200" border="1">
<?php
$cp="3";
for($cp=1; $cp<4;$cp++)
{
echo "<tr>
<td> n° ".$cp."</td> </tr>";
}
?>
</table>
</body>
<body>
<table width="200" border="1">
<?php
$cp="3";
for($cp=1; $cp<4;$cp++)
{
?>
<tr><td>
<?php echo "n° ".$cp;?>
</td>
</tr>
<?php}
?>
</body>
count permet d'incrémenter les valeurs
<body>
<table width="200" border="1">
<?php
$liste= array("toto","titi","paul");
//echo "<pre>";
//print_r ($liste);
//echo "</pre>";
//-------------------------
for($cp=0; $cp<count($liste);$cp++)
{
?>
<tr><td>
<?php echo $liste[$cp];?>
</td>
</tr>
<?php
}
?>
</table>
</body>
-----------------------------------------------
foreach uniquement adapté aux tableaux :
Code :
<body>
<table width="200" border="1">
<?php
$liste= array("toto","titi","paul","pierre");
foreach($liste as $nom)
{
?>
<tr><td>
<?php echo $nom;?>
</td>
</tr>
<?php
}
?>
</table>
</body>
--------------------------------------
<body>
<?php
if(isset($_GET['nom']))
{
echo $_GET['nom'];
}
?>
<form action="formulaire_affiche.php" method="get">
<p>nom : <input name="nom" type="text" maxlength="15" />
</p>
<p>
<label>
<input type="submit" name="valider" id="valider" value="valider" />
</label>
</p>
</form>
</body>
et efface le formulaire
<body>
<?php
if(isset($_GET['nom']))
{
echo $_GET['nom'];
}
else{
?>
<form action="isset.php" method="get">
<p>nom : <input name="nom" type="text" maxlength="15" />
</p>
<p>
<label>
<input type="submit" name="valider" id="valider" value="valider" />
</label>
</p>
</form>
<?php
}
?>
</body>
2) La redirection
<?php
if(isset($_GET['code']))
{
//echo $_GET['code'];
if ($_GET['code']=="1234")
header("location:pageok.html");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>page php</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="get">
<p>nom : <input name="code" type="text" maxlength="15" />
</p>
<p>
<label>
<input type="submit" name="valider" id="valider" value="valider" />
</label>
</p>
</form>
</body>
</html>
-------------------------------------------
Code :
<?php
$erreur=1;
if(isset($_GET['code']))
{
if ($_GET['code']=="1234")
header("location:pageok.html");
}
else{
$erreur=0;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>page php</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="get">
<p>nom : <input name="code" type="text" maxlength="15" />
</p>
<p>
<label>
<input type="submit" name="valider" id="valider" value="valider" />
</label>
</p>
</form>
<?php
if($erreur==1)
echo "Pas bon code";
?>
</body>
</html>
<?php
echo "<pre>";
print_r($_SERVER);
echo "<pre>";
?>
</body>
SELECT *FROM adherent WHERE naissance>1969
SELECT *FROM adherent WHERE nom like 'Amaral'
SELECT *FROM adherent WHERE (coursID=2) AND (naissance<1970)
SELECT *FROM adherent ORDER BY nom DESC nom par ordre décroissant
Jointure entre clé etrangère et clé primaire :
SELECT adherent.nom, cours.jour, cours.heure From adherent, cours WHERE adherent.coursID=cours.ID AND adherent.naissance=1969
SELECT adherent.nom, cours.jour, cours.heure From adherent, cours WHERE adherent.coursID=cours.ID AND adherent.nom='Amaral'
WHERE a.coursID=c.ID AND a.nom='Amaral'
Connection base de données
Code :
<?php
$i=mysql_connect("localhost", "root", "");
mysql_select_db("club_db");
$query="SELECT*FROM adherent";
$result=mysql_query($query,$i);
$row = mysql_fetch_array($result);
echo"<pre>";
print_r($row);
echo"</pre>"; ?>
$i=mysql_connect("localhost", "root", "");
mysql_select_db("club_db");
$query="SELECT*FROM adherent";
$result=mysql_query($query,$i);
while ($row = mysql_fetch_array($result))
{echo "<pre>";
echo $row['nom'];
echo "<pre>";
?>
$i=mysql_connect("localhost", "root", "");
mysql_select_db("club_db");
$query="SELECT*FROM adherent";
$result=mysql_query($query,$i);
while ($row = mysql_fetch_array($result))
{
echo "<pre>";
echo $row['nom'];
echo "<pre>";
}
?>