Un problème sur mon code php a propos d'un choix de couleur


#1

Bonjour a tous voila j’ai un problème sur mon code de php qui consiste à que l’utilisateur choisit la couleur de sa peinture a partir d’une boucle while afin que ca soit automatiser

<?php
  /*	On fait une boucle while pour automatiser la condition de la variable paint afin de savoir si l'utilisateur a bien choisi sa couleur*/
$paint=$_POST['paint'];
$submit=$_POST['sub'];
while ($paint<9){
    if($paint==TRUE && $submit==TRUE){
        echo "vous avez oublié de choisir votre couleur";
    }
    else{
        
    }
}
$paint++;
?>

<SELECT name="paint" size="1">
<OPTION>rouge
<OPTION>noir
<OPTION> vert
</SELECT>
<input type="submit" value="envoyer" name="sub">

`


#2
$paint = isset($_REQUEST['paint']) ? $_REQUEST['paint'] : null;
$submit = isset($_REQUEST['sub']) ? $_REQUEST['sub'] : null;
while ($paint<9){
    if($paint==TRUE && $submit==TRUE){
        echo "vous avez oublié de choisir votre couleur";
    }
    else{
        
    }
}
$paint++;    // <- outside of loop

#3

thanks but it doesn’t work there is no the message for the echo when i run the code :confused:


#4

There is a disagreement between your values for $paint. The while loop indicates it is a number, not a boolean.

Test the variable before running the loop…

if ($paint) {
    while ($paint < 9) {
        // ...
        $paint++;
    }
}

#5

oh so i put $paint = true in the while loop ?


#6

Or use another variable, say, $flag which is set to a boolean at some point.


#7

like that ?

<?php
  
$paint = isset($_REQUEST['paint']) ? $_REQUEST['paint'] : null;
$sub = isset($_REQUEST['sub']) ? $_REQUEST['sub'] : null;
if($paint){
    while($paint==false){
    if($paint==TRUE && $submit==TRUE){
        echo "vous avez oublié de choisir votre couleur";
    }
    else{
        
   }
 $paint++;
}
        
       
    }




?>


#8

Sort of. I fixed a typo in my earlier example.

$submit = ...

Also we could set $flag,

$flag = false

Now the if statement,

if ($flag && $submit) {

}

These are minor concerns if you are having difficulty with the program logic. For that you will need to do some more reading and sketch out your plain.


#9

Thanks for your advices :slight_smile: i tried to do this code but i don’t know why i have both echo my code seems good and i deleted the html because i changed the choice to the price because to see if the while loop works with the if condition since i am a beginner for php

<?php
 

$price=0;

while ($price<=20){
    if($price<=18){
        echo 'you cant buy it';
    }
 else {
        echo 'you can buy it ';
    }
     $price++;   
    }


#10

Do you have any other programming experience? If not, then suggest you set PHP aside for the time and learn JavaScript.


#11

well i learned java and python in school but python was for algorithm :slight_smile:


#12

The reason I suggest JavaScript is that it looks and feels a lot like PHP, and it is a script language, unlike Java. Python is also a script language, but does not have a direct connection to browsers the way JS does. Not to offend, but from what we can see, you could use some practice in building code logic for which JS is a great candidate, and a lot more friendly than PHP.


#13

Thanks for the advices :slight_smile: