Display different objects with equal groups

2

I tried in several ways, got different results some good, others not so much. I would like to do something like store group / objects in MySQL and display on screen using PHP

I have this table

Iwouldliketodisplayaresultsimilartothis(madeinphotoshop):

SofarIhavethiscode:

<?php
   include("connect.php");
    $consulta = "SELECT * FROM Grupos";
    $con = $mysqli->query($consulta) or die($mysqli->error);

    while($dado = $con->fetch_array()){
        
for($Objeto =1; $Objeto <= 3; $Objeto++){
 echo $dado["$Objeto"] . "<br>";
   }     

}
?>

But it looks like this:

group1 10

20

group2 30

54

group3 2039

4433

    
asked by anonymous 29.09.2015 / 01:02

2 answers

3

1) To get the result you expect purely with SQL, you can use UNION. However, this option is not useful if you increase the number of objects. It responds to your need with 2 objects:

SELECT grupo, objeto1 FROM objetos
UNION
SELECT grupo, objeto2 FROM objetos

Proof: link

Result:

AnoptionwithJOIN,allowinginfinitegroupsandobjects.

2)RECOMMENDED:OptiondirectlysortingbySQL

link

SELECT g.grupo, o.objeto
FROM objetos o
LEFT JOIN grupos g ON g.id = o.grupoid
WHERE g.id IN (SELECT id FROM grupos ORDER BY grupo)

3) Option ordering by PHP

  • Not recommended, set out here as an example

Database structure (example): link

p>

The QUERY SQL returns grouped by groups, however the PHP code sort of the way you asked for the question.

WARNING: This code works as long as you always enter the same number of objects per group.

include("connect.php");

$consulta = "SELECT g.grupo, objeto 
FROM objetos o
LEFT JOIN grupos g ON g.id = o.grupoid";
$con = $mysqli->query($consulta) or die($mysqli->error);

$objetos = [];
$grupos = [];
$qtdObjetosGrupoKey = '';

foreach($con->fetch_array() as $linha) { 
    if(!in_array($linha['grupo'], $grupos)) {
        $grupos[] = $linha['grupo'];
    }

    if(!isset($qtdObjetos)) {
        $qtdObjetos = [$linha['grupo']] => 0;
        $qtdObjetosGrupoKey = $linha['grupo'];
    }

    if(!isset($objetos[$linha['grupo']])) {
        $objetos[$linha['grupo']] = [];

    }

    $objetos[$linha['grupo']][] = $linha['objeto'];

    if(isset($qtdObjetos[$linha['grupo']])) {
        $qtdObjetos[$linha['grupo']]++;
    }
}

$itensPorGrupo = $qtdObjetos[$qtdObjetosGrupoKey];
$itemPos = 0;

for($i = 0; $i < $itensPorGrupo; $i++) {
    foreach($grupos as $grupo) {
        $resultado[] = [
            'grupo' => $grupo,
            'objeto' => $objetos[$grupo][$itemPos]
        ];
        $itemPos++;
    }
}

print_r($resultado);
    
29.09.2015 / 01:42
0

Try this

<?php
include("connect.php");
$consulta = "SELECT * FROM Grupos";
$con = $mysqli->query($consulta) or die($mysqli->error);

foreach($con->fetch_array() as $linha) { 
    $grupo[] = $linha['Grupo'];
    $obj1[] = $linha['Objeto1'];
    $obj2[] = $linha['Objeto2'];
}

for ($i=0; $i<count($grupo);$i++) {
    echo $grupo[$i];
    echo $obj1[$i];
    echo $obj2[$i];
    echo "<br/>";
}
?> 
    
29.09.2015 / 01:36