Jump to content

PHP logic help


simeonC
Go to solution Solved by simeonC,

Recommended Posts

Here I have three form elements

<td colspan="6">Intended Payment Method 
        
<input type="checkbox" name="payment_method[]" id="checkbox" value="cash">      
Cash
<input type="checkbox" name="payment_method[]" id="checkbox2" value="check">
Check 
<input type="checkbox" name="payment_method[]" id="checkbox3" value="credit/debit">
Credit/Debit
</td>

I would like to insert them into a database with the following conditions, however I have no idea how to construct these conditions.

Condition 1: if there is no records which indicate the Intended payment method Insert that which have been checked.

Condition 2: if there is a record for the customer which indicates an intended payment method and the user is attempting to change these settings using the same fields.

  -Check what payment method have been inserted the mysql database

  -Check if the new payment methods inserted match whats in the database; if so do nothing to that row

if the option has been unchecked or checked update the database to meet that condition

 

As you can see below I have not gotten far because I am fluent with php to that which you would consider mastery. Any help will be greatly appreciated.

if (isset($payment_method) && is_array($payment_method)) { 
	$count_insert=count($payment_method);
	$payment_count_query=mysql_query("SELECT * FROM payment_option");
	$count_query=mysql_num_rows($payment_count_query);
	echo $count_query; echo $count_insert;
	
	if ($count_query==0){
		foreach ($payment_method as $value){
			$insert_method==mysql_query("INSERT INTO payment_option (repair_id,payment_method) VALUES ('$_SESSION[repair_id]','$value')");
			}//end foreach statment where query count equals 0
		}//end of queyr count
	
	if ($count_query>0){
		
		foreach ($payment_method as $value){
			
			while ($payment_fetch=mysql_fetch_array($payment_count_query)){
				if ($payment_fetch['payment_method']==$value){}else{
					mysql_query("UPDATE payment_option SET ");
					
					}/// I was lost at this point because I can see the outcome not being what I had in mind///////
				
				}
			
			}///end foreach statement count_query<0
		
		
		}//end if statment for query count==1
	   
}//end of if payment method was set
Link to comment
Share on other sites

  • Solution
well since everyone seemed to overlook my post. Not to sure if it was hard to understand. I will just post my answer to my own post.
$payment_method=$_POST['payment_method'];//the check box array from forum
	$payment_count=count($payment_method);//count of elements in the array
	list($a, $b, $c) = ($payment_method); // use the list function to take each value and place them into a variable in the following order these variables are used in my query. 

//if no checkboxes are selected we do nothing 

//if you one item has been selected we perform the following action//

if ($payment_count==1) { $query_count=mysql_query("SELECT * FROM payment_option WHERE repair_id='$_SESSION[repair_id]'");
$count=mysql_num_rows($query_count);//we count the option in the database to see if it exist
 
///if not we insert the checked elements into the data base
if ($count==0){$insert_options=mysql_query("INSERT INTO payment_option (repair_id,`$a`) VALUES ('$_SESSION[repair_id]','$a')");}
 
////if there is an item in the database already we delete it and insert the new options
if ($count>0){$delete_query=mysql_query("DELETE FROM payment_option WHERE repair_id='$_SESSION[repair_id]'");
$insert_options=mysql_query("INSERT INTO payment_option (repair_id,`$a`) VALUES ('$_SESSION[repair_id]','$a')");
 
}
 
}
///Here the same logic above is used same format for the next two options just added the listed variables into the query
elseif ($payment_count==2){
$query_count=mysql_query("SELECT * FROM payment_option WHERE repair_id='$_SESSION[repair_id]'");
$count=mysql_num_rows($query_count);
 
if ($count==0){$insert_options=mysql_query("INSERT INTO payment_option (repair_id,`$a`,`$b`) VALUES ('$_SESSION[repair_id]','$a','$b')");}
 
if ($count>0){$delete_query=mysql_query("DELETE FROM payment_option WHERE repair_id='$_SESSION[repair_id]'");
$insert_options=mysql_query("INSERT INTO payment_option (repair_id,`$a`,`$b`) VALUES ('$_SESSION[repair_id]','$a','$b')");
}
 
}
elseif  ($payment_count==3){
$query_count=mysql_query("SELECT * FROM payment_option WHERE repair_id='$_SESSION[repair_id]'");
$count=mysql_num_rows($query_count);
 
if ($count==0){$insert_options=mysql_query("INSERT INTO payment_option (repair_id,`$a`,`$b`,`$c`) VALUES ('$_SESSION[repair_id]','$a','$b','$c')");}
if ($count>0){$delete_query=mysql_query("DELETE FROM payment_option WHERE repair_id='$_SESSION[repair_id]'");
$insert_options=mysql_query("INSERT INTO payment_option (repair_id,`$a`,`$b`,`$c`) VALUES ('$_SESSION[repair_id]','$a','$b','$c')");
}
 
}
 
I just have a feeling that there was a shorter way to do this if so please post so i can learn as I age thank you

 

 

 

Edited by simeonC
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.