resident1155 Posted November 28, 2009 Share Posted November 28, 2009 Hello, I am having some trouble assigning a value to a variable inside a switch statement. What I am trying to do here is trip an error if the user has already added an item in the shopping cart. The While and Foreach loops work perfectly fine. The problem is that the value of variable $error does not get passed outside of the case. As a result, I cannot get the error message to display. I really need to get this to work and any help is appreciated. The code looks something like this: switch ($action) { case 'add' : { if ($_SESSION['CartID'] == "") { $query = 'INSERT INTO ShopCart( UserID )VALUES("' . $UserID . '")'; mysql_query($query, $db) or die(mysql_error($db)); $Shop = 'SELECT ShopCartID FROM ShopCart WHERE UserID = "' . $UserID . '" ORDER BY ShopCartID ASC LIMIT 1'; $Cart = mysql_query($Shop); $ShopCart = mysql_fetch_row($Cart); $_SESSION['CartID'] = $ShopCart[0]; } $query = 'SELECT ProductID FROM ShopCartLine WHERE ShopCartID = "' . $_SESSION['CartID'] . '"'; $result=mysql_query($query, $db) or die(mysql_error($db)); //$row = mysql_fetch_row( $result ); while($var = mysql_fetch_array($result)) { foreach ($var as $i) { if ($i == $ProductID) { $error = 1; //TRIP THE ERROR HERE! echo "<script type='text/javascript'>window.top.location='http://amarcy2.db-class.ids.uic.edu/shopcart.php';</script>"; } } } } The code being used to display the error message is as follows: if ($error == 1) { echo '<p>You already have this item in your basket!</p>'; } Quote Link to comment https://forums.phpfreaks.com/topic/183190-problem-with-variable-declaration-in-switch-statement/ Share on other sites More sharing options...
Virvo Posted November 28, 2009 Share Posted November 28, 2009 PHP does not consider variables to be local to loops or if/switch statements ... only to functions. The problem you are having is probably because your switch is inside a function and the $error variable is inside another function. a quick fix is using global variables (although using globals is generally bad practice) instead of: $error use: $GLOBALS['error'] Quote Link to comment https://forums.phpfreaks.com/topic/183190-problem-with-variable-declaration-in-switch-statement/#findComment-966857 Share on other sites More sharing options...
resident1155 Posted November 28, 2009 Author Share Posted November 28, 2009 Actually the code is definitely not in a function which is why I have no idea why it won't pass the value of $error outside of the switch case. I also tried a global variable a number of times with no success. Does anybody else know why this isn't working and or how to resolve the issue? Quote Link to comment https://forums.phpfreaks.com/topic/183190-problem-with-variable-declaration-in-switch-statement/#findComment-967069 Share on other sites More sharing options...
KevinM1 Posted November 28, 2009 Share Posted November 28, 2009 Can you show more code? Is $error being declared somewhere else? How are you using $error once its been set? Quote Link to comment https://forums.phpfreaks.com/topic/183190-problem-with-variable-declaration-in-switch-statement/#findComment-967071 Share on other sites More sharing options...
PFMaBiSmAd Posted November 28, 2009 Share Posted November 28, 2009 It is fairly likely that $ProductID is not set and if ($i == $ProductID) { is FALSE. What makes you think $error should be a 1? You also don't need the foreach() loop because $var will only consist of one element, $var['ProductID'], because that is all your SELECT statement is returning. Quote Link to comment https://forums.phpfreaks.com/topic/183190-problem-with-variable-declaration-in-switch-statement/#findComment-967074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.