Passer valeur pour panier PHP
bonjour,
voila j ai decider de passer las variable par session, comme ca plus de probleme avec url mais cela me met des message d'erreur:
Citation:
Warning: array_search() [function.array-search]: Wrong datatype for second argument in C:\wamp\www\lileauxboutschou\pages\fonctions-panier.php on line 32
Warning: array_push() [function.array-push]: First argument should be an array in C:\wamp\www\lileauxboutschou\pages\fonctions-panier.php on line 41
Warning: array_push() [function.array-push]: First argument should be an array in C:\wamp\www\lileauxboutschou\pages\fonctions-panier.php on line 42
Warning: array_push() [function.array-push]: First argument should be an array in C:\wamp\www\lileauxboutschou\pages\fonctions-panier.php on line 43
voici donc mes pages:
panier.php
Code:
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 87 88 89 90 91 92
| <?php
include("../include/session.php");
include_once("fonctions-panier.php");
session_start();
$ida=$_SESSION['id'];
$la=$_SESSION['l'];
$qa=$_SESSION['q'];
$pa=$_SESSION['p'];
if (isset($_GET['action'])){
$erreur=false;
if(in_array( $_GET['action'],array('ajout', 'suppression')))
$action = $_GET['action'];
else
$erreur=true;
$id = intval($ida);
$l = preg_replace('#\v#', '', $la);
$q = intval($qa);
$p = floatval($pa);
}
elseif(isset($_POST['action'])){
unset($_GET);
$erreur=false;
if(in_array($_POST['action'],array('ajout', 'suppression')))
$action=$_POST['action'];
else
$erreur=true;
$id = floatval($_POST['id']);
$l = preg_replace('#\v#', '',$_POST['l']);
$p = floatval($_POST['p']);
$QteArticle = array();
$i=0;
foreach ($_POST['QteArticle'] as $contenu){
$QteArticle[$i++] = intval($contenu);
}
}
if ($erreur==false){
switch($action){
Case "ajout":
ajouterArticle($id,$l,$q,$p);
break;
Case "suppression":
supprimerArticle($id);
break;
/*Case "refresh" :
for ($i = 0 ; $i < count($QteArticle) ; $i++){
modifierQTeArticle($_SESSION['panier']['libelleProduit'][$i],round($QteArticle[$i]));
}
break;*/
Default:
break;
}
}
$body='<form method="post" action="panier.php">
<div id="panier">
<table>
<tr>
<td colspan="4"><h3>Votre panier<h3></td >
</tr>
<tr class="libelle">
<td>Libellé</td>
<td>Quantité</td>
<td>Prix Unitaire</td>
<td>Action</td>
</tr>
';
if (creationPanier()){
//$nbArticles=count($_SESSION['panier']['libelleProduit']);
$nbArticles=count($_SESSION['panier']['idProduit']);
echo $_SESSION['panier']['idProduit'];
//session_destroy();
if ($nbArticles <= 0)
$body.='<tr><td>Votre panier est vide. </ td></tr>';
else{
for ($i=0 ;$i < $nbArticles ; $i++){
$body.='<tr>';
$body.='<td>'.htmlspecialchars($_SESSION['panier']['libelleProduit'][$i]).'</ td>';
$body.='<td>'.htmlspecialchars($_SESSION['panier']['qteProduit'][$i]).'</td>';
$body.='<td>'.htmlspecialchars($_SESSION['panier']['prixProduit'][$i]).'</td>';
$body.='<td><a href="'.htmlspecialchars("panier.php?action=suppression&id=".rawurlencode($_SESSION['panier']['idProduit'][$i])).'">Supprimer</a></td>';
$body.='</tr>';
}
$body.='<tr><td colspan=\"4\">';
$body.='<h3>Montant Total de votre commande: '.MontantGlobal();
$body.=' euros</h3></td></tr>';
}
}
$body.='</table></div>
</form>
</body>
</html>';
include("../include/base.php");
?> |
fonctions-panier.php:
Code:
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
| <?php
function creationpanier(){
$ret=false;
if (isset( $_SESSION['panier']))
$ret = true;
else{
$_SESSION['panier']=array();
$_SESSION['panier']['idProduit'] = array();
$_SESSION['panier']['libelleProduit'] = array();
$_SESSION['panier']['qteProduit'] = array();
$_SESSION['panier']['prixProduit'] = array();
$ret=true;
//print_r($_SESSION);
session_destroy();
}
return $ret;
}
function ajouterArticle($idProduit,$libelleProduit,$qteProduit,$prixProduit){
if (creationpanier()){
$positionProduit = array_search($idProduit,$_SESSION['panier']['idProduit']);
if ($positionProduit !== false){
$_SESSION['panier']['qteProduit'][$positionProduit] += $qteProduit ;
}
else{
array_push( $_SESSION['panier']['idProduit'],$idProduit);
array_push( $_SESSION['panier']['libelleProduit'],$libelleProduit);
array_push( $_SESSION['panier']['qteProduit'],$qteProduit);
array_push( $_SESSION['panier']['prixProduit'],$prixProduit);
}
}
else
echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}
function supprimerArticle($idProduit){
if (creationpanier()){
$tmp=array();
$tmp['idProduit'] = array();
$tmp['libelleProduit'] = array();
$tmp['qteProduit'] = array();
$tmp['prixProduit'] = array();
for($i = 0; $i < count($_SESSION['panier']['idProduit']); $i++) {
if ($_SESSION['panier']['idProduit'][$i] !== $idProduit){
array_push( $tmp['idProduit'],$_SESSION['panier']['idProduit'][$i]);
array_push( $tmp['libelleProduit'],$_SESSION['panier']['libelleProduit'][$i]);
array_push( $tmp['qteProduit'],$_SESSION['panier']['qteProduit'][$i]);
array_push( $tmp['prixProduit'],$_SESSION['panier']['prixProduit'][$i]);
}
}
$_SESSION['panier'] = $tmp;
unset($tmp);
}
else
echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}
function modifierQTeArticle($idProduit,$qteProduit){
if (creationpanier()){
if ($qteProduit > 0){
$positionProduit = array_search($idProduit, $_SESSION['panier']['idProduit']);
if ($positionProduit !== false){
$_SESSION['panier']['qteProduit'][$positionProduit] = $qteProduit ;
}
}
else
supprimerArticle($idProduit);
}
else
echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}
function MontantGlobal(){
$total=0;
for($i = 0; $i < count($_SESSION['panier']['idProduit']); $i++){
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i];
}
return $total;
}
?> |
et voici le liens que j appelle avant:
Code:
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
| <?php
session_start();
include("../include/session.php");
$id=$_GET['id'];
$l=$_GET['l'];
$q=$_GET['q'];
$p=$_GET['p'];
$_SESSION['id'] = $id ;
$_SESSION['l'] = $l ;
$_SESSION['q'] = $q ;
$_SESSION['p'] = $p ;
$body='
<div id="choix">
<div id="conteneur_panier" style="float:left;">
<h4>Choix 1</h4>
<ul>
<br /><li><a href="../index.php">Continuer mes achats</a><br /><br /></li>
</ul>
</div>
<div id="conteneur_panier"style="float:right;margin-bottom:100px;" >
<h4>Choix 2</h4>
<ul class="last_element">
<br /><li><a href="panier.php?action=ajout">Commander maintenant</a><br /><br /></li>
</ul>
</div>
</div> ';
//$body.='<a href="choix.php?action=ajout&id='.$pro['produit_id'].'&l='.$sous_categorie['categorie_titre'].'&q=1&p='.$pro['produit_prix'].'"><img src="../images/caddie.gif">Commande terminer</a>';
include("../include/base.php");
?> |
merci de votre aide.