PHP怎么让mysql数据表自动归类?

WBOY
Release: 2016-06-23 14:04:05
Original
991 people have browsed it

有两个表

商品数据表 goods:title,商品标题。brand,品牌id
id    title                     brand
1   化妆品欧莱雅65号   1
2   化妆品温碧泉75
3   化妆品雅琳娜45
4   化妆品香奈儿45     2
5   化妆品欧诗漫22    

品牌表 brand
id  name
1   欧莱雅
2   香奈儿
3   温碧泉
4   雅琳娜
5   欧诗漫


用PHP和sql语句 让 goods表没brand id的号自动获取品牌id,比如商品表id2后面的brand值为3.不知道怎么做了。哪位给个思路。实际上品牌表里面有几百个品牌,商品名肯定包含了一个品牌名。


回复讨论(解决方案)

遍历goods表中的title ,看是否包含brand中的name在里面,有的话,记下id放入一个新数组中作为title的brand值,楼主可以试下

用了个笨办法实现了,只需要功能,不讲究效率了。。

你去研究下orm吧

笨办法就是。先用substr截取3-6位的字符串。取得诸如“欧莱雅”“香奈儿”之类的品牌名称,再去brand表里面查询对应的brand_id。。前提是。你的3-6位字符串恰好是品牌名。

substr($str,9,9); 由于是中文字符串。 如果品牌刚好是第四个中文字符开始。那么截取9个字符、
foreach($brand_name as $key=> $item){
  $name = substr($str,9,9);
  switch($name){
  case '欧莱雅':
   //do something;
   $brand_name[$key]['brand_id'] = 3;
  break;
  case 'xxx':
}
}
不知道是不是你要的那种。。。

substr($str,9,9); 由于是中文字符串。 如果品牌刚好是第四个中文字符开始。那么截取9个字符、
foreach($brand_name as $key=> $item){
  $name = substr($str,9,9);
  switch($name){
  case '欧莱雅':
   //do something;
   $brand_……
其实每个标题没那么规则的 用多重条件 先遍历品牌 然后 遍历没品牌的商品 然后修改为品牌ID

那foreach循环里修改下。 碰到没有id的在查询。
如果商品名称不规则。则用preg_match 正则查询出需要的数据。 逻辑你应该清楚。编码的事相信你可以搞定

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!