1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| <html>
<SCRIPT LANGUAGE="JavaScript">
function ModifierListe(code_ville)
{
lg = document.MonFormulaire.ListeVille.length;
// ON VIDE LA LISTE DES VILLES
for (i = lg - 1; i >= 0; i--)
{
document.MonFormulaire.ListeVille.options[i] = null;
}
code_pays = document.MonFormulaire.ListePays.selectedIndex;
<?php
// CONNEXION A LA BASE DE DONNEES
$id_connexion = mysql_connect("localhost","root","");
mysql_select_db("exercice") or die("Could not select database");
$sql = "SELECT code FROM demo_pays ORDER BY code";
$resultat = mysql_query($sql) or die("Query failed");
// Génération des Villes par Pays
$max_lignes = 0;
$option_max = '';
while ($enr = mysql_fetch_array($resultat)) {
$sql = "SELECT code, ville FROM demo_ville WHERE code_pays=$enr[0] ORDER BY ville";
$resultat2 = mysql_query($sql) or die("Query failed");
echo " if (document.MonFormulaire.ListePays.options[code_pays].value == ".$enr[0].") {\n";
echo " document.MonFormulaire.ListeVille.length = ".(mysql_num_rows($resultat2)).";\n";
$cpt = 0;
while ($enr2 = mysql_fetch_array($resultat2)) {
echo " document.MonFormulaire.ListeVille.options[".$cpt."].value = ".$enr2[0].";\n";
echo " document.MonFormulaire.ListeVille.options[".$cpt."].text = \"".$enr2[1]."\";\n";
echo " if (code_ville== ".$enr2[0].") document.MonFormulaire.ListeVille.options[".$cpt."].selected = true;\n";
$cpt++;
if ($cpt > $max_lignes) $max_lignes = $cpt;
if (strlen($enr2[1]) > strlen($option_max)) $option_max = $enr2[1];
}
echo " }\n";
}
?>
}
</SCRIPT>
<?php
// ----------------------------------------------------------------------------
// LISTE DES PAYS
// ----------------------------------------------------------------------------
$sql = "SELECT code, pays FROM demo_pays ORDER BY pays";
$resultat = mysql_query($sql) or die("Query failed");
echo "<FORM METHOD=POST NAME='MonFormulaire' action='".$_SERVER['PHP_SELF']."' >";
echo "<BR> <B>PAYS :</B> ";
echo " <SELECT NAME='ListePays' onChange='ModifierListe(-1)'>\n";
if (!isset($ListePays)) $ListePays = - 1;
while ($enr = mysql_fetch_array($resultat)) {
echo "<OPTION VALUE='".$enr[0]."'";
if ($ListePays == $enr[0]) echo " SELECTED";
echo ">".htmlspecialchars($enr[1])."</OPTION>\n";
}
echo "</SELECT> \n";
// ----------------------------------------------------------------------------
// LISTE DES VILLES
// ----------------------------------------------------------------------------
echo " <B>VILLE :</B> ";
echo " <SELECT NAME='ListeVille'>\n";
for ($cpt = 0; $cpt < $max_lignes; $cpt++)
echo "<OPTION>".ereg_replace(".", "--", $option_max)."</OPTION>\n";
echo "</SELECT> \n";
if (!isset($ListeVille)) $ListeVille = -1;
echo "<SCRIPT LANGUAGE='JavaScript'>\n;ModifierListe(".$ListeVille.");\n</SCRIPT>\n";
// ----------------------------------------------------------------------------
echo "<br><br>";
echo "<INPUT TYPE='submit' VALUE='Valider'>\n";
echo "</FORM>";
echo "<br><br>";
// ----------------------------------------------------------------------------
// Résultats des sélections
// ----------------------------------------------------------------------------
$sql = "SELECT pays FROM demo_pays WHERE code='".$ListePays."'";
$resultat = mysql_query($sql) or die("Query failed");
$enr = @mysql_fetch_array($resultat);
echo "Pays = $ListePays [$enr[0]]<BR>\n";
if (isset($ListePays) && $ListePays != "") {
$sql = "SELECT pays FROM demo_pays WHERE code='".$ListePays."'";
$resultat = mysql_query($sql) or die("Query failed");
$enr = @mysql_fetch_array($resultat);
echo "Pays = $ListePays [$enr[0]]<BR>\n";
}
if (isset($ListeVille) && $ListeVille != "" && $ListeVille != -1) {
$sql = "SELECT ville FROM demo_ville WHERE code='".$ListeVille."'";
$resultat = mysql_query($sql) or die("Query failed");
$enr = @mysql_fetch_array($resultat);
echo "Ville = $ListeVille [$enr[0]]<BR>\n";
}
?>
<body>
</body>
</html> |