par
Botitine » 14 janv. 2009, 12:02
Tout d'abord bonjour à tous, très heureux d'être parmis vous

.
Voici mon petit problème :
Je fais un site pour un amis.
C'est un site immobilier et il voudrait que ses clients puissent mettre les biens qui les intéressent dans une sélection.
Le problème est que je flanche sur ce qui est pour moi le moins compliqué : n'autoriser le même biens qu'a apparaitre une seule fois dans le panier.
Voici mon code que j'ai simplifié :
Code ajout
<?php
if (isset($_POST['panier']) && $_POST['panier']=='Ajouter')
{
$id = $_POST['idpanier'];
$reference=$data['id'];
$nb=1; //par défaut la quantité est = 1
$prix=$data['budget'];
$liste[]=array($id,$reference,$nb,$prix);
$_SESSION['liste']=$liste;
}
?>
Mon panier
<?php
// on se connecte à notre base
$base = mysql_connect ();
mysql_select_db ();
if ($liste <= 0){
echo '<tr bgcolor="#ffec90"><td valign="middle" align="center" height="400"><span class="titre2"> Votre panier est vide</span></td></tr>
<tr bgcolor="#ffec90"><td colspan="3" height="24" align="center" background="./img/bcriteres.png"> </td></tr>';
}
else
{
for ($i=0;$i<count($liste);$i++)
{
$sql = 'SELECT * FROM immobilier WHERE id ="'.$liste[$i][0].'"';
// on exécute la requête
$req = mysql_query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
// on va scanner tous les tuples un par un
while ($data = mysql_fetch_array($req)) {
// on affiches les résultats dans la <table>
echo '<tr bgcolor="#ffec90"><td valign="top"><table width="500" border="0" align="center" cellpadding="0" cellspacing="0" style="border: solid #ff8600 1px">
<tr bgcolor="#ffec90" height="36">
<td colspan="2" class="titrebien"> ' , htmlentities(trim($data['type'])) , ' ' , htmlentities(trim($data['transaction'])) , ' à ' , stripslashes ( htmlentities(trim($data['ville']))) , ' (' , htmlentities(trim($data['dept'])) , ') </td>
<td align="right" style="color:red; font-weight:bold; font:16px;">' , htmlentities(trim($data['budget'])) , ' € </td>
</tr>
<tr bgcolor="#ffec90" height="36">
<td width="135" rowspan="3" align="center"><img src="./img/biens/' , htmlentities(trim($data['image'])) , '" height="100" width="100" border="0" /></td>
<td width="400"><span class="recherche" style="font-weight:bold">Référence :</span> ' , htmlentities(trim($data['id'])) , ' </td>
<td width="75" class="recherche" > </td>
</tr>
<tr bgcolor="#ffec90">
<td width="400" valign="top"><span class="recherche" style="font-weight:bold">Contact :</span> ' , htmlentities(trim($data['contact'])) , '</td>
<td class="recherche"> </td>
</tr>
<tr bgcolor="#ffec90">
<td><div align="justify">'.substr(''.stripslashes (htmlentities(trim($data['description']))).'', 0, 150).'...</div></td>
<td class="recherche"> </td>
</tr>
<tr bgcolor="#ffec90" height="36">
<td colspan="3" align="center" class="recherche"><a href=index.php?page=id9&ref='.htmlentities(trim($data['id'])).'><img src="./img/boutons/savoir.jpg" alt="En savoir +" height="20" border="0" /></a></td>
</tr>
</table><br></td></tr>';
}
}
// on libère l'espace mémoire alloué pour cette requête
mysql_free_result ($req);
echo '<td colspan="3" height="24" align="center" background="./img/bcriteres.png"> </td>';
}
// on libère l'espace mémoire alloué pour cette requête
mysql_free_result ($resultat);
// on ferme la connexion à la base de données.
mysql_close ();
?>
Avec ce code, a chaque que je clique sur "ajouter au panier" le bien s'ajoute au panier indéfiniment alors que je voudrais qu'il n'apparaisse qu'une fois par bien.
Merci de votre aide
Tout d'abord bonjour à tous, très heureux d'être parmis vous :D .
Voici mon petit problème :
Je fais un site pour un amis.
C'est un site immobilier et il voudrait que ses clients puissent mettre les biens qui les intéressent dans une sélection.
Le problème est que je flanche sur ce qui est pour moi le moins compliqué : n'autoriser le même biens qu'a apparaitre une seule fois dans le panier.
Voici mon code que j'ai simplifié :
[b]Code ajout[/b]
[php]
<?php
if (isset($_POST['panier']) && $_POST['panier']=='Ajouter')
{
$id = $_POST['idpanier'];
$reference=$data['id'];
$nb=1; //par défaut la quantité est = 1
$prix=$data['budget'];
$liste[]=array($id,$reference,$nb,$prix);
$_SESSION['liste']=$liste;
}
?>
[/php]
[b]Mon panier[/b]
[php]
<?php
// on se connecte à notre base
$base = mysql_connect ();
mysql_select_db ();
if ($liste <= 0){
echo '<tr bgcolor="#ffec90"><td valign="middle" align="center" height="400"><span class="titre2"> Votre panier est vide</span></td></tr>
<tr bgcolor="#ffec90"><td colspan="3" height="24" align="center" background="./img/bcriteres.png"> </td></tr>';
}
else
{
for ($i=0;$i<count($liste);$i++)
{
$sql = 'SELECT * FROM immobilier WHERE id ="'.$liste[$i][0].'"';
// on exécute la requête
$req = mysql_query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
// on va scanner tous les tuples un par un
while ($data = mysql_fetch_array($req)) {
// on affiches les résultats dans la <table>
echo '<tr bgcolor="#ffec90"><td valign="top"><table width="500" border="0" align="center" cellpadding="0" cellspacing="0" style="border: solid #ff8600 1px">
<tr bgcolor="#ffec90" height="36">
<td colspan="2" class="titrebien"> ' , htmlentities(trim($data['type'])) , ' ' , htmlentities(trim($data['transaction'])) , ' à ' , stripslashes ( htmlentities(trim($data['ville']))) , ' (' , htmlentities(trim($data['dept'])) , ') </td>
<td align="right" style="color:red; font-weight:bold; font:16px;">' , htmlentities(trim($data['budget'])) , ' € </td>
</tr>
<tr bgcolor="#ffec90" height="36">
<td width="135" rowspan="3" align="center"><img src="./img/biens/' , htmlentities(trim($data['image'])) , '" height="100" width="100" border="0" /></td>
<td width="400"><span class="recherche" style="font-weight:bold">Référence :</span> ' , htmlentities(trim($data['id'])) , ' </td>
<td width="75" class="recherche" > </td>
</tr>
<tr bgcolor="#ffec90">
<td width="400" valign="top"><span class="recherche" style="font-weight:bold">Contact :</span> ' , htmlentities(trim($data['contact'])) , '</td>
<td class="recherche"> </td>
</tr>
<tr bgcolor="#ffec90">
<td><div align="justify">'.substr(''.stripslashes (htmlentities(trim($data['description']))).'', 0, 150).'...</div></td>
<td class="recherche"> </td>
</tr>
<tr bgcolor="#ffec90" height="36">
<td colspan="3" align="center" class="recherche"><a href=index.php?page=id9&ref='.htmlentities(trim($data['id'])).'><img src="./img/boutons/savoir.jpg" alt="En savoir +" height="20" border="0" /></a></td>
</tr>
</table><br></td></tr>';
}
}
// on libère l'espace mémoire alloué pour cette requête
mysql_free_result ($req);
echo '<td colspan="3" height="24" align="center" background="./img/bcriteres.png"> </td>';
}
// on libère l'espace mémoire alloué pour cette requête
mysql_free_result ($resultat);
// on ferme la connexion à la base de données.
mysql_close ();
?>
[/php]
Avec ce code, a chaque que je clique sur "ajouter au panier" le bien s'ajoute au panier indéfiniment alors que je voudrais qu'il n'apparaisse qu'une fois par bien.
Merci de votre aide