Can't get my $fees to display with the correct amount

I am trying to do the following if statement to add fees for my services… Basically, I am having a piano website with 3 services. If an user were to watch level 1 content, it would cost them $100 per month, for a level 2 content, it would cost them $150 per month and for a level 3, it would cost them $200 per month. User can have more than 1 service… they can enroll in both level and level 2 or level 1 and level 2 and level 3… I tried to do the following if statements but I can’t get my level 1 and level 2 or level 1 and level 2 and level 3 to show the correct amount…:Can I have both and && and || operators in my if statements?


<?php
   
   if (!isset($_POST['submit'])) {
      header("Location: ../signup.php");
      exit();
   } else {
   	   include_once 'dbh.php';

   	   $first = mysqli_real_escape_string($conn, $_POST['first']);
   	   $last = mysqli_real_escape_string($conn, $_POST['last']);
   	   $email = mysqli_real_escape_string($conn, $_POST['email']) ;
   	   $uid = mysqli_real_escape_string($conn, $_POST['uid']);
   	   $password = mysqli_real_escape_string($conn, $_POST['pwd']);
   	   $user_permission = mysqli_real_escape_string($conn, $user_permission = 'Standard User');
   	   $freelesson = mysqli_real_escape_string($conn, $_POST['freelesson']);
   	   $subscriptionplan = mysqli_real_escape_string($conn, $_POST['subscriptionplan']);
   	   $subscriptionplan2 = mysqli_real_escape_string($conn, $_POST['subscriptionplan2']);
   	   $subscriptionplan3 = mysqli_real_escape_string($conn, $_POST['subscriptionplan3']);
   	   $fees = mysqli_real_escape_string($conn, $_POST['fees']);
   	   $totalfees = mysqli_real_escape_string($conn, $_POST['totalfees']);
   	   $expired = mysqli_real_escape_string($conn, $expired = '0');
       $user_activate = mysqli_real_escape_string($conn, $user_activate = '0');
       
      
   	   //Error handlers...

   	   if (empty($first) || empty($last) || empty($email) || empty($uid)|| empty($password)) {
   	   	   header("Location: ../signup.php?signup=empty");
   	   	   exit();
   	   } else {
          //Check if input characters are valid
   	   	    if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
                header("Location: ../signup.php?signup=invalid");
                exit();
   	   	    } else {
   	   	    	
   	   	    	//Checking for valid emails
                 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                     header("Location: ../signup.php?signup=email");
                     exit();
                 } else {
                   if (Strlen($password) < 5) {
                   header("Location: ../signup.php?signup=invalidlength");
                   exit();
                   } else {
                      if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,20}$/', $password)) {
                       header ("Location: ../signup.php?signup=notalphanumeric");
                       exit();
                      } else {
                           if ($subscriptionplan == 'Level 1' || $subscriptionplan2 == 'Level 1' || $subscriptionplan3 == 'Level 1') {

                           $fees =100;
                           } else {
                           	    if($subscriptionplan == 'Level 2' || $subscriptionplan2 == 'Level 2' || $subscriptionplan3 == 'Level 2') {

                                $fees = 150;
                           	    } else {
                           	    	   if ($subscriptionplan == 'Level 3' || $subscriptionplan2 == 'Level 3' || $subscriptionplan3 == 'Level 3') {
                                      
                                      $fees = 200;

                           	    	   } else {
                           	    	   	    if ($subscriptionplan == 'Level 1' && $subscriptionplan2 == 'Level 2' && $subscriptionplan3 == 'None' || $subscriptionplan2 == 'Level 1' && $subscriptionplan3 == 'Level 2' && $subscriptionplan == 'None' || $subscriptionplan3 == 'Level 1' && $subscriptionplan == 'Level 2' && $subscriptionplan2 == 'None') {

                                            $fees = 250;

                           	    	   	    } else {
                           	    	   	    	   if ($subscriptionplan == 'Level 1' && $subscriptionplan2 == 'Level 2' && $subscriptionplan3 == 'Level 3' || $subscriptionplan2 == 'Level 1' && $subscriptionplan3 == 'Level 2' && $subscriptionplan == 'Level 3' || $subscriptionplan3 == 'Level 1' && $subscriptionplan == 'Level 2' && $subscriptionplan2 == 'Level 3') {

                           	    	   	    	   	$fees = 450;

                           	    	   	    	   }
                           	    	   	    }
                           	    	   }
                           	    }
                           }




                           
                      $sql = "SELECT * FROM users WHERE user_uid = $uid'";
                      $result = mysqli_query($conn, $sql);
                      $resultCheck = mysqli_num_rows($result);

                      if($resultCheck > 0) {
                         header("Location: ../signup.php?signup=usertaken");
                         exit();
                      } else {








           // Select subscription 

          


                                     
       

                         // Supply a random generated token for email activation

                      	$token = 'qqewreqreqwsdfdfdafcbvcQERFGHFGHGFHRETERTDF!@#$%^^()';
                      	$token = str_shuffle($token);
                      	$token = substr($token, 0, 10);

                      	//Hashing the password
                      	$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                      	//Insert the user into the database
                      	$sql ="INSERT INTO users (user_first, user_last, user_email, user_uid, user_password, user_permission, freelesson, datejoined, user_token, user_activate) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd', '$user_permission', '$freelesson', now(),'$token', '$user_activate');";

                      	$name = $first .$last;
                        $to = $email;
                        $header = 'From: pianocourse101@hotmail.com';
        
                        $subject = 'Email activation is required';
                         $message = <<<EMAIL

                        Hello $first  $last, 


                        Thanks for registering with PianoCourse101!
                        Please activate your account below before you  
                        can login. Activate your account by clicking   
                        on the following link below....

                         http://localhost/loginsystem/includes/activate.php?email=$to&activatetoken=$token

EMAIL;
       
        
       

                        mail($to, $subject, $message, $header);



                      	mysqli_query($conn, $sql);

                       // insert into user_lessonsubscription table

                      $sql ="INSERT INTO user_lessonsubscription (user_uid, subscriptionplan, subscriptionplan2, subscriptionplan3, fees, totalfees, paidbydate, expirydate, expired) VALUES ('$uid', '$subscriptionplan', '$subscriptionplan2', '$subscriptionplan3','$fees', '$totalfees', now() + interval '1' week, now() + interval '1' month, '$expired')";

                        mysqli_query($conn, $sql);





                      	header("Location: ../signup.php?signup=success");
                      	exit();
                        }
                 }
             }
         }
     
   	   }
   	   }

   	   
  } 	 
   

Play around with this:

  // $subs = array( 'Level 1',  'Level 2',  'Level 3' );
$subs = array( $subscripition1, $subscriptio2, $subscripio3, $subsciption4);
  $fees = 0;
  foreach($subs as $sub):
    switch($sub):
      case 'Level 1' : $fees += 100;
        break;
      case 'Level 2' : $fees += 150;
        break;
      case 'Level 3' : $fees += 200;
        break;
    endswitch;
  endforeach;    
  echo 'Total ==> ' .$fees;

Edit:
Amended $subs array.
Please excuse spelling but it is not easy to rectify using a tablet :frowning:

Of course… I should have used the switch option… but should my if statement be working?

I have heard about switch but when would I need to use switch as opposed to if statement? when there are too many variables to compare? Your switch statement almost work but if I inserted level 1 and level 2, the cost should be 250 but for some reason, it is displaying 450… I think your code is adding all 3 services instead of just 2 because for the third option… I have inputed “none”

You can mix || and && but you need to be aware that && has a higher precedence than ||. Meaning that when you have a || b && c || d php will parse that as a || (b && c) || d. If you want it to parse it differently you need to add your own parentheses, eg (a || b) && (c || d).

1 Like

Thanks for the suggestions! Will try that way… When would I use switch instead of if statement? In this scenario, would I use switch? The switch looks easier but the total amount is incorrect… but i guess for my if statement, I dd have. if (a && b || b && c)… but it still didn’t work…

I managed to get it to work with the case statement but am still wondering if it should have worked with my if statement and I thought that my and and or were placed correctly… but I guess the switch statement is easier to use for multiple if conditions…

Out of interest, could you post your final solution you came up with? It might help people who are also reading this thread and are wondering what happened :slight_smile:

No worries… Here is my code with the case statement…but I am still not sure why my if statements did not work…I guess I am setting a condition to add the amount if the user selected level 1 and level 2 but not level 3… I am just wondering, perhaps I can’t do this… if($subscriptionplan == “None”) but I did use none in my select option… I am also curious to work out why it didn’t work but here is my code…

<?php
   
   if (!isset($_POST['submit'])) {
      header("Location: ../signup.php");
      exit();
   } else {
   	   include_once 'dbh.php';

   	   $first = mysqli_real_escape_string($conn, $_POST['first']);
   	   $last = mysqli_real_escape_string($conn, $_POST['last']);
   	   $email = mysqli_real_escape_string($conn, $_POST['email']) ;
   	   $uid = mysqli_real_escape_string($conn, $_POST['uid']);
   	   $password = mysqli_real_escape_string($conn, $_POST['pwd']);
   	   $user_permission = mysqli_real_escape_string($conn, $user_permission = 'Standard User');
   	   $freelesson = mysqli_real_escape_string($conn, $_POST['freelesson']);
   	   $subscriptionplan = mysqli_real_escape_string($conn, $_POST['subscriptionplan']);
   	   $subscriptionplan2 = mysqli_real_escape_string($conn, $_POST['subscriptionplan2']);
   	   $subscriptionplan3 = mysqli_real_escape_string($conn, $_POST['subscriptionplan3']);
   	  // $fees = mysqli_real_escape_string($conn, $_POST['fees']);
   	  // $totalfees = mysqli_real_escape_string($conn, $_POST['totalfees']);
   	   $expired = mysqli_real_escape_string($conn, $expired = 8);
       $user_activate = mysqli_real_escape_string($conn, $user_activate = 0);
       
      
   	   //Error handlers...

   	   if (empty($first) || empty($last) || empty($email) || empty($uid)|| empty($password)) {
   	   	   header("Location: ../signup.php?signup=empty");
   	   	   exit();
   	   } else {
          //Check if input characters are valid
   	   	    if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
                header("Location: ../signup.php?signup=invalid");
                exit();
   	   	    } else {
   	   	    	
   	   	    	//Checking for valid emails
                 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                     header("Location: ../signup.php?signup=email");
                     exit();
                 } else {
                   if (Strlen($password) < 5) {
                   header("Location: ../signup.php?signup=invalidlength");
                   exit();
                   } else {
                      if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,20}$/', $password)) {
                       header ("Location: ../signup.php?signup=notalphanumeric");
                       exit();
                      } else {

                           
                      $sql = "SELECT * FROM users WHERE user_uid = $uid'";
                      $result = mysqli_query($conn, $sql);
                      $resultCheck = mysqli_num_rows($result);

                      if($resultCheck > 0) {
                         header("Location: ../signup.php?signup=usertaken");
                         exit();
                      } else {

                    
  								$totalfees = 0;
  										
   										    switch($subscriptionplan):
     										 case 'Level 1' : $totalfees += 100;
      										  break;
    										  case 'Level 2' : $totalfees += 150;
      										  break;
     										 case 'Level 3' : $totalfees += 200;
       										 break;
                                             case 'None' : $totalfees += 0;
                                             break;
    										endswitch;
 											    
 												

 												 switch($subscriptionplan2):
     										 case 'Level 1' : $totalfees += 100;
      										  break;
    										  case 'Level 2' : $totalfees += 150;
      										  break;
     										 case 'Level 3' : $totalfees += 200;
       										 break;
                                             case 'None' : $otalfees += 0;
                                             break;
    										endswitch;
 											 
 												 
	 			

	 			                            switch($subscriptionplan3):
     										 case 'Level 1' : $totalfees += 100;
      										  break;
    										  case 'Level 2' : $totalfees += 150;
      										  break;
     										 case 'Level 3' : $totalfees += 200;
       										 break;
                                             case 'None' : $totalfees += 0;
                                             break;
    										endswitch;
 											    
 											
	 			
	 			






           // Select subscription 

          


                                     
       

                         // Supply a random generated token for email activation

                      	$token = 'qqewreqreqwsdfdfdafcbvcQERFGHFGHGFHRETERTDF!@#$%^^()';
                      	$token = str_shuffle($token);
                      	$token = substr($token, 0, 10);

                      	//Hashing the password
                      	$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                      	//Insert the user into the database
                      	$sql ="INSERT INTO users (user_first, user_last, user_email, user_uid, user_password, user_permission, freelesson, datejoined, user_token, user_activate) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd', '$user_permission', '$freelesson', now(),'$token', '$user_activate');";

                      	$name = $first .$last;
                        $to = $email;
                        $header = 'From: pianocourse101@hotmail.com';
        
                        $subject = 'Email activation is required';
                         $message = <<<EMAIL

                        Hello $first  $last, 


                        Thanks for registering with PianoCourse101!
                        Please activate your account below before you  
                        can login. Activate your account by clicking   
                        on the following link below....

                         http://localhost/loginsystem/includes/activate.php?email=$to&activatetoken=$token

EMAIL;
       
        
       

                        mail($to, $subject, $message, $header);



                      	mysqli_query($conn, $sql);

                       // insert into user_lessonsubscription table

                      $sql ="INSERT INTO user_lessonsubscription (user_uid, subscriptionplan, subscriptionplan2, subscriptionplan3, totalfees, paidbydate, expirydate, expired) VALUES ('$uid', '$subscriptionplan', '$subscriptionplan2', '$subscriptionplan3','$totalfees', now() + interval '1' week, now() + interval '1' month, '$expired')";

                        mysqli_query($conn, $sql);





                      	header("Location: ../signup.php?signup=success");
                      	exit();
                        }
                 }
             }
         }
     
   	   }
   	   }

   	   
  } 	 
   

All three subscription plans should be in the array that the foreach iterate through only once.

Your if statements were remarkably convoluted.

What happens if you add another two, three or four extra subscription plans?

Edit:
Script in previous post amended.

But if I put it inside the array, it will enter all 3 plans but the user can also select plan 1 and plan 2 but not plan 3… When I omitted the array, it works…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.