rabadaba Posted April 2, 2008 Share Posted April 2, 2008 I am working on my first shopping cart and I need the following: I need to compare if a newly selected item has already been added to my cart using a SESSION variable. What is the best way to compare a variable delimited using ',' and not add the new item if it already exist. I don't need you to formulate code - just help out with the function that compares variable contents. i.e. if my SESSION variable contains 'AAA,BBB,CCC,DDD' and the user selects a second CCC which is not a valid selection for this state, how do I figure that out? Thanks Link to comment https://forums.phpfreaks.com/topic/99274-a-little-php-help-please/ Share on other sites More sharing options...
CMC Posted April 2, 2008 Share Posted April 2, 2008 if I understand you correctly (somehow I doubt I do), you need to separate the data? the explode function. explode it using ',' (i.e: explode(",",$text) and then you could use in_array to see if it is there. ex: <?php $item1 = 'CCC'; $inCart = $_SESSION['cart']; $individualItems = explode(",",$inCart); if(in_array($item1,$inCart)){ echo "in cart"; }else{ echo "not in cart"; } ?> Link to comment https://forums.phpfreaks.com/topic/99274-a-little-php-help-please/#findComment-507947 Share on other sites More sharing options...
rabadaba Posted April 2, 2008 Author Share Posted April 2, 2008 Perfect. Thanks!! :) Link to comment https://forums.phpfreaks.com/topic/99274-a-little-php-help-please/#findComment-507950 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.