how to remove comma and display records

て烟熏妆下的殇ゞ 提交于 2019-12-26 15:21:02

问题


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.



来源:https://stackoverflow.com/questions/40866465/how-to-remove-comma-and-display-records

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!