Bonjour, je souhaite faire apparaître dans un tableau une ligne comprenant des infos de deux tables, produit et famille.
Tout fonctionnait avec seulement les informations du produit, mais maintenant je veux faire apparaître leur famille à coté avec la colonne famille.nom mais je n'y arrive pas.
Voici mon code :
Comme vous pouvez le voir j'ai essayé de créer $sql2 mais je ne sais pas si j'ai besoin d'un deuxième while ou si je peux incorporer famille.nom dans le premier.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php $localhost = "localhost"; $username = "root"; $password = ""; $dbname = "bon_commande"; $con = new mysqli($localhost, $username, $password, $dbname); if( $con->connect_error){ die('Error: ' . $con->connect_error); } $sql = "SELECT * FROM produit"; $sql2 ="SELECT nom FROM famille"; $recherche =""; if( isset($_GET['recherche']) ){ $recherche = mysqli_real_escape_string($con, htmlspecialchars($_GET['recherche'])); $sql = "SELECT * FROM produit WHERE nomProduit LIKE '%$recherche%'"; $sql2 ="SELECT nom FROM famille INNER JOIN produit ON famille.id_famille=produit.id_famille;"; } $result = $con->query($sql); $result2 = $con->query($sql2); ?> <html> <head> <title>Recherche de produits</title> <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <header> <img src="./images/batimans.jpg" alt="" height="80px"/> </header> <div class="container"> </br> <form method="GET"> <input type="text" placeholder="que cherchez vous ?" name="recherche"> <input type="submit" value="Recherche" name="btn" class="btn btn-sm btn-primary"> <a href="index.php"><button class="btn btn-outline-success" type="button">Retour</button></a> <!-- à faire : liste déroulante pour sélection de famille de produits --> </form> <h2>Produits proposés</h2> <table class="table table-striped table-responsive"> <tr> <th>ID</th> <th>nom</th> <th>Famille</th> <th>Prix</th> </tr> <?php while($row = $result->fetch_assoc()){ ?> <tr> <td><?php echo $row['id_produit']; ?></td> <td><?php echo $row['nomProduit']; ?></td> <td><?php echo $row['prixProduit']; ?></td> <td><input type="button" value="Ajouter" name="btn" class="btn btn-sm btn-primary"></td> <?php } while($row = $result2->fetch_assoc()){ ?> <td><?php echo $row['nom']; ?></td> <?php } ?> </tr> </table> <?php include 'commande.php'; ?> </div> </body> </html>
En tout cas pour l'instant c'est sans succès.
Merci de votre aide.
Partager