Composite key, it shows in the query only the last record [closed]

-1
$cria = "CREATE TABLE tb_complemento (
  'com_referencia_produto' varchar(09),
  'com_sequencia_produto'  int(01) NOT NULL,
  'com_aplicacao_produto'  varchar(80),
  'com_anoi_anof'          varchar(30),
  'com_complemento'        varchar(50),
   CONSTRAINT com_chave primary key(com_referencia_produto,com_sequencia_produto)
)  ENGINE=MyISAM DEFAULT CHARSET=latin1";
$result_cr = mysqli_query($strcon, $cria);   

Only the last record in the add-in table reads. I inserted the array, but it did not work.

 <!DOCTYPE html>
    <html>
    <head>
    <meta charset='UTF-8'/>
    <title>Catálogo Thor</title>
    <link rel="stylesheet" href="../css/catalog.css"/>  
    <script src="jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="jquery.elevatezoom.js"></script>

</head>
<body>

 <div id="titulo">AURÉLIUS CATALOG</div>

<?php
$host    = "localhost";
$dbname  = "bd_thor";
$user    = "root";
$senha   = "";
$arquivo_pd = "tb_produtos";     
$arquivo_co = "tb_complemento";         
$selecao = "0"; 

$rfprod  = "";
$approd  = "";
$rows    = "0";    

$aplic   = array();
$anoif   = array();
$compl   = array();  



$refer   = $_GET['referencia'];   

$strcon  = mysqli_connect($host, $user, $senha, $dbname) or die('Erro de conexão ao bd_thor');

$sql     = "SELECT * FROM $arquivo_pd where pro_referencia_produto = '$refer'"
                                          or die('Erro de selecão na tb_produtos');
$result_pd  = mysqli_query($strcon,$sql)  or die('Erro de consulta na tb_produtos');
while ($row = mysqli_fetch_array($result_pd)) {
       $rfprod = $row['pro_referencia_produto'];
       $approd = $row['pro_aplicacao_produto'];
       $fmprod = $row['pro_familia'];
       $cfprod = $row['pro_cofap'];
       $moprod = $row['pro_monroe'];
       $nkprod = $row['pro_nakata'];
       $kyprod = $row['pro_kyb'];
       $mtprod = $row['pro_motorcraft'];  
       $marca   = $row['pro_marca']; 
       $familia = $row['pro_familia']; 
       $carcaca = $row['pro_carcaca'];  
       $caixa   = $row['pro_caixa'];
       $observ  = $row['pro_observacoes'];    

}
?> 
<div class="referencia">
    <span><font color="#519cf8"><?php echo "$rfprod"; ?></font></span>
    <span><?php echo " - ";     ?></span>
    <span><?php echo "$approd"; ?></span><br><br>
</div> 

<div id="imagem-item">
   <img  id="img_01"     src="../images/small/RE38-FR.jpg" data-zoom-image="../images/large/RE38-FR.jpg"/>  

   <div  id="gallery_01">  
      <a href="#" data-image="../images/small/RE38-FR.jpg" data-zoom-image="../images/large/RE38-FR.jpg"> 
         <img id="img_01" src="../images/thumb/RE38-FR.png" />
      </a>

      <a href="#" data-image="../images/small/RE38-PR.jpg" data-zoom-image="../images/large/RE38-PR.jpg">
         <img id="img_02" src="../images/thumb/RE38-PR.png" />
      </a>

      <a href="#" data-image="../images/small/RE38-PE.jpg" data-zoom-image="../images/large/RE38-PE.jpg">
         <img id="img_03" src="../images/thumb/RE38-PE.png" />
      </a>

      <a href="#" data-image="../images/small/CAMERA-SM.jpg" data-zoom-image="../images/large/CAMERA-LG.jpg">
         <img id="img_04" src="../images/thumb/camera.png" />
      </a>
   </div>
</div>  

<div id="dados-conversao">
    <div class="tit-conversao">CONVERSÃO</div>    
    <table style="width:100%" cellspacing=5>
    <tr>

    <td style=color:#f6eb8f width="20%">COFAP</td>
    <td style=color:#f6eb8f width="20%">MONROE</td>
    <td style=color:#f7e867 width="20%">NAKATA</td>
    <td style=color:#f7e867 width="20%">KAYABA</td>
    <td style=color:#f7e867>MOTORCRAFT</td>
    </tr>
    <tr>
    <td><?php echo "$cfprod" ?></td> 
    <td><?php echo "$moprod" ?></td> 
    <td><?php echo "$nkprod" ?></td> 
    <td><?php echo "$kyprod" ?></td> 
    <td><?php echo "$mtprod" ?></td>     
    </tr> 
    </table> 
</div> 
<?php    
$sql = "SELECT * FROM $arquivo_co where com_referencia_produto = '$refer'"
                                          or die('Erro de selecão na tb_complemento');
$result_co  = mysqli_query($strcon,$sql)  or die('Erro de consulta na tb_complemento');
while ($row = mysqli_fetch_array($result_co)) {
       $aplic[] = $row['com_aplicacao_produto'];
       $anoif[] = $row['com_anoi_anof'];
       $compl[] = $row['com_complemento'];
 }

for($i = 0; $i < 20; $i++) { 
    if ($aplic[$i] != "") {
        $rows++; 
    }
}

for($i = 0; $i < $rows; $i++) {  
?>    
<div id="dados-complemento">
    <table  cellspacing=5>
    <tr>
    <td style=color:#f6eb8f width="20%">APLICAÇÃO</td>
    <td style=color:#f6eb8f width="20%">ANO</td>
    <td style=color:#f7e867 width="20%">COMPLEMENTO</td>
    </tr>
    <tr>
    <td><?php echo "$aplic[$i]" ?></td> 
    <td><?php echo "$anoif[$i]" ?></td> 
    <td><?php echo "$compl[$i]" ?></td> 
    </tr> 
    </table> 
</div> 
<?php
}
?>    
    
asked by anonymous 21.08.2017 / 12:27

1 answer

2

Loop working while

Imagine a table whose column is called "productsCofap" and the (->) arrow is the list pointer.

-> Amortecedores
   Baterias
   Bielas

When we use the while loop, it returns the first line:

   while ($row = mysqli_fetch_array($result_pd)) {
   $cfprod = $row['pro_cofap'];

At this point the $ cfprod variable takes the value Shock absorbs and the pointer moves forward, and our list looks like this:

   Amortecedores
-> Baterias
   Bielas

At this point the $ cfprod variable takes the Batteries value and the pointer moves one position forward, and so on.

  

So that's why in your while in the variable $cfprod , as well as all other variables, only the last record appears, because the variables are being overwritten.

Solution

while ($row = mysqli_fetch_array($result_pd)) {
   $rfprod = $row['pro_referencia_produto'];
   $approd = $row['pro_aplicacao_produto'];
   $fmprod = $row['pro_familia'];
   .................................
   .................................
}

When executing the while while shown above the values of the variables $rfprod , $approd , $fmprod etc .. are being overwritten because you are not concatenating values

.

You can for example concatenate (join) the values of $approd in the following ways:

using the concatenation operator point (.) before the equal sign (=)

$approd .= $row['pro_aplicacao_produto'];

or

$approd = $approd.$row['pro_aplicacao_produto'];

Of course, as it is above the names will all return together. It's up to you to decide how the $nome variable should be built for later presentation. For example:

'$approd .= "<li>" . $row['pro_aplicacao_produto'] . "</li>";'

and in the place that should be displayed for example:

echo "<ul>".$approd."</ul>";
    
21.08.2017 / 15:35