Reputation: 223
I am Makeing a store. When user will purchase an item In Mysql database it will be added that they purchased product id 1 or 2 or both. Suppose a user purchases both (id 1 and 2) then i want the mysql column purchases
of table users
to show 1,2
, I can do that. But the main part comes that how can i get the result in PHP and separate the string by delimiter ,
in loop then get all the numbers separately and perform this:
//inside loop with $i incrementing till no more delimiter is found.
if($purchase[$i] == 1){
echo 'You purchased '.$i.'';
}
Basically i want to store all purchased in purchases
column in users
separated by a comma then retrieve the result, separate the comma then compare each number in PHP.
PS. Noob here!
Upvotes: 0
Views: 949
Reputation: 780724
This is a bad database design, you should create a table with a separate row for each purchase. But if you're stuck with this, use explode()
:
$purchases = explode(',', $row['purchases']);
foreach ($purchases as $purchase) {
echo 'You purchased '.$purchase.'<br>';
}
Upvotes: 1
Reputation:
You can use PHP explode function to get all values as an elements of Array: http://php.net/manual/en/function.explode.php
Upvotes: 0