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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
<?php require_once('../connexion/localhost.php'); ?>
<?php
$conn = mysql_connect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("SET CHARACTER SET 'utf8';")or die(mysql_error());
mysql_select_db($database_localhost, $conn);
//param modifiables
$nb_par_page=2;
//
if(isset($_POST['valider']) || isset($_GET['page'])){ //le bouton valider a été cliqué ou appel par pagination
//nom du script
$currentScript = $_SERVER["PHP_SELF"];
//recup id_produit sélectionné ou passé par l'url
if(isset($_POST['categories'])){
$categorie_pagination = $_POST['categories'];
}else{
$categorie_pagination = $_GET['categ'];
}
//recup page demandée passée par l'URL
if(isset($_GET['page'])){
$page_courante=$_GET['page'];
}else{
$page_courante=0;
}
//calcul nombre de pages
$sql_pagination = "SELECT * FROM produits WHERE id_categorie = '".$categorie_pagination."'";
$requete_pagination = mysql_query($sql_pagination) or die( mysql_error());
$nb_enr=mysql_num_rows($requete_pagination);
$nb_pages=ceil($nb_enr/$nb_par_page);
//calcul limites d'affichage
$debut=$page_courante*$nb_par_page;
//requette avec les limites
$sql_pagination = "SELECT * FROM produits WHERE id_categorie = '".$categorie_pagination."' LIMIT ".$debut.",".$nb_par_page." ";
$requete_pagination = mysql_query($sql_pagination) or die( mysql_error());
?>
<!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>Base de Données</title>
</head>
<body>
<script type="text/javascript">
function supprimer(id)
{
if(confirm('Voulez vous vraiment supprimer ?'))
{
window.open("?Supprimer=Supprimer&id="+id,"_self");
}
}
</script>
<center>
<br />
<?php
if(isset($_GET['id'])){
$id = $_GET['id'];
mysql_query("DELETE FROM produits WHERE ID_produit='$id'")or die(mysql_error());
echo '<p style="color:#ff0000;font-weight:bold;">La catégorie a bien été supprimée</p>'."<br><br><a href=\"addproduit.php\">Ajouter une nouvelle produit</a>"."<br><br><a href=\"admin.php\">Revenir à l'accueil</a>";
}
else{
?>
</p>
<?php
if(isset($_POST['valider'])){ //le bouton valider a été cliqué
$categorie = $_POST['categories'];
$sql = "SELECT * FROM produits WHERE id_categorie = '".$categorie."'";
$requete = mysql_query($sql) or die( mysql_error());?>
<?php }}} ?>
</form>
<?php
//affichage
echo '<table align="center" border="1">'; //tableau
echo '<tr><th> ID produit </th><th> Nom produit </th></tr>';
while ($result = mysql_fetch_array($requete_pagination)) {
echo '<tr>'; //ligne
echo '<td align="center" valign="middle"> '.$result['ID_produit'].' </td>'; //colonne
echo '<td align="center" valign="middle"> '.$result['nom_produit'].' </td>';?>
<td align="center" valign="middle"><input type="button" name="Supprimer" value="Supprimer" onclick="supprimer(<?php echo $result['ID_produit']; ?>)"/></td>
<td align="center" valign="middle">
<?php
echo '<a href="modifier.php?ID_produit='.$result['ID_produit'].'">
Modifier</a>';
echo '</tr>'; //fin ligne
}
echo '</table>';
//pagination:
// gestion numeros de page + page courante
$i=0;
$j=1;
if($nb_enr>$nb_par_page){
while($i<$nb_pages){
if($i!=$page_courante){
echo "<a href=\"".$currentScript."?page=".$i."&categ=".$categorie_pagination."\"> ".$j." </a>";
//on passe par l'URL le numero de page demandée (commence à 0) et l'id_produit selectionné
} else {
echo "<a href=\"".$currentScript."?page=".$i."&categ=".$categorie_pagination."\" > <b> ".$j." </b> </a>";
}
$i++;
$j++;
}
}
?>
</center>
</body>
</html> |
Partager