how to remove comma and display records

2019-09-22 08:08发布

问题:

I have a database table package_details which include fields

  1. package_id
  2. features
  3. description

in features field I have stored data like feature1,fea2,fea3 and so on. I have separated features by comma.
I have to display records. Now my question is how to remove comma and display records one by one like

feature1  
fea2  
fea3  

can anyone help me? Thanks.

回答1:

$array = explode(',' $data); 

will explode your string based on comma



回答2:

A simple solution :

//The initial string where ... represents more elements
$string = feature1,fea2,fea3,....;

//$items is an array which contains the elements obtained after breaking $string
$items = explode(',' , $string);

foreach($items as $item)
{
    echo $iteml;
    echo "</br>";
}


回答3:

The solutions posted are correct but the proper way to store multiple values for a record is in a foreign key table. For example you would also create a table called features which would contain all the possible features any of your products could have. Ideally each record would have an Id as the primary key. You then would have a table called product-features. Each record would have the product id from your product table and the feature id from the features table. Hence linking the two together. For multiple features you would have multiple records with the same product id.

You would then use a join query to output the product features when displaying your product.