Skip to content

Commit a010e2d

Browse files
committed
Listing de produits sur page d'accueil, séparation de la config de la bd et du code, etc.
1 parent 12ead84 commit a010e2d

8 files changed

+115
-53
lines changed

.htaccess

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Files "config.ini">
2+
Order Allow,Deny
3+
Deny from all
4+
</Files>

LogiKek_Creation.sql

+14-3
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,24 @@ ADD CONSTRAINT ProduitsCategories_Categories_FK
6565
FOREIGN KEY (idCategorie) REFERENCES Categories (idCategorie);
6666

6767

68-
CREATE OR REPLACE VIEW ListeCategories AS
69-
SELECT nom
68+
CREATE OR REPLACE VIEW FetchAllCategories AS
69+
SELECT codeCategorie, nom
7070
FROM Categories
7171
ORDER BY nom ASC;
7272

7373

7474
CREATE OR REPLACE VIEW Catalog AS
7575
SELECT nom, prix, quantite
7676
FROM Produits
77-
ORDER BY nom DESC;
77+
ORDER BY nom DESC;
78+
79+
CREATE OR REPLACE VIEW FetchAllProduits AS
80+
SELECT p.nom, p.description, p.prix, p.codeProduit, p.quantite, p.quantiteMin, c.codeCategorie, c.nom AS categorie
81+
FROM Produits p
82+
INNER JOIN ProduitsCategories pc ON pc.idProduit = p.idProduit
83+
INNER JOIN Categories c ON c.idCategorie = pc.idCategorie
84+
GROUP BY p.idProduit
85+
ORDER BY p.nom, categorie ASC;
86+
87+
88+
SELECT * FROM FetchAllProduits;

config.ini

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[database]
2+
username = root
3+
password =
4+
dbname = LogiKek

css/index.css

+4
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ p {
3333
-webkit-margin-start: 0px;
3434
-webkit-margin-end: 0px;
3535
}
36+
37+
.produit{
38+
39+
}

index.php

+27-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<?php require_once("php/mysqli.php"); ?>
1+
<?php
2+
require_once("php/mysqli.php");
3+
require_once("php/Classes/Categorie.php");
4+
require_once("php/Classes/Produit.php");
5+
?>
26

37
<!DOCTYPE html>
48
<html lang="en">
@@ -39,11 +43,10 @@
3943
<input type="text" class="form-control" placeholder="Search for...">
4044
<div class="input-group-btn">
4145
<select class="btn dropdown-toggle">
46+
<option selected>Tout les produits</option>
4247
<?php
43-
global $mysqli;
44-
$requeteCategories = "SELECT * FROM ListeCategories";
45-
foreach($mysqli->query($requeteCategories) as $value): ?>
46-
<option><?php echo $value['nom']; ?></option>
48+
foreach(Categorie::fetchAll() as $value): ?>
49+
<option><?php echo $value->nom; ?></option>
4750
<?php endforeach; ?>
4851
</select>
4952
</div>
@@ -85,10 +88,8 @@
8588
<div class="col-md-2">
8689
<ul>
8790
<?php
88-
global $mysqli;
89-
$requeteCategories = "SELECT * FROM ListeCategories";
90-
foreach($mysqli->query($requeteCategories) as $value): ?>
91-
<li><a href="#"><?php echo $value['nom'] ?></a></li>
91+
foreach(Categorie::fetchAll() as $value): ?>
92+
<li><a href="#"><?php echo $value->nom ?></a></li>
9293
<?php endforeach; ?>
9394

9495

@@ -101,21 +102,23 @@
101102
</div>
102103
<!-- Début des produits -->
103104
<div class="row">
104-
<div class="col-md-4">
105-
<a href="#" class="thumbnail">
106-
<img src="..." alt="...">
107-
</a>
108-
</div>
109-
<div class="col-md-4">
110-
<a href="#" class="thumbnail">
111-
<img src="..." alt="...">
112-
</a>
113-
</div>
114-
<div class="col-md-4">
115-
<a href="#" class="thumbnail">
116-
<img src="..." alt="...">
117-
</a>
118-
</div>
105+
<?php foreach(Produit::fetchAll() as $value): ?>
106+
<div class="col-md-4 panel panel-default produit">
107+
<h4><?php echo $value->nom ?></h4>
108+
<a href="#" class="thumbnail">
109+
<img src="..." alt="<?php echo $value->nom ?>">
110+
</a>
111+
<?php foreach($value->categories as $categorie): ?>
112+
<span class="label label-info"><?php echo $categorie ?></span>
113+
<?php endforeach; ?>
114+
<h4>
115+
<?php echo $value->prix ?>$
116+
<a href="#">
117+
<i class="fa fa-shopping-cart"></i>
118+
</a>
119+
</h4>
120+
</div>
121+
<?php endforeach; ?>
119122
</div> <!-- Fin des produits -->
120123
</div> <!-- Fin section central col-md-9 -->
121124
<div class="col-md-1"> <!-- Début Section de droite central -->

php/Classes/Categorie.php

+23-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,30 @@
22

33
class Categorie
44
{
5-
private $nom;
6-
private $codeCategorie;
7-
}
5+
public $nom;
6+
public $codeCategorie;
87

9-
public function __construct($tableau)
10-
{
11-
$this->nom = $tableau['nom'];
12-
$this->codeCategorie = $tableau['code'];
8+
9+
public function __construct($tableau)
10+
{
11+
$this->nom = $tableau['nom'];
12+
$this->codeCategorie = $tableau['codeCategorie'];
13+
}
14+
15+
public static function fetchAll()
16+
{
17+
global $mysqli;
18+
$categories = array();
19+
20+
$requeteCategories = 'SELECT * FROM FetchAllCategories';
21+
foreach($mysqli->query($requeteCategories) as $value)
22+
{
23+
$tmp = new Categorie($value);
24+
$categories[] = $tmp;
25+
}
26+
27+
return $categories;
28+
}
1329
}
1430

1531
?>

php/Classes/Produit.php

+36-17
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,43 @@
22

33
class Produit
44
{
5-
private $nom;
6-
private $codeProduit;
7-
private $prix;
8-
private $description;
9-
private $quantite;
10-
private $quantiteMin;
11-
private categories = array();
12-
}
5+
public $nom;
6+
public $codeProduit;
7+
public $prix;
8+
public $description;
9+
public $quantite;
10+
public $quantiteMin;
11+
public $categories = array();
12+
13+
14+
public function __construct($tableau)
15+
{
16+
$this->nom = $tableau['nom'];
17+
$this->codeProduit = $tableau['codeProduit'];
18+
$this->prix = number_format($tableau['prix'], 2);
19+
$this->description = $tableau['description'];
20+
$this->quantite = $tableau['quantite'];
21+
$this->quantiteMin = $tableau['quantiteMin'];
22+
$this->categories = explode(",", $tableau['categories']);
23+
}
24+
25+
public static function fetchAll()
26+
{
27+
global $mysqli;
28+
$produits = array();
29+
30+
$requeteProduits = 'SELECT * FROM FetchAllProduits';
31+
foreach($mysqli->query($requeteProduits) as $value)
32+
{
33+
$tmp = new Produit($value);
34+
$produits[] = $tmp;
35+
}
36+
37+
return $produits;
38+
}
39+
40+
1341

14-
public function __construct($tableau)
15-
{
16-
$this->nom = $tableau['nom'];
17-
$this->codeProduit = $tableau['code'];
18-
$this->prix = $tableau['prix'];
19-
$this->description = $tableau['description'];
20-
$this->quantite = $tableau['quantite'];
21-
$this->quantiteMin = $tableau['quantiteMin'];
22-
$this->categories = $tableau['categories'];
2342
}
2443

2544
?>

php/mysqli.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2+
$config = parse_ini_file('./config.ini');
23

3-
$mysqli = new mysqli("127.0.0.1", "root", "", "LogiKek", 3306);
4+
$mysqli = new mysqli("127.0.0.1", $config['username'], $config['password'], $config['dbname'], 3306);
45
if ($mysqli->connect_errno) {
5-
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
6+
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
67
}
78
?>

0 commit comments

Comments
 (0)