vinpkl Posted July 7, 2012 Share Posted July 7, 2012 hi all I have "model" field in accessory category of product table which have values like "E71,E72,E73,E74......" On the "mobile product" page i want to match the model of accessories with model of mobile and display them. /* query to select all models from accessories category from product table */ $qry_model="select * from product_table where category_id=4"; $qry_model_result=mysql_query($qry_model); while($qry_model_row=mysql_fetch_array($qry_model_result)) { $desc_model_all = $qry_model_row['model']; $desc_model_all = explode(',', $desc_model_all); foreach ($desc_model_all as $value) { echo $value; /* this outputs as E73E72 */ } } $mobile_model = $row['model']; if($value == $mobile_model) /* problem starts here */ { $qryc="select * from product_table where model = '$value' and category_id=4"; $resultc = mysql_query($qryc); if(mysql_num_rows($resultc)>0) { // product detail is displayed here } Quote Link to comment https://forums.phpfreaks.com/topic/265344-display-accessory-according-to-model/ Share on other sites More sharing options...
requinix Posted July 7, 2012 Share Posted July 7, 2012 I have "model" field in accessory category of product table which have values like "E71,E72,E73,E74......" BZZZT! There's your problem. Don't store multiple values in one column because it'll only cause you headaches later. Use a second table with (at least) two columns: one is for the product ID and the other is a "model". One product+model pair per row: if a product has three models then there will be three rows for it. Quote Link to comment https://forums.phpfreaks.com/topic/265344-display-accessory-according-to-model/#findComment-1359836 Share on other sites More sharing options...
vinpkl Posted July 7, 2012 Author Share Posted July 7, 2012 ok, i can create new table and add the new accessories accordingly. but what should i do for the accessories already added to the database. they are many. vineet Quote Link to comment https://forums.phpfreaks.com/topic/265344-display-accessory-according-to-model/#findComment-1359887 Share on other sites More sharing options...
requinix Posted July 8, 2012 Share Posted July 8, 2012 Since I don't know if there's a good MySQL solution, write a quick script that imports the data. $results = mysql_query("SELECT id field, model FROM table"); while($row = mysql_fetch_array($results)) { if ($row['model']) { $query = "INSERT INTO new table (product id, model id) VALUES ({$row['id field']}, '" . implode("'), ({$row['id field']}, '", explode(',', $row['model'])) . "')"; mysql_query($query); } } Quote Link to comment https://forums.phpfreaks.com/topic/265344-display-accessory-according-to-model/#findComment-1359977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.