Comparasion between products

Discussion in 'PHP' started by lokodaweb, Sep 5, 2010.

  1. #1
    db.sql

    index.php

    <?
    $servidor       = "localhost";
    $basedados      = "teste";
    $usuario        = "root";
    $senha          = "root";
    
    //conexão com o servidor
    $conn = @mysql_connect($servidor, $usuario, $senha)or die (mysql_error());
    
    //conexao o Banco de Dados.    
    $db = @mysql_select_db($basedados, $conn) or die (mysql_error());
    ?>     
    <table border="1">
    <tr>
    <td>
    Caracteristicas
    </td>
    <?  
    $id = "'4','3','1'";
     
    //Aqui faco o select dos produtos, usando o IN para pegar varios registros
    $sql = mysql_query("SELECT * FROM produto WHERE id IN ($id)") or die (mysql_error());  
    while ($rs = mysql_fetch_array($sql)) {
    ?>
    <td>
    <table border="1">
    <?
    //Aqui faco o select da info
    $sql_info = mysql_query("SELECT * FROM info ORDER BY info ASC") or die (mysql_error());
    while ($rs_info = mysql_fetch_array($sql_info)) {
    
    //Aqui faco o select das info dos produtos usando o IN
    $sql_info_prod = mysql_query("SELECT * FROM info_produto WHERE id_produto IN ($id)")or die (mysql_error());                                                                                                                                    
    while ($rs_info_prod = mysql_fetch_array($sql_info_prod)) {
    
    //aqui faco a verificacao se o ID do produto e igual ao da info e as info sao iguais, mostra o ON
    if($rs['id'] == ($rs_info_prod['id_produto']) && $rs_info['info'] == ($rs_info_prod['info'])) {
    echo '<tr><td>';
    echo '<b>'.$rs_info_prod['info'].'</b>';
    echo '</td></tr>';
    //aqui faco a verificacao caso o ID seja diferente e as info iguais, mostrara o OFF
    }elseif ($rs['id'] != ($rs_info_prod['id_produto']) && $rs_info['info'] == ($rs_info_prod['info'])) {
    echo '<tr><td>';
    echo '<s>'.$rs_info_prod['info'].'</s>';
    echo '</td></tr>';
    }
    }
    }?>    
    </table>
    </td>
    <?}?>
    </tr>
    </table>
    Code (markup):
    To make easy the understood about my situation, I´ll available some pictures.

    The example below it´s the comparion between the ID 3 (TV lcd 50) and ID 2 (TV lcd 42)

    [​IMG]

    As you can notice the comparation is correct, because not there is repeat the data.

    The example below it´s the comparion between the ID 3 (TV lcd 50) and ID 1 (TV Plasma 70)

    [​IMG]

    As you can notice 1920x1080 and FULL HD repeated as much as exist as not exist
    The idea it´s that remain just 'exist' and not repeat the 'not exist'

    One more example between ID 4,3 and 1

    [​IMG]

    As you can notice 1920x1080 and FULL HD repeated as much as exist as not exist

    That´s is my original idea:

    [​IMG]

    The real example, check the site out below:

    http://www.rossiresidencial.com.br/empreendimentos-comparacao.aspx?emp1=NB-1102&emp2=NB-1401&estado=SP&cidade=9668

    This website is what I wish do. Check out Opções de Lazer

    I just need that don´t repeat yes as much no, only keep yes.

    So just help me about IF, to make of right way.

    So, I guess that must solve in the condition IF,...

    Thanks you guys !
     
    lokodaweb, Sep 5, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I'd probably want to play around with this for a bit but I'd start by turning your query around to

    $sql = "select `info`, sum(case `id_produto` when {$id_a} then 1 else 0 end) as proda, sum(case `id_produto` when {$id_b} then 1 else 0 end) as prodb
    from `info_produto`
    order by `info`
    group by `info`"

    that will give you a full list of the info options (once per option) and whether or not it applies to your products.

    Use php to set the values of $id_a and $id_b as required.

    This isn't tested but will be close to what you need.
     
    sarahk, Sep 6, 2010 IP