salut tout le monde ! Je suis nouveau sur ce forum, c'est donc ma première publication.
Voici mon problème : je suis en pleine création d'un site web(E-commerce) et j'ai créé une page d'administration pour ajouter et supprimer un article, ajouter et modifier une catégorie.
Jusqu'à présent tout marche parfaitement. Mais je souhaiterais qu'un article ait plusieurs images. or dans mon cas... je n'arrives qu'à insérer qu'une image via mon formulaire depuis mon espace administration. Et je voudrais que lorsqu'on clique sur l'article, on arrive à voir automatiquement les autres images du produit.
Je ne sais pas du tout comment m'y prendre sur ce point.
Y'a t-il une manière de le faire en php ? Merci de bien vouloir m'aider
voici le code de ma page d'administration(admin.php)
voici le code ma page boutique également pour l'affichage demes articles
Modifié par kouamkoff (18 Jun 2019 - 17:31)
Voici mon problème : je suis en pleine création d'un site web(E-commerce) et j'ai créé une page d'administration pour ajouter et supprimer un article, ajouter et modifier une catégorie.
Jusqu'à présent tout marche parfaitement. Mais je souhaiterais qu'un article ait plusieurs images. or dans mon cas... je n'arrives qu'à insérer qu'une image via mon formulaire depuis mon espace administration. Et je voudrais que lorsqu'on clique sur l'article, on arrive à voir automatiquement les autres images du produit.
Je ne sais pas du tout comment m'y prendre sur ce point.
Y'a t-il une manière de le faire en php ? Merci de bien vouloir m'aider
voici le code de ma page d'administration(admin.php)
<?php
session_start();
?>
<br/>
<!DOCTYPE html>
<html>
<head>
<title>Espace administration</title>
<meta charset='utf-8'>
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="../style.css">
</head>
<style>
body{margin-left: 15px;background-color: rgba(155,226,167, 0.7);}
</style>
<body>
<div style="text-align: center">
<h1><span style="font-family: cursive;background-color: #fff; border-radius: 5px;padding: 5px 5px">Bienvenue  <?php echo $_SESSION['username']; ?></span><a href="index.php" style="margin-left: 100px"><button class="btn btn-warning">Deconnexion</button></a></h1></div>
<br/>
<div class="container-fluid">
<a href="?action=add"><button class="btn btn-primary">Ajouter un véhicule</button></a> 
<a href="?action=modifyanddelete"><button class="btn btn-primary">Modifier / Supprimer un véhicule</button></a> 
<a href="?action=add_category"><button class="btn btn-primary">Ajouter une marque</button></a> 
<a href="?action=modifyanddelete_category"><button class="btn btn-primary">Modifier / Supprimer une marque</button></a> 
<a href="?action=add_images"><button class="btn btn-primary">Ajout_photos_supp</button></a> 
</div>
<br><br><br>
<?php
try{
$bdd = new PDO("mysql:host=127.0.0.1;dbname=site e-commerce", 'root', '');
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e){
echo $e->getMessage();
die("Une erreur s'est produite, veuillez rééssayer....");
}
if(isset($_SESSION['username'])){
if(isset($_GET['action'])){
if($_GET['action']=='add'){//action pour ajouter
if(isset($_POST['submit'])){
$title = htmlspecialchars(trim($_POST['title']));
$description = htmlspecialchars(trim($_POST['description']));
$price = htmlspecialchars(trim($_POST['price']));
$statut = htmlspecialchars(trim($_POST['statut']));
$discussion = htmlspecialchars(trim($_POST['discussion']));
$img = $_FILES['img']['name'];
$img_tmp = $_FILES['img']['tmp_name'];
$category = $_POST['name'];
if(!empty($img_tmp)){
$image = explode('.',$img);
$image_ext = end($image);
print_r($image_ext);
if(in_array(strtolower($image_ext),array('png', 'jpg', 'jpeg', 'gif', 'bmp'))===false){
$erreur='<p style="color:red; font-size:15px; font-family:century gothic; font-weight:bolder; margin-left: 15px">Veuillez entrer une image ayant pour extension : png, jpg, jpeg, bmp, gif</p>';
}else{
$image_size = getimagesize($img_tmp);
if($image_size['mime']=='image/jpeg'){
$image_src = imagecreatefromjpeg($img_tmp);
}elseif($image_size['mime']=='image/png'){
$image_src = imagecreatefrompng($img_tmp);
}elseif($image_size['mime']=='image/jpeg'){
$image_src = imagecreatefromjpeg($img_tmp);
}elseif($image_size['mime']=='image/gif'){
$image_src = imagecreatefromgif($img_tmp);
}else{
$image_src = false;
$erreur = '<p style="color:red; font-size:15px; font-family:century gothic; font-weight:bolder; margin-left: 15px">Veuillez entrer une image valide !</p>';
}
if($image_src!=false){
$image_width = 370;
if($image_size[0]==$image_width){
$image_finale = $image_src;
}else{
$new_width[0] = $image_width;
$new_height[3] = 220;
$image_finale = imagecreatetruecolor($new_width[0],$new_height[3]);
imagecopyresampled($image_finale, $image_src, 0, 0, 0, 0, $new_width[0], $new_height[3],$image_size[0],$image_size[1]);
}
imagejpeg($image_finale, 'imgs/' .$title.'.jpg');
}
}
}else{
$erreur= '<p style="color:red; font-size:22px; font-family:century gothic; font-weight:bolder; margin-left: 20px">Image obligatoire ! Veuillez insérer une image </p>';
}
if(!empty($title) AND !empty($description) AND !empty($price)AND !empty($category) AND !empty($statut) AND !empty($discussion)){
try{
$bdd = new PDO("mysql:host=127.0.0.1;dbname=site e-commerce", 'root', '');
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e){
echo $e->getMessage();
}
$insert = $bdd->prepare("INSERT INTO products(title, description, price, discussion,category,statut) VALUES(?, ?, ?, ?, ?, ?)");
$insert->execute(array($title, $description, $price, $discussion, $category, $statut));
die('<br><p style="color:green; font-size:28px; font-family:century gothic; font-weight:bolder; margin-left: 15px">Bien éffectué ! Produit ajouté avec succès...');
}else{
$erreur='<p style="color:red;font-size:20px; font-family:century gothic; font-weight:bolder; margin-left: 13px">Veuillez remplir tous les champs !</p>';
}
}
?>
<div class="container-fluid">
<div class="container">
<div class="jumbotron">
<div class="row"><?php if(isset($erreur)){ echo $erreur; } ?></div>
<form action="" method="POST" enctype="multipart/form-data" onsubmit="return confirm('Confirmez-vous l\'ajout de ce véhicule au site ?');">
<h3 style="font-family: century gothic">Série du véhicule</h3><input type='text' name='title' id='title' style="font-family: tahoma; width: 200px; border: 1px solid black; border-radius: 4px;font-weight: bold; color:#777; height: 22px; text-align: center; font-size: 17px" />
<h3 style="font-family: century gothic">Caractéristiques</h3><textarea name='description' id='description' style="font-weight: bolder; width: 400px;height: 200px;color:#777;font-size: 15px"></textarea>
<h3 style="font-family: century gothic">Prix</h3><input type='number_format' name='price' id='price' style="font-family: tahoma; width: 200px; border: 1px solid black; border-radius: 4px; height: 22px; font-weight: bold; color:#777;text-align: center; font-size: 17px" />
<span style="font-family: century gothic; font-weight: bold;">Discussion</span>
    <select name="discussion" style="font-family: tahoma; width: 200px; border: 1px solid black; font-weight: bolder;border-radius: 3px; height: 22px; text-align: center; font-size: 15px; color:#777">
<optgroup>
<option value="Négociable">Négociable</option>
<option value="Non négociable">Non négociable</option>
<option value=""></option>
</optgroup>
</select>
<h3 style="font-family: century gothic">Image</h3><input type='file' name='img' id='img[]' multiple=""/>
<br />
<h3 style="font-family: century gothic">Marques : </h3>
<input type="text" name="name" style="font-family: tahoma; width: 200px; border: 1px solid black; font-weight: bolder;border-radius: 3px; height: 22px; text-align: center; font-size: 15px; color:#777" ><br /><br><br>
<span style="font-family: century gothic; font-weight: bold;">Statut</span>
    <select name="statut" style="font-family: tahoma; width: 200px; border: 1px solid black; font-weight: bolder;border-radius: 3px; height: 22px; text-align: center; font-size: 15px; color:#777">
<optgroup>
<option value="Disponible">Disponible</option>
<option value="Vendu">Vendu</option>
</optgroup>
</select>
<br><br>
<input type="submit" name="submit" value="Ajouter ce véhicule" />
</form>
</div>
</div>
</div>
<?php
}elseif($_GET['action']=='modifyanddelete'){
$modifydel = $bdd->prepare("SELECT * FROM products");
$modifydel->execute();
while($s=$modifydel->fetch(PDO::FETCH_OBJ)){
echo $s->title.'     ';
?>
<a href="?action=modify&id=<?php echo $s->id; ?>"><button class="btn btn-info">Modifier</button></a>
<a href="?action=delete&id=<?php echo $s->id; ?>"><button class="btn btn-danger">Supprimer</button></a>
<br/><br/>
<?php
}
}elseif ($_GET['action']=='modify'){
$id= $_GET['id'];
$modif = $bdd->prepare("SELECT * FROM products WHERE id= $id");
$modif->execute();
$data = $modif->fetch(PDO::FETCH_OBJ);
?>
<div class="container-fluid">
<div class="container">
<div class="jumbotron">
<form action="" method="POST">
<h3 style="font-family: century gothic">Série du véhicule</h3><input value="<?php echo $data->title; ?>" type="text" name="title" id="title" style="font-family: tahoma; width: 200px; border: 1px solid black; font-weight: bold;border-radius: 3px; height: 22px; text-align: center; font-size: 15px; color:#777">
<h3 style="font-family: century gothic">Caractéristiques</h3><textarea name='description' id='description' style="font-weight: bolder; width: 400px;height: 150px;color:#777;font-size: 15px">
<?php echo $data->description; ?></textarea>
<h3 style="font-family: century gothic">Prix</h3><input value="<?php echo $data->price; ?>" type='text' name="price" id='price' style="font-family: tahoma; width: 200px; border: 1px solid black; font-weight: bold;border-radius: 3px; height: 22px; text-align: center; font-size: 15px; color:#777" />
<span style="font-family: century gothic; font-weight: bold;">Discussion</span>
    <select name="discussion" style="font-family: tahoma; width: 200px; border: 1px solid black; font-weight: bolder;border-radius: 3px; height: 22px; text-align: center; font-size: 15px; color:#777">
<optgroup>
<option value="Négociable">Négociable</option>
<option value="Non négociable">Non négociable</option>
<option value=""></option>
</optgroup>
</select><br><br>
<span style="font-family: century gothic; font-weight: bold;">Statut</span>
    <select name="statut" style="font-family: tahoma; width: 200px; border: 1px solid black; font-weight: bolder;border-radius: 3px; height: 22px; text-align: center; font-size: 15px; color:#777">
<optgroup>
<option value="Disponible">Disponible</option>
<option value="Vendu">Vendu</option>
</optgroup>
</select><br><br>
<input type="submit" name="submit" value="Mettre à Jour les informations" />
</form>
</div>
</div>
</div>
<?php
if(isset($_POST['submit'])){
$title = htmlspecialchars(trim($_POST['title']));
$description = htmlspecialchars(trim($_POST['description']));
$price = htmlspecialchars(trim($_POST['price']));
$discussion = htmlspecialchars(trim($_POST['discussion']));
$statut= htmlspecialchars(trim($_POST['statut']));
$update = $bdd->prepare("UPDATE products SET title='$title', description='$description', price='$price', discussion='$discussion', statut='$statut' WHERE id= $id");
$update->execute(array($title, $description, $price, $discussion, $statut));
header('Location: admin.php?action=modifyanddelete');
}
}elseif ($_GET['action']=='delete'){
$id = $_GET['id'];
$delete = $bdd->prepare("DELETE FROM products WHERE id = $id");
$delete->execute();
header('Location: admin.php?action=modifyanddelete');
}else if($_GET['action']=='add_category'){
if(isset($_POST['submit'])){
$namz = htmlspecialchars(trim($_POST['name']));
if(!empty($namz)){
$insert = $bdd->prepare("INSERT INTO category(name)VALUES(?)");
$insert->execute(array($namz));
$erreur = '<p style="color:green; font-size:15px; font-family:century gothic; font-weight:bolder; margin-left: 10px">Marque bien ajoutée... <br><br><br>';
}else{
$erreur = '<p style="color:red; font-size:15px; font-family:century gothic; font-weight:bolder; margin-left: 10px">Aucune marque ajoutée ! <br><br>';
}
}
?>
<?php
?>
<div style="margin-left: 40">
<?php if(isset($erreur)){ echo $erreur; } ?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="name" style="font-family: tahoma; width: 200px; border: 1px solid black; border-radius: 3px; height: 22px; text-align: center; font-size: 15px;font-weight: bold; color:#777" placeholder="Nom de la marque"/>
<input type="submit" name="submit" value="Valider">
</form>
</div>
<?php
}else if($_GET['action']=='modifyanddelete_category'){//modifier et supprimer une catégorie
$modifydel1 = $bdd->prepare("SELECT * FROM category");
$modifydel1->execute();
while($s=$modifydel1->fetch(PDO::FETCH_OBJ)){
echo $s->name. '   ';
?>
<a href="?action=modify_category&id=<?php echo $s->id; ?>"><button class="btn btn-info">Modifier</button></a>
<a href="?action=delete_category&id=<?php echo $s->id; ?>"><button class="btn btn-danger">Supprimer</button></a><br/><br/>
<?php
}
}else if($_GET['action']=='modify_category'){//modifier une catégorie
$id= $_GET['id'];
$modif = $bdd->prepare("SELECT * FROM category WHERE id= $id");
$modif->execute();
$data = $modif->fetch(PDO::FETCH_OBJ);
?>
<div style="margin-left: 40px">
<form action="" method="POST">
<input value="<?php echo $data->name; ?>" type='text' name="name" id="name" placeholder="Nom de la marque" style="font-family: tahoma; width: 200px; border: 1px solid black; border-radius: 3px; height: 22px; text-align: center; font-size: 15px; font-weight: bold;color:#777" >  
<input type="submit" name="submit" value="Modifier" />
</form>
</div>
<?php
if(isset($_POST['submit'])){
$title = htmlspecialchars(trim($_POST['name']));
$update = $bdd->prepare("UPDATE category SET name='$title' WHERE $id= id");
$update->execute(array($namz));
header('Location: admin.php?action=modifyanddelete_category');
}
}else if($_GET['action']=='delete_category'){
$id = $_GET['id'];
$delete_category = $bdd->prepare("DELETE FROM category where id = $id");
$delete_category->execute();
header('Location: admin.php?action=modifyanddelete_category');
}else if($_GET['action']=='add_images'){
if(isset($_POST['ajoutImages'])){
$img2 = $_FILES['images_supp']['name'];
$img_tmp2 = $_FILES['images_supp']['tmp_name'];
if(!empty($img_tmp2)){
$image2 = explode('.',$img2);
$image_ext2 = end($image2);
print_r($image_ext2);
if(in_array(strtolower($image_ext2),array('png', 'jpg', 'jpeg', 'gif', 'bmp'))===false){
$erreur='<p style="color:red; font-size:15px; font-family:century gothic; font-weight:bolder; margin-left: 15px">Veuillez entrer une image ayant pour extension : png, jpg, jpeg, bmp, gif</p>';
}else{
$image_size2 = getimagesize($img_tmp2);
if($image_size2['mime']=='image/jpeg'){
$image_src2 = imagecreatefromjpeg($img_tmp2);
}elseif($image_size2['mime']=='image/png'){
$image_src2 = imagecreatefrompng($img_tmp2);
}elseif($image_size2['mime']=='image/jpeg'){
$image_src2 = imagecreatefromjpeg($img_tmp2);
}elseif($image_size2['mime']=='image/gif'){
$image_src2 = imagecreatefromgif($img_tmp2);
}else{
$image_src2 = false;
$erreur = '<p style="color:red; font-size:15px; font-family:century gothic; font-weight:bolder; margin-left: 15px">Veuillez entrer une image valide !</p>';
}
if($image_src2!=false){
$image_width2 = 370;
if($image_size2[0]==$image_width2){
$image_finale2 = $image_src2;
}else{
$new_width2[0] = $image_width2;
$new_height2[3] = 220;
$image_finale2 = imagecreatetruecolor($new_width2[0],$new_height2[3]);
imagecopyresampled($image_finale2, $image_src2, 0, 0, 0, 0, $new_width2[0], $new_height2[3],$image_size2[0],$image_size2[1]);
}
imagejpeg($image_finale2, 'galleries/.jpg');
}
}
}else{
$erreur= '<p style="color:red; font-size:22px; font-family:century gothic; font-weight:bolder; margin-left: 20px">Image obligatoire ! Veuillez insérer une image </p>';
}
}
?>
<div class="container">
<div class="jumbotron">
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="images_supp" multiple=""><br><br>
<input type="submit" name="ajoutImages" value="Valider">
</form>
</div>
</div>
<?php
}
}else{
/* on inscrit rien tout simplement si la variable $_GET existe
<?php $select = $bdd->query("SELECT * FROM category");
while($s = $select->fetch(PDO::FETCH_OBJ)){
?>
<option><?php echo $s->name; ?></option>
<?php
}
?>*/
}
}else{
header('Location: ../index.php');
}
?>
</body>
</html>
voici le code ma page boutique également pour l'affichage demes articles
<?php
require_once ('header.php');
echo '<br>';
if(isset($_GET['show'])){
$product = $_GET['show'];
$select = $bdd->prepare("SELECT * FROM products WHERE title ='$product'");
$select->execute();
$s = $select->fetch(PDO::FETCH_OBJ);
$description = $s->description;
$description_finale = wordwrap($description, 40,'<br />', false);
$statut = $s->statut;
?>
<!DOCTYPE html>
<html>
<head>
<title>Boutique E-Cars</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv ="X-UA-compatible" content="ie-edge">
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="style.css" />
<style>.marques:hover{color: orange;background-color: black; border-radius: 10px;text-align: center;}
</style>
</head>
<body>
<div class="container-fluid" style="background: #f0ece3" id="sec">
<div>
<div style="margin-left: 10px;position: relative" id="boutiqua">
<figcaption style="margin-left: 7%;margin-top: 3%; font-family: century gothic; font-size: 20px; font-weight: bolder"><?php echo $s->title; ?></figcaption><br>
   <img src="admin/imgs/<?php echo $s->title; ?>.jpg" />     
<fieldset style="position: absolute;margin-top: 0%;margin-left: -1%;display: inline; width: 380px; padding: 20px 15px; font-family: century gothic; border: 0px solid grey; border-radius: 5px;background-color: white"><?php echo $description_finale; ?><br><br>
<?php echo 'Prix : '.$s->price.' Francs cfa   '; ?><strong><?php echo $s->discussion; ?></strong><br><br>
<?php echo 'Statut : <strong>'.$s->statut; ?></strong>      <a href="panier.php?action=ajout&l=<?php echo $s->title; ?>&q=1&p=<?php echo $s->price; ?>"><button type="button" class="btn btn-danger" style="color: #fff;">ACHETER</button></a><br>
<div>
</div>
<div style="display:none">Lire les <a href="conditions_generales_utilisation"><span style="font-family: consolas; color : black; font-weight: bolder; background-color: black; color: orange">Conditions générales</span></a></div><br><br>
</fieldset><br><br><br><br><br>
</div>
</div>
<div id="offr">
<fieldset style="margin: 0 auto;font-size: 24px; text-align: center;position: relative;width: 800px;padding: 80px 10px; font-family: calibri; border: 0px solid grey; border-radius: 5px;background-color: navy; color: #fff">Obtenir une voiture en fonction de mon budget ? <a href="budget.php" style="background-color: orange; padding: 3px 8px; font-weight: bold;font-size: 17px;font-family: consolas;color: #fff;border-radius: 5px;">Plus de détails ici</a>
</fieldset>
<div>
</div>
</div>
<?php
}else{
if(isset($_GET['category'])){
$category = $_GET['category'];
$select = $bdd->prepare("SELECT * FROM products WHERE category ='$category'");
$select->execute();
while($s = $select->fetch(PDO::FETCH_OBJ)){
$lenght = 15;
$description = $s->description;
$new_description = substr($description,0,$lenght)." ...";
$description_finale = wordwrap($new_description,15,'<br /><br />', true);
?>
<div id="boutiq">
<h4 style="font-family: century gothic; margin-left: 80px"><?php echo $s->title; ?></h4>
<a data-ligthbox="mygallery" href="?show=<?php echo $s->title; ?>"><img src="admin/imgs/<?php echo $s->title; ?>.jpg" /></a>
<a style="text-decoration: none" href="?show=<?php echo $s->title; ?>"><h4 style="color: black; font-family: calibri; margin-left: 30px"><?php echo $new_description; ?><span style="color: blue">voir plus</span></h4></a>
<span style="color: black; margin-left: 30px;font-family: century gothic;">Prix : <?php echo $s->price. ' Francs cfa'; ?></span>
<a style="margin-left: 30px" href="panier.php?action=ajout&l=<?php echo $s->title; ?>&q=1&p=<?php echo $s->price; ?>"><span style="color: #fff; background-color: #af135c; font-size: 13px;border: 2px solid #af135c;font-family: helvetica; border-radius: 4px; padding: 4px 6px">ACHETER</a></span>
<br><br>
</div>
<?php
}
}else{
$select=$bdd->query("SELECT * FROM category");
while($s = $select->fetch(PDO::FETCH_OBJ)){
?>
<div class="categ" style="margin-left: 10px; display: inline-block;">
<a style="text-decoration: none;color: firebrick; font-family: century gothic; font-weight: bolder;" href="?category=<?php echo $s->name; ?>"><h3 class="marques"><?php echo $s->name; ?></h3></a>
<section>
<div>
</div>
</div>
</div>
<?php } echo '<br><br><br><br><br><br><br>';
} } ?><br><br><br><br><br><br>
<?php echo '<br><br><br><br><br><br><br><br><br><br><br>'; ?>
<?php require_once('footer.php'); ?>
</body>
</html>
Modifié par kouamkoff (18 Jun 2019 - 17:31)