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 } PHP: what should i do ? vineet
I don't think $value contains anything when you make your comparison, you should probably try something more like this $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) { $mobile_model = $row['model']; //im not sure what you are doing here, $row doesn't exist? or did i miss something? // You should query the accessory table to find items that match the model, let's say you get E71, then we could compare it $mobile_model = "E71"; //This is an example since $mobile_model wont contain anything the way you wrote it if($value == $mobile_model) { $qryc="select * from product_table where model = '$value' and category_id=4"; $resultc = mysql_query($qryc); if(mysql_num_rows($resultc)>0) { //show the product stuff here } } } PHP:
hi marukoy i tried your code. If i have 2 accessories for "E72" then your code will output 1 accessory 2 times. It does not output 2 different accessories. please check my screenshot http://wdpixels.com/banners/output.jpg vineet
hi here is rest code <table> <tr> <td> <?php if(isset($_REQUEST['id'])) { $id=$_REQUEST['id']; $qry="select * from product_table where product_id=$id and status='Y'"; $result = mysql_query($qry); if(mysql_num_rows($result)>0) { $row=mysql_fetch_array($result); $pname= $row['product_name']; echo "<img width=311 height=389 alt=$pname id='placeholder' src='/graphics/".$row['image'] . "'/>"; } ?> </td> <td> <?php $qry_model="select * from product_table where category_id=4 and status='Y'"; $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) { $mobile_model = $row['model']; if($value == $mobile_model) { $qryc="select * from product_table where model = '$value' and category_id=4"; $resultc = mysql_query($qryc); if(mysql_num_rows($resultc)>0) { //product details are displayed here } } } } } ?> </td> </tr> </table> PHP:
I haven't tested this code, but i think you are making it too complicated, try something like this <table> <tr> <td> <?php if(isset($_REQUEST['id'])) { $id=$_REQUEST['id']; $qry="select * from product_table where product_id=$id and status='Y'"; $result = mysql_query($qry); if(mysql_num_rows($result)>0) { $row=mysql_fetch_array($result); $pname= $row['product_name']; echo "<img width=311 height=389 alt=$pname id='placeholder' src='/graphics/".$row['image'] . "'/>"; } ?> </td> <td> <?php $desc_model_all = $row['model']; $desc_model_all = explode(',', $desc_model_all); foreach ($desc_model_all as $value) { $qryc="select * from product_table where model = '$value' and category_id=4 and status='Y'"; $resultc = mysql_query($qryc); if(mysql_num_rows($resultc)>0) { //product details are displayed here } } } ?> </td> </tr> </table> PHP: You should also make your $id more secure before inserting it into sql to make it more secure, atleast escape the $id. You could also use trim on the model variable to prevent errors should there be a space or something in the string. trim($value);
<table> <tr> <td> <?php if(isset($_REQUEST['id'])) { $id=$_REQUEST['id']; $qry="select * from product_table where product_id=$id and status='Y'"; $result = mysql_query($qry); if(mysql_num_rows($result)>0) { $row=mysql_fetch_array($result); $pname= $row['product_name']; echo "<img width=311 height=389 alt=$pname id='placeholder' src='/graphics/".$row['image'] . "'/>"; } ?> </td> <td> <?php $desc_model_all = $row['model']; $desc_model_all = explode(',', $desc_model_all); foreach ($desc_model_all as $value) { $qryc="select * from product_table where model = '$value' and category_id=4 and status='Y'"; $resultc = mysql_query($qryc); if(mysql_num_rows($resultc)>0) { while ($data = mysql_fetch_array($resultc)) { //Then echo the data like $data['product_name']; // for each accessory //product details are displayed here } } } } ?> </td> </tr> </table> PHP:
before the while loop try print_r($resultc); to see what the sql query is returning. If it is only returning one accessory then it may be something with your sql, otherwise it may be the space issue I talked about above where you could use trim. <table> <tr> <td> <?php if(isset($_REQUEST['id'])) { $id=$_REQUEST['id']; $qry="select * from product_table where product_id=$id and status='Y'"; $result = mysql_query($qry); if(mysql_num_rows($result)>0) { $row=mysql_fetch_array($result); $pname= $row['product_name']; echo "<img width=311 height=389 alt=$pname id='placeholder' src='/graphics/".$row['image'] . "'/>"; } ?> </td> <td> <?php $desc_model_all = $row['model']; $desc_model_all = explode(',', $desc_model_all); foreach ($desc_model_all as $value) { $value = trim($value); $qryc="select * from product_table where model = '$value' and category_id=4 and status='Y'"; $resultc = mysql_query($qryc); if(mysql_num_rows($resultc)>0) { while ($data = mysql_fetch_array($resultc)) { //Then echo the data like $data['product_name']; // for each accessory //product details are displayed here } } } } ?> </td> </tr> </table> PHP:
foreach ($desc_model_all as $value) { $value = trim($value); $qryc="select * from product_table where model = '$value' and category_id=4 and status='Y'"; $resultc = mysql_query($qryc); print_r($resultc); if(mysql_num_rows($resultc)>0) { while ($data = mysql_fetch_array($resultc)) { //Then echo the data like $data['product_name']; // for each accessory //product details are displayed here } } } } PHP:
hello you need use like or match try this $qry_model_result=mysql_query("select * from product_table where category_id=4"); 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){ $resultc = mysql_query("select * from product_table where model (like '%$value%') and category_id=4"); if(mysql_num_rows($resultc)>0){ // here display product based in $value while($result=mysql_fetch_array($resultc)){ echo $result['model']; } } } } PHP: Best
as far as i know if mobile name is = e71 Then "LIKE" with display products starting with "e7" also. i want exact "e71" match only vineet