- This topic is empty.
-
AuthorPosts
-
December 14, 2010 at 6:04 pm #31004
scottgm
MemberHello 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
while ($client_prices = each($_POST)) { /* sets the name of the array to $client_prices for easy identification */
echo $client_prices; /* Displays the keys withing the $client_prices array */
echo ' - ';
echo $client_prices; /* Displays the values within the $client_prices array */
echo '
';
}
?>and for the sake of it ill display the array ive set up on the server side using similar code :
while ($server_prices = each($coffee_prices)) { /* sets the name of the array to $server_prices for easy identification */
echo $server_prices; /* Displays the keys withing the $server_prices array */
echo ' - ';
echo $server_prices; /* Displays the values within the $server_prices array */
echo '
';
}
?>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 :
if(
$client_prices === $server_prices && $client_prices === $server_prices
)
echo 'Prices Match';
else
echo 'Oops! Somthing Has Gone, Please Call Us To Place Your Order
';
?>
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.
December 16, 2010 at 11:22 am #69675Rob MacKay
ParticipantNeed some { } in there :)
if( $client_prices === $server_prices && $client_prices === $server_prices ) {
echo 'Prices Match';
} else {
echo 'Oops! Somthing Has Gone, Please Call Us To Place Your Order
';
}
Or some :’s
if( $client_prices === $server_prices && $client_prices === $server_prices ):
echo 'Prices Match';
else:
echo 'Oops! Somthing Has Gone, Please Call Us To Place Your Order
';
endif;
December 19, 2010 at 10:13 am #69493jimmymac87
Memberalso instead of === use == to check out more operators go here http://www.w3schools.com/PHP/php_operators.asp
December 19, 2010 at 11:20 am #69495TT_Mark
MemberNot strictly true @Robskiwarrior The if/else statement @scottgm has written is perfectly valid.
You only need {} if the if statement does more than one line
December 19, 2010 at 1:15 pm #69496Rob MacKay
ParticipantYes, but it is terrible practice lol
December 19, 2010 at 4:30 pm #69498TT_Mark
MemberThink that is more personal preference! :-)
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.