There needs to be some conditional that queries the stock quantity and changes the badge accordingly.
Though it is best to keep that kind of logic out of your HTML and do all the data processing before that.
On an unrelated side note, it’s not a good idea to have Delete actions on a GET request, use POST instead.
if($row->quantite < 1){ // Quantity is less than 1.
echo '<td > <span class="badge bg-fail">No Stock</span> </td>' ;
}
else{
echo '<td > <span class="badge bg-success">en Stock</span> </td>' ;
}
Change the class and wording to suit.
Though this is not how I would do this. I would probably create a class for products with methods to handle setting up all this and do most of the logic processing outside (before) the HTML.
The logic may be the same, but instead of directly outputting to HTML I would store the result in an object property to be output later at the right place.
For more complex logical processing it can get very messy and you should keep that away from the HTML or it will be confusing and hard to manage.
I am unable to do a test because when I try to register a new product and I indicate 0 in quantity it does not register it I am forced to indicate a quantity greater than 0
Do you have an idea ?
here is my inde.php code and my product code.php
index.php
<?php
// faire appel a la base de donnees
require_once 'db.php';
// ajouter un produit depuis le formulaire
if(isset($_POST['ajouter']) && !empty($_POST['code_article'])
&& !empty($_POST['nom_article'])
&& !empty($_POST['quantite'])
)
{
$code_article = $_POST['code_article'];
$nom_article = $_POST['nom_article'];
$quantite = $_POST['quantite'];
// pour l'image ya bouceaup de chose a jouter
// je laisse le code dans un lien dans la descreption
$images=$_FILES['profile']['name'];
$tmp_dir=$_FILES['profile']['tmp_name'];
$imageSize=$_FILES['profile']['size'];
// creer un dossier nommer le uplods
// pour stocker nos images
$upload_dir='uploads/';
$imgExt=strtolower(pathinfo($images,PATHINFO_EXTENSION));
$valid_extensions=array('jpeg', 'jpg', 'png', 'gif', 'pdf');
$picProfile=rand(1000, 1000000).".".$imgExt;
move_uploaded_file($tmp_dir, $upload_dir.$picProfile);
$sql ="INSERT INTO produit(code_article, nom_article, image_produit, quantite)
VALUES (:code_article, :nom_article, :pic, :quantite)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':code_article', $code_article);
$stmt->bindParam(':nom_article', $nom_article);
$stmt->bindParam(':pic', $picProfile);
$stmt->bindParam(':quantite', $quantite);
$stmt->execute();
header('Location:index.php');
}
$stmt = $pdo->query('SELECT * FROM produit');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gestion de Stock</title>
</head>
<body>
<?php require_once 'navbar.php'; ?>
<div class="container">
<div class="row">
<div class="col-2 mt-3">
<form action="index.php" class="form-group mt-3" method="POST" enctype="multipart/form-data" >
<label for="">Image du Produit :</label>
<input type="file" class="form-control mt-3" name="profile" accept="*/image">
<label for="">Code Article :</label>
<input type="text" class="form-control mt-3" name="code_article" required>
<label for="">Designation du Produit :</label>
<input type="text" class="form-control mt-3" name="nom_article" required>
<label for="">Quantite :</label>
<input type="text" class="form-control mt-3" name="quantite" required>
<button type="submit" class="btn btn-primary mt-3" name="ajouter">Enregistrer</button>
</form>
</div>
<div class="col-10 mt-3">
<table class="table table-striped display" id="example">
<thead>
<th>Image</th>
<th>Code Article</th>
<th>Designation</th>
<th>Quantite</th>
</thead>
<tbody>
<?php
while ( $row = $stmt->fetch())
{
?>
<tr>
<td><img src="./uploads/<?php echo $row-> image_produit; ?>" alt=" " class="image_product "></td>
<td><?php echo $row-> code_article; ?> </td>
<td><?php echo $row-> nom_article; ?> </td>
<td><?php echo $row-> quantite; ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#example').DataTable( {
"scrollY": "500px",
"scrollCollapse": true,
"paging": false
});
} );
</script>
</body>
</html>
Removing the line will allow a 0 value to be passed. It will also allow an empty value too, which you may not want. Though you could set it to default to 0 in the absence of any value.
You need to trim user submitted data, mainly so that you can detect if a value is all white-space characters, then validate all input data separately. Form data are string datatypes, regardless of what value they are. You should test if an input is or is not exactly an empty string, using === ‘’ or !== ‘’
After a redirect, you need to have an exit/die statement to stop php code execution. Currently, the rest of your code still runs while the browser is requesting the URL.