Paid8x Posted June 4, 2011 Share Posted June 4, 2011 So I'm working on a shopping cart and basically, all the item's details are put into an array and stored in a session. When another product is $_POST'ed and compared to the existing $_SESSION variables to check if I need to update the quanity, I can't seem to get two of the same strings to equal each other. I tried trim, strcmp, == and === to compare. I've echoed the two variables and they look exactly the same but it's driving me crazy because I can't get them to match. What am I doing wrong? <?php if (isset($_POST['cut'])) {$itemToAdd = array($_POST['cut'], $_POST['size'], $_POST['color'], $_POST['design'], $_POST['style'], 1); $addOrNot = true; foreach ($_SESSION as $akey => $avalue) { if (strcmp( '$avalue[0]' , '$itemToAdd[0]' ) == 0) {$avalue[5]++; $addOrNot = false; }; }; if ($addOrNot = true) {$len = count($_SESSION); $m = "item".$len; $_SESSION[$m]=$itemToAdd; }; }; Thanks! MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/ Share on other sites More sharing options...
jcbones Posted June 4, 2011 Share Posted June 4, 2011 Without seeing how the array is built in $_SESSION, we really can't help to much. However, if you provide a dump of the session array, it would greatly help us in helping you. echo '<pre>'; print_r($_SESSION); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1224958 Share on other sites More sharing options...
Paid8x Posted June 4, 2011 Author Share Posted June 4, 2011 //this line $_SESSION[$m]=$itemToAdd; //and this line $itemToAdd = array($_POST['cut'], $_POST['size'], $_POST['color'], $_POST['design'], $_POST['style'], 1); all the values are simple strings like "Men" or "Green" Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1224960 Share on other sites More sharing options...
Paid8x Posted June 4, 2011 Author Share Posted June 4, 2011 oops... sorry didn't see the code you provided. Array ( [item0] => Array ( [0] => Men [1] => Small [2] => Organic Natural [3] => Eat [4] => Short Sleeve [5] => 1 ) [item1] => Array ( [0] => Men [1] => Small [2] => Organic Natural [3] => Eat [4] => Short Sleeve [5] => 1 ) [item2] => Array ( [0] => Men [1] => Small [2] => Organic Natural [3] => Eat [4] => Short Sleeve [5] => 1 ) [item3] => Array ( [0] => Men [1] => Small [2] => Organic Natural [3] => Eat [4] => Short Sleeve [5] => 1 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1224973 Share on other sites More sharing options...
jcbones Posted June 4, 2011 Share Posted June 4, 2011 Have you tried: if ($avalue[0] == $itemToAdd[0]) Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1224977 Share on other sites More sharing options...
Paid8x Posted June 4, 2011 Author Share Posted June 4, 2011 Yes I tried the == comparison. It doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1224978 Share on other sites More sharing options...
jcbones Posted June 4, 2011 Share Posted June 4, 2011 Try: if (trim($avalue[0]) == trim($itemToAdd[0])) Or, even: if (strtolower(trim($avalue[0])) == strtolower(trim($itemToAdd[0]))) Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1224981 Share on other sites More sharing options...
Paid8x Posted June 4, 2011 Author Share Posted June 4, 2011 That's not it either. I can even echo the two values surrounded by periods to see if it's any space or capitalization issues and they look exactly the same. It must have something to do with the utf-8 or ansi coding and how the session and post return strings differently coded? Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1224983 Share on other sites More sharing options...
Paid8x Posted June 4, 2011 Author Share Posted June 4, 2011 So I still don't know what's happening exactly so I'm going to leave this open in hopes that someone more knowledgeable than myself can shed some light and correct me if I'm wrong, but I came to this conclusion: When I tried to compare two array items even after trimming, turning to lowercase, using strcmp or double or triple equal signs to compare, they're not the same. You're comparing two objects with other properties, not just their values? Maybe? When you create two new variables and make them equal to the values of the array items the two new vars match up just fine. But even better, scrap using the string comparison altogether and use in_array to see if the string you're looking for is in the session array. len = count($_SESSION); if (isset($_POST['cut'])) {$addItem = array($_POST['cut'], $_POST['size'], $_POST['color'], $_POST['design'], $_POST['style'], 1); $addOrNot = True; foreach ($_SESSION as $akey => $avalue) { if (in_array($avalue[0], $addItem) && in_array($avalue[1], $addItem)) { $_SESSION[$akey][5]++; $addOrNot = False; }; }; if ($addOrNot == True) {$m = "item".$len; $_SESSION[$m]=$addItem; }; }; Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1224989 Share on other sites More sharing options...
revraz Posted June 4, 2011 Share Posted June 4, 2011 Sounds like poor database design to me actually. Fix that problem and it will solve this one. Why are you comparing description and not IDs? Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1224991 Share on other sites More sharing options...
Pikachu2000 Posted June 4, 2011 Share Posted June 4, 2011 Get rid of the quotes in the strcmp() function; they're making it compare the variable names as string literals. if (strcmp( $avalue[0] , $itemToAdd[0] ) == 0) // NOT if (strcmp( '$avalue[0]' , '$itemToAdd[0]' ) == 0) Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1224994 Share on other sites More sharing options...
Paid8x Posted June 4, 2011 Author Share Posted June 4, 2011 Thanks for the help revraz! That smack talking about my poor design instead of offering actual insight into the language is totally helpful!!! It's not an issue that there are over four hundred possible combinations of values and there are price adjustments for each possible value... obviously, it's that my design sucks and I should be trying to work with non-semantic id numbers instead of easily understandable product descriptions! On a serious note: Good catch, Pikachu. Thanks! Totally works. Can't believe I missed that. That's what I get for not taking a break, I guess. Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1225010 Share on other sites More sharing options...
jcbones Posted June 4, 2011 Share Posted June 4, 2011 Can't believe I missed it either. Quote Link to comment https://forums.phpfreaks.com/topic/238359-same-string-not-testing-for-equality/#findComment-1225059 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.