display accessory according to model

Discussion in 'PHP' started by vinpkl, Jul 7, 2012.

  1. #1
    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
     
    vinpkl, Jul 7, 2012 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    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:
     
    Anveto, Jul 7, 2012 IP
  3. vinpkl

    vinpkl Active Member

    Messages:
    899
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #3
    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
     
    vinpkl, Jul 7, 2012 IP
  4. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #4
    Can you show the full code that generates that output.
     
    Anveto, Jul 8, 2012 IP
  5. vinpkl

    vinpkl Active Member

    Messages:
    899
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #5
    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:
     
    vinpkl, Jul 8, 2012 IP
  6. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #6
    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);
     
    Anveto, Jul 8, 2012 IP
  7. vinpkl

    vinpkl Active Member

    Messages:
    899
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #7
    hi marukoy96

    i m not getting any value for $value

    $value is null

    vineet
     
    vinpkl, Jul 8, 2012 IP
  8. vinpkl

    vinpkl Active Member

    Messages:
    899
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #8
    sorry my mistake.

    i tried it again.
    But your code is displaying only 1 accessory

    vineet
     
    vinpkl, Jul 8, 2012 IP
  9. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #9
    <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:
     
    Anveto, Jul 8, 2012 IP
  10. vinpkl

    vinpkl Active Member

    Messages:
    899
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #10
    hi

    it still display only 1 accessory

    vineet
     
    vinpkl, Jul 8, 2012 IP
  11. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #11
    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:
     
    Anveto, Jul 8, 2012 IP
  12. vinpkl

    vinpkl Active Member

    Messages:
    899
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #12
    hi

    print_r($resultc)

    outputs
    Resource id #121

    vineet
     
    vinpkl, Jul 8, 2012 IP
  13. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #13
    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:
     
    Anveto, Jul 8, 2012 IP
  14. vinpkl

    vinpkl Active Member

    Messages:
    899
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #14
    again same resource id is ouput on print_r

    Resource id #121

    vineet
     
    vinpkl, Jul 8, 2012 IP
  15. Estevan

    Estevan Peon

    Messages:
    120
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    0
    #15
    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
     
    Estevan, Jul 8, 2012 IP
  16. vinpkl

    vinpkl Active Member

    Messages:
    899
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #16
    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
     
    vinpkl, Jul 8, 2012 IP
  17. Estevan

    Estevan Peon

    Messages:
    120
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    0
    #17
    you sent mobile name in query ?
     
    Estevan, Jul 8, 2012 IP