Composite key, shows in the query only the last record

Asked

Viewed 60 times

-2

$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 reads the last record of the add-on table. I inserted the array, but it didn’t 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
}
?>    
  • Enter the code for analysis.

  • How many records do you have on this table with the reference passed by parameter ?

  • @Paulo Ladeira Inside the while vc has to pass the values to an array. Otherwise, it will always be the last value.

  • Paulo Ladeira, I believe that it is better to remake your question, so that it is not negative, several times!

1 answer

2

Functioning of the while loop

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

-> Amortecedores
   Baterias
   Bielas

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

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

At this time the variable $cfprod takes on the value Shock absorbers and the pointer moves one position forward, and our list is as follows:

   Amortecedores
-> Baterias
   Bielas

At this time the variable $cfprod takes on the value Batteries 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 shown above the values of the variables $rfprod, $approd, $fmprod etc.. are being overwritten because you is not concatenating the values.

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

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

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

or

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

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

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

and in the place that should be presented for example:

echo "<ul>".$approd."</ul>";
  • OK Thanks! It was logic problem. It worked already!

  • @Pauloladeira takes advantage and read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

Browser other questions tagged

You are not signed in. Login or sign up in order to post.