how to group mysql while result inside php table

Asked

Viewed 527 times

0

Good afternoon someone can give me a light I’m trying to organize the data inside the while, Today I can take the data and put it in the normal table only my problem is when there are many items within a volume so it is repeated a lot of time the name of the volume, weight and dimension. The idea would be to group the volume name weight and dimension and repeater results and make one with the amount of information grouped as in the attached photo. inserir a descrição da imagem aqui

  • What programming language will you use for that?

  • php usage friend and mysql

  • Okay, I added that tag to the question to make it clearer.

  • You can share the code you already have?

  • 1

    tried to use a distinct com group by no select?

  • <? while ($line = mysql_fetch_array($sql_vol)) { ? > <tr> <! -- Start list volume items --> <td width="auto"><? echo $line ['pro_cod'];? ></td> <td width="auto"><? echo $line ['pro_nome'];? ><br><font class="desc"><i><<<? echo $line ['pro_descricao'];? ></i></font></td> <td width="auto"><? echo $line ['vi_quantidade'];? ></td> <td width="auto"><? echo $line ['med_name'];? ></td> <td width="auto" rowspan="""><? echo $line ['vol_name'];? ></td> <td width="auto" rowspan=""><? echo $line ['vol_peso'];? > kg</td>

  • (SELECT &#xA;produto.pro_cod,&#xA;produto.pro_nome,&#xA;produto.pro_descricao,&#xA;SUM(volume_itens.vi_quantidade) AS vi_quantidade,&#xA;medida.med_nome,&#xA;volume.vol_id,&#xA;volume.vol_nome,&#xA;volume.vol_peso,&#xA;volume.vol_altura,&#xA;volume.vol_largura,&#xA;volume.vol_comprimento&#xA;FROM volume&#xA;LEFT JOIN volume_itens ON volume.vol_id = volume_itens.vi_vol_id&#xA;LEFT JOIN produto ON volume_itens.vi_pro_id = produto.pro_id&#xA;LEFT JOIN medida ON produto.pro_med_id = medida.med_id&#xA;WHERE vol_sai_id = '$id'&#xA;GROUP BY pro_id, vol_name ORDER BY vol_name AND pro_name ASC)

  • I tried but it didn’t work

  • and as distinct?

  • no chance I tried.... in reality it is happening like this

  • will not already tried.... is pulling the right db information. my priblema and in the game hr on the screen q I want to organize in the picture while pulls 3 resolutions so id, Cod, product, Qtd, un, volume, weight and dimension 1, 10, product 1, 100, Un, box 1, 10kg, 50 x 43 x 55 cm 2, 13, wwww, 1, Un, box 1, 10kg, 50 x 43 x 55 cm 3, 10, product 1, 200, Un, Box 2, 15kg, 23 x 54 x 82 cm .

  • Can you post the HTML of one of the images to know which style is applied to the table? Class, id etc...

  • The weight of box 1 should not be 20 kg ?

Show 8 more comments

1 answer

3

Example table

Tabela de exemplo

PHP code

$td1="";
$td2="";
$td3="";
$Linha="";
$linhaFim="";
$result="";
$pesoOld="";
$dimensaoOld="";
$dimensaoOld2="";
$z="1";

$link = new mysqli ("localhost", "USUARIO", "SENHA", "NOME_DB");

$sqlm = ("SELECT * FROM dados order by id asc");

$resultado = mysqli_query($link,$sqlm);

echo "<table border=\"1\"><tr><th>Cod.:</th><th>Produto</th><th>Qtd.:</th><th>Un.:</th><th>Volume:</th><th>Peso:</th><th>Dimensao: A.L.C.</th></tr>\n";

while($row = mysqli_fetch_assoc($resultado)) {
 $cod=$row["cod"];
  $produto=$row["produto"];
   $qtd=$row["qtd"];
    $un=$row["un"];
     $volume=$row["volume"];
      $peso=$row["peso"];
       $dimensao=$row["dimensao"];

        $Linha = "<tr>\n<td>".$cod."</td>\n<td>".$produto."</td>\n<td>".$qtd."</td>\n<td>".$un."</td>\n";

        $td1 = "<td>".$volume."</td>\n";

        $td2 = "<td class=".$volume.">".$peso."</td>\n";

        $td3 = "<td class=".$volume.">".$dimensao."</td>\n";    

        $linhaFim = "\n</tr>\n";

        $result .= $Linha.$td1.$td2.$td3.$linhaFim;   
}

$ocorrencias = substr_count($result,"<td>Caixa");

for ($k = 1; $k < $ocorrencias; $k++) {
    $num = substr_count($result,"<td>Caixa ".$k."</td>");
    if ($num > 1){

        $result =  preg_replace("#<td>Caixa ".$k."</td>#", "<td rowspan=\"".$num."\">Caixa ".$k."</td>", $result,1);
        $result =  preg_replace("#<td>Caixa ".$k."</td>#", "", $result,$num-1);
        $volume="Caixa ".$k;

        $consultar = "SELECT volume,dimensao, peso, SUM(peso) AS subt FROM (SELECT volume,dimensao,peso FROM dados Where volume='$volume' ORDER BY id ASC LIMIT $num) AS tabelaSubt";
        $resulta = mysqli_query($link, $consultar);

            while($item = mysqli_fetch_assoc($resulta)){
                $peso = $item["peso"];
                $subt = $item['subt'];
                $dimensao = $item['dimensao'];
                $volume = $item['volume'];

                $dimensaoTot="23 x 54 x 82 cm";

                //volume para 2 itens de 23 x 54 x 82 cm
                $dimensaoTot2="?? x ?? x ?? cm";

                $result =  preg_replace("#<td class=".$volume.">".$peso."</td>#", "<td rowspan=\"".$num."\">".$subt." Kg</td>", $result,1);
                $result =  preg_replace("#<td class=".$volume.">".$peso."</td>#", "", $result,$num-1);
                $result =  preg_replace("#<td class=".$volume.">".$pesoOld."</td>#", "", $result,$num-1);

                $result = preg_replace("#<td class=".$volume.">50 x 43 x 55 cm</td>#", "<td rowspan=\"".$num."\">".$dimensaoTot."</td>", $result,1);
                $result = preg_replace("#<td class=".$volume.">50 x 43 x 55 cm</td>#", "", $result,$num-1);
                $result = preg_replace("#<td class=".$volume.">".$dimensaoOld."</td>#", "", $result,1);

                $result = preg_replace("#<td class=".$volume.">23 x 54 x 82 cm</td>#", "<td rowspan=\"".$num."\">".$dimensaoTot2."</td>", $result,1);
                $result = preg_replace("#<td class=".$volume.">23 x 54 x 82 cm</td>#", "", $result,$num-1);
                $result = preg_replace("#<td class=".$volume.">".$dimensaoOld2."</td>#", "", $result,1);

                $pesoOld=$peso;
                $dimensaoOld=$dimensao;
                $dimensaoOld2=$dimensao;
            }
    }
}

echo $result;
echo "</table>";    

This is a basic routine that can be incremented according to needs since the question did not give all possible variations

Browser other questions tagged

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