Jump to content

IF statement not working correctly?


scottgm@me.com
 Share

Recommended Posts

Hello PHP Wizards, i wonder if anyone can help, Ive got an associative array set up with a bunch of items, the idea is to compare the array items with items from a form using $_POST.

 

So far ive got the items displaying from the form as follows

 

 

<?php
			while ($client_prices = each($_POST)) {			/* sets the name of the array to $client_prices for easy identification */
		 		echo $client_prices['key'];					/* Displays the keys withing the $client_prices array */
				echo ' - ';
		 		echo $client_prices['value'];				/* Displays the values within the $client_prices array */
		 		echo '<br />';
				}							
?>

 

and for the sake of it ill display the array ive set up on the server side using similar code :

 

<?php
		while ($server_prices = each($coffee_prices)) {			/* sets the name of the array to $server_prices for easy identification */
			echo $server_prices['key'];							/* Displays the keys withing the $server_prices array */						
			echo ' - ';				
			echo $server_prices['value'];						/* Displays the values within the $server_prices array */			
			echo '<br />';
			}							
	?>

 

now, what i want to do is make sure that the keys (name of items) and the values (cost of items) from the client side are the same as the keys and values ive set on the server. so i created an if statement :

 

<?php 
		if(
		$client_prices['key'] === $server_prices['key'] && $client_prices['value'] === $server_prices['value']
		)
			echo 	'Prices Match';
		else
			echo 	'<h2>Oops! Somthing Has Gone, Please Call Us To Place Your Order</h2>';

?>

 

 

so i thought i had it all working when as it came back with 'prices match', i thought all is good. however when i purposely change the value of one of the items in my server side array so it will be different from the incoming client value, it still says 'prices match' when they clearly don;t?

 

Anyone help would be appreciated :)

 

p.s. im fairly new to php, and this is for my learning, not a real client.

Link to comment
Share on other sites

Where is that condition  in relation to those loops? each returns the current key/value of the array and moves the internal pointer forward.  Your loops work by doing that over and over until each returns false (no more array elements). If that condition comes after your loops, all you are doing is comparing false/null variables to false/null variables. 

 

The only way your condition is really gonna work is if you were to combine those two loops and put the condition inside the loop.

Link to comment
Share on other sites

 

Where is that condition  in relation to those loops? each returns the current key/value of the array and moves the internal pointer forward.  Your loops work by doing that over and over until each returns false (no more array elements). If that condition comes after your loops, all you are doing is comparing false/null variables to false/null variables. 

 

The only way your condition is really gonna work is if you were to combine those two loops and put the condition inside the loop.

 

Hello, Thanks for the reply

 

ive created a new if statement but now im not getting any result

 


<?php
		while (($server_prices = each($coffee_prices)) && ($client_prices = each($_POST))) {
		if	(isset ($client_prices) === ($server_prices)) {
			echo 	'Prices Match'; 
		}
		else {
			echo 	'<h2>Oops! Somthing Has Gone, Please Call Us To Place Your Order</h2>';
		}
		}
	?>	

 

client prices is the info taken from the form $_POST and server prices is the array in the server $coffee_prices that i've renamed and been able to display using the while loops i've shown in my first post?

 

 

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.