Sélection de lignes et affichage dans TABLE
bonjour tout le monde
revenant sur mon exemple, j ai réaliser un tableau d'affichage des produits, tous sa fonctionne bien
ma deuxième étape j'ai crée une colonne qui compare si la quantité est inferieur a 5 donc une colonne porte que "1" ou "0" voir calcule.php
a partir de la, je voulais afficher uniquement le tableau mais les ligne qui contient "1" uniquement dans calculeetat1.php et ici le souci vu que je suis débutant
merci bien pour votre aide et suggestion
code:
index.php
Code:
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
| <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>php5</title>
<style>
input{
margin-bottom: 15px;
}
</style>
</head>
<body>
<!-- créer formulaire -->
<form action="" method="post">
<label for="">Nom de produit</label><input type="text" name="nom" autocomplete="off"><br>
<label for="">Prix</label><input type="text" name="prix" autocomplete="off"><br>
<label for="">Quantité</label><input type="number" name="quantite" min="1" autocomplete="off"><br>
<button type="submit" name="enregistrer">Enregistrer</button>
</form>
</body>
</html>
<!-- créer connexion pour transmettre les information au BD -->
<?php
require_once("connect.php");
if(isset($_POST['enregistrer']))
{
$nom=$_POST['nom'];
$prix=$_POST['prix'];
$quantite=$_POST['quantite'];
if(!empty($nom) AND !empty($prix) AND !empty($quantite)){
if(strlen($nom)<5){
echo "cinque caracteres minimum";
} else {
$req=$con->prepare("INSERT INTO produit(nomproduit, prix, quantite) VALUES (?,?,?)");
$req->execute(array($nom, $prix, $quantite));
if($req){ header("location: afficher.php");
}
}
}else {echo "rempli tt champs";}
}
?> |
connect.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| <?php
try{
$con=new PDO("mysql:localhost=localhost; dbname=php5", "root","");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo "défaut de connexion".$e->getMessage();
}
//pour tester la connexion ecrir la ligne suivante;
// if($con){ echo "connecxion réussite" ;}
?> |
afficher.php
Code:
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
| <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>afficher</title>
<h4>afficher nos produits</h4>
</head>
<style>
table{
border: 2px solid blue;
}
td, th{ border: 1px solid red;
border-collapse: collapse;
}
</style>
<body>
<table>
<th>ID</th>
<th>nom produit</th>
<th>prix</th>
<th>Quantite</th>
<th>action</th>
<?php
require_once("connect.php");
$req=$con->query("SELECT * FROM produit");
while($aff=$req->fetch()){?>
<tr>
<td><?php echo $aff['id'];?></td>
<td><?php echo $aff['nomproduit'];?></td>
<td><?php echo $aff['prix'];?></td>
<td><?php echo $aff['quantite'];?></td>
<td>
<a href="modifier.php?id=<?php echo $aff['id']?>">Modifier</a>
<a href="supprimer.php?id=<?php echo $aff['id']?>">supprimer</a>
</td>
</tr>
<?php }?>
</table>
<a href="calcule.php"><h2>calcule</h2></a>
<a href="calculeetat1.php"><h2>calculeetat1</h2></a>
</body>
</html> |
modifier.php
Code:
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
| <?php
require_once("connect.php");
if(isset($_GET['id'])){
$id=$_GET['id'];
$req=$con->prepare("SELECT * FROM produit where id= ?");
$req->execute([$id]);
$mod=$req->fetch();
}?>
<h2>modification produit</h2>
<form action="" method="post">
<label for="">Nom de produit</label><input value="<?php echo $mod['nomproduit'];?>" type="text" name="nom" autocomplete="off"><br>
<label for="">Prix</label><input value="<?php echo $mod['prix'];?>" type="text" name="prix" autocomplete="off"><br>
<label for="">Quantité</label><input value="<?php echo $mod['quantite'];?>" type="number" name="quantite" min="1" autocomplete="off"><br>
<button type="submit" name="modifier">Enregistrer Modification</button>
</form>
<?php
if(isset($_POST['modifier']))
{
$nom=$_POST['nom'];
$prix=$_POST['prix'];
$quantite=$_POST['quantite'];
$req=$con->prepare("UPDATE produit SET nomproduit=?, prix=?, quantite=? where id=$id");
$req->execute(array($nom, $prix, $quantite));
if($req){
header("location:afficher.php");
}
}
?> |
supprimer.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?php
require_once("connect.php");
if(isset($_GET["id"])){
$id=$_GET["id"];
$req=$con->prepare("DELETE FROM produit where id= ?");
$req->execute(array($id));
if($req){
header("location: afficher.php");
}
}
?> |
calcule.php
Code:
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
| <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>calcule</title>
<h4>afficher nos produits</h4>
</head>
<style>
table{
border: 2px solid blue;
}
td, th{ border: 1px solid red;
border-collapse: collapse;
}
</style>
<body>
<table>
<th>ID</th>
<th>nom produit</th>
<th>prix</th>
<th>Quantite</th>
<th>action</th>
<th>etat</th>
<?php
require_once("connect.php");
$req=$con->query("SELECT * FROM produit");
while($aff=$req->fetch()){?>
<tr>
<td><?php echo $aff['id'];?></td>
<td><?php echo $aff['nomproduit'];?></td>
<td><?php echo $aff['prix'];?></td>
<td><?php echo $aff['quantite'];?></td>
<td>
<a href="modifier.php?id=<?php echo $aff['id']?>">Modifier</a>
<a href="supprimer.php?id=<?php echo $aff['id']?>">supprimer</a>
</td>
<td><?php
if($aff['quantite']<5)
{echo '1';} else {echo '0';}?> </td>
</tr>
<?php }?>
</table>
</body>
</html> |
calculeetat1.php
Code:
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
| <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>calcule</title>
<h4>afficher nos produits</h4>
</head>
<style>
table{
border: 2px solid blue;
}
td, th{ border: 1px solid red;
border-collapse: collapse;
}
</style>
<body>
<table>
<th>ID</th>
<th>nom produit</th>
<th>prix</th>
<th>Quantite</th>
<th>action</th>
<th>etat</th>
<?php
require_once("connect.php");
$req=$con->query("SELECT * FROM produit");
while($aff=$req->fetch()){?>
<tr>
<td><?php echo $aff['id'];?></td>
<td><?php echo $aff['nomproduit'];?></td>
<td><?php echo $aff['prix'];?></td>
<td><?php echo $aff['quantite'];?></td>
<td>
<a href="modifier.php?id=<?php echo $aff['id']?>">Modifier</a>
<a href="supprimer.php?id=<?php echo $aff['id']?>">supprimer</a>
</td>
<td><?php
if($aff['quantite']<5)
{echo '1';} else {echo '0';}?> </td>
</tr>
<?php }?>
</table>
<?php
$ss= var_dump($aff);
echo $ss;
?>
</body>
</html> |
merci, agréable journée