How does PHP automatically classify mysql data tables? There are two tables
Product data table goods: title, product title. Brand, brand id
Id title brand
1 cosmetic L'Oreal 65 1
2 cosmetics Wen Biquan 75
3 cosmetic Yarina 45
4 cosmetics Chanel 45 2
5 cosmetic oshiman 22
Brand
Id name
1 L'Oreal
2 Chanel
3 Wen Biquan
4 Yarina
5. ou Shiman
Use the PHP and SQL statements to automatically obtain the brand id for the goods table without the brand id. for example, the brand value after the item table id2 is 3. I don't know how to do it. Which gives me a thought. In fact, there are hundreds of brands in the brand list, and the product name must include a brand name.
Reply to discussion (solution)
Traverse the title in the goods table to check whether the name in the brand is included. If yes, write down the id and put it into a new array as the brand value of the title. you can try it.
I implemented it in a stupid way. I only need functions and don't pay attention to efficiency ..
Study the orm.
The stupid way is. Use substr to extract 3-6 characters. Obtain the brand names such as "L'Oreal" and "Chanel" and query the corresponding brand_id in the brand table .. The premise is. Your 3-6 character string is exactly the brand name.
Substr ($ str, 9, 9); it is a Chinese string. If the brand begins with the fourth Chinese character. Then, it intercepts 9 characters,
Foreach ($ brand_name as $ key => $ item ){
$ Name = substr ($ str, 9, 9 );
Switch ($ name ){
Case 'oreal ':
// Do something;
$ Brand_name [$ key] ['brand _ id'] = 3;
Break;
Case 'XXX ':
}
}
I don't know if it's what you want...
Substr ($ str, 9, 9); it is a Chinese string. If the brand begins with the fourth Chinese character. Then, it intercepts 9 characters,
Foreach ($ brand_name as $ key => $ item ){
$ Name = substr ($ str, 9, 9 );
Switch ($ name ){
Case 'oreal ':
// Do something;
$ Brand _......
In fact, different titles use multiple criteria to traverse the brand and then traverse the products without the brand, and then change them to the brand ID.
Modify it in the foreach loop. If there is no id in the query.
If the product name is irregular. Then, use the preg_match regular expression to query the required data. You should be clear about the logic. I believe you can fix the coding.