Les teste de condition
samiapirou
Messages postés
143
Statut
Membre
-
samiapirou Messages postés 143 Statut Membre -
samiapirou Messages postés 143 Statut Membre -
Bonjour,
j'ai fais un programme qui permet de tester d'abod l'etat du client pour caluculer le taotal mais il me prend tjr la première condition pour quoi?
aider moi svp
.merci
function MontantGlobal(){
$total=0;
for($i = 0; $i < count($_SESSION['panier']['refProduit']); $i++)
{
$req1= mysql_query("SELECT COUNT(*) AS existe1 FROM client ")or die(mysql_error());
$donnees1 = mysql_fetch_array($req1);
if($donnees1['existe1']!=0)
{
$req1= mysql_query("SELECT * FROM client")or die(mysql_error());
$donnees = mysql_fetch_array($req1);
$_SESSION['etat_cli']=$donnees['etat_client']; //variable session
$eta=$_SESSION['etat_cli'];
if($eta='particulier'){
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i];
else
if($eta='anseig'){
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixAnseig'][$i];
}
}
return $total;
}
}
j'ai fais un programme qui permet de tester d'abod l'etat du client pour caluculer le taotal mais il me prend tjr la première condition pour quoi?
aider moi svp
.merci
function MontantGlobal(){
$total=0;
for($i = 0; $i < count($_SESSION['panier']['refProduit']); $i++)
{
$req1= mysql_query("SELECT COUNT(*) AS existe1 FROM client ")or die(mysql_error());
$donnees1 = mysql_fetch_array($req1);
if($donnees1['existe1']!=0)
{
$req1= mysql_query("SELECT * FROM client")or die(mysql_error());
$donnees = mysql_fetch_array($req1);
$_SESSION['etat_cli']=$donnees['etat_client']; //variable session
$eta=$_SESSION['etat_cli'];
if($eta='particulier'){
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i];
else
if($eta='anseig'){
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixAnseig'][$i];
}
}
return $total;
}
}
3 réponses
-
Salut, problème d'accolades:
ton else et son contenu sont DANS le bloc du if, or ils doivent être apès, comme ceci:if(condition) { // Code } else if(condition2) { // Sinon... }
Au passage, l'indentation du code (décalage vers la droite selon les blocs de code, comme dans l'exemple) ne devrait pas être en option, et les forums de CommentCaMarche ont une balise <code ></code > pour y rendre le code plus lisible.
from human import idiocy
del idiocy-
voici gje l'ai rectifier comme ca mais toujour il prend la première condition
function MontantGlobal(){
$total=0;
for($i = 0; $i < count($_SESSION['panier']['refProduit']); $i++)
{
$req1= mysql_query("SELECT COUNT(*) AS existe1 FROM client ")or die(mysql_error());
$donnees1 = mysql_fetch_array($req1);
if($donnees1['existe1']!=0)
{
$req1= mysql_query("SELECT * FROM client")or die(mysql_error());
$donnees = mysql_fetch_array($req1);
$_SESSION['etat_cli']=$donnees['etat_client']; //variable session
$eta=$_SESSION['etat_cli'];
if($eta='anseig'){
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixAnseig'][$i];}
else if($eta='particulier'){
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i];
}
}
return $total;
}
}
-
-
Autre chose (pourtant évidente) que je n'ai pas remarquée: le test d'égalité s'effectue avec DEUX symboles égal, en utilisant un seul on affecte une valeur à une variable.
Le contenu d'un if($eta='anseig') s'éxécutera toujours car l'expression $eta='anseig' renverra toujours "anseig", valeur considérée comme true par PHP.-
-
-
-
" je ne sais pas comment le faire": je ne suis pas là pour vous donner un cours, si vous en cherchez un , allez voir sur le Site du Zéro, y'a une section sur les switch en PHP.
-
-
j'ai l'ai fair avec les case mais il m'affiche cette erreur
Parse error: syntax error, unexpected T_STRING, expecting T_CASE or T_DEFAULT or '}' in C:\Users\CBS-COMPUTER\Desktop\EasyPHP-5.3.3.1\www\doc1\panier_fct_vehi.php on line 134
voila le code:::::
function MontantGlobal(){
$req1= mysql_query("SELECT COUNT(*) AS existe1 FROM client ")or die(mysql_error());
$donnees1 = mysql_fetch_array($req1);
if($donnees1['existe1']!=0)
{
$req1= mysql_query("SELECT * FROM client")or die(mysql_error());
$donnees = mysql_fetch_array($req1);
$_SESSION['etat_cli']=$donnees['etat_client']; //variable session
$eta=$_SESSION['etat_cli'];}
$total=0;
for($i = 0; $i < count($_SESSION['panier']['refProduit']); $i++)
{
switch($etat){
case0:
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i];break;
case1:
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixAnseig'][$i];break;
case2:
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixLicence'][$i];break;
default:
}
return $total;
}