Jump to content

A Little php Help Please


rabadaba

Recommended Posts

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

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";
}
?>

Archived

This topic is now archived and is closed to further replies.

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