Jump to content

How to extract comma delimited items from a variable


geroido

Recommended Posts

Hi

I have a session variable called  $_SESSION['cart'];. This variable will typically hold values in the form 69,69,69,65,73,75. When I echoed this variable this is the exact readout that I got. What I need to do is extract the distinct values as well as a count of those distinct values. So for the above example I would get 'There are 3 (69s), 1 (65), 1 (73) and 1 (75)'. I need this info to insert into my database for fields 'quantity(3,1,1,1,) ' and MenuItemID(69, 65, 73, 75). Any ideas?

 

Hi The little guy

I got the following result from the echo which is great

 

Array ( [69] => 3 [65] => 1 [73] => 1 [75] => 1 )

 

but how do I insert the different values into the database fields e.g.

 

MenuItemID            quantity

69                              3

65                              1

73                              1

75                              1

$string = '69,69,69,65,73,75';
$arr = explode(',',$string);
$countVals = array_count_values($arr);
foreach($countVals as $key => $val){
     mysql_query("INSERT INTO tbl_name (`MenuItemID`,`quantity`) VALUES ('$key','$val')");
}

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.