Block splitting mysql results

Asked

Viewed 197 times

1

I have a system in which I register the number of people in a given establishment that goes from 1 to 200 and each establishment holds up to 3 people. How I would make it so that when I bring this amount from mysql, it is divided into blocks. For example.:

As I’m currently bringing

<?php
....
$c = 1;
while($jm = mysqli_fetch_object($sqlMostrar)){
   $mostrar .= "Estabelecimento " . $c . "<br>";
   $mostrar .= $jm->Nome. " " .$jm->Idade;
 $c++; 
}

This way it brings rushed and not in blocks. I would like to bring this result as follows. Let’s assume that the client registered 09 people:

**Estabelecimento 1**
Fernando Pessoa 32 anos
Ruth Cardoso 56 anos
Santos Dumont 60 anos

**Estabelecimento 2**
Carlos Drummond de Andrade 70 anos
Mario Lago 72 anos
Manuel Bandeira 60 anos

**Estabelecimento 3**
Olavo Bilac 22 anos
Cecília Meireles 25 anos
Gonçalves Dias 55 anos

2 answers

2


I do not know if for your case this would be advantageous because of performance, but what could be done is to divide the result through function array_chunk. For that we would have to use Mysqli::fetch_all, instead of fetch.

$chunks = array_chunk(mysqli_fetch_all($sql), 3)

And then:

<?php foreach ($chunks as $key => $results) : ?>
   Estabelecimento <?= $key+1?>
   <?php foreach ($results as $row) : ?>
      ...
   <?php endforeach ?>
<?php endforeach ?>

Another solution is to check in a loop that the number is divisible by three, in order to generate this "3-in-three jump".

Example:

$c = 1;
$d = 0;
while($jm = mysqli_fetch_object($sqlMostrar)){

   if ($d++ % 3 == 0) {
      $mostrar .= "Estabelecimento " . $c . "<br>";
      $c++;
   }

   $mostrar .= $jm->Nome. " " .$jm->Idade;
}
  • Hi Wallace, I used your code, but it gives the error Call to Undefined Function mysqli_fetch_all(). The php version is 5.5.29. Is there any incompatibility?

  • Hi Wallace. Unfortunately this last example of yours didn’t work either. It doesn’t show the result after if parole().

  • 1

    For the first question: Yes, the error occurs because you are using a version less than 5.3 of PHP.

  • 1

    For the second question: I used your sample code. Make sure you started the variable $mostrar before the loop.

  • There is an error in the code: The $d has to be 0

  • I put $contar = $jmListar->Qtdpax; for($cc = 0; $cc < $count; $cc++) and it worked correctly.

  • Just one more question to close. Let’s assume that the blocks are divided into 2 and in the bank bring 5, automatically the last field will contain only one. How would I make even with the difference, it closes with 02 blocks?

Show 2 more comments

1

I don’t quite understand, but I’m going to assume that the people register is sequential, and you’re simply distributing people.
Nor has it been defined that the break is vertical or horizontal.

# SIMULA AS PESSOA
$pessoa = array();
for ($i=0; $i < 9; $i++){ 
    $pessoa[] = array(
        'nome' => "Pessoa {$i}",
    );
}

# CRIA ARRAY HIPOTIPÓTICO
$estabelecimento = array();
$estabelecimento2 = array();
$x = 0;
foreach ($pessoa as $k => $options){
    # QUEBRA HORIZONTAL
    $estabelecimento[$k%3][] = $options;

    # QUEBRA VERTIVAL
    if($k%3 == 0){
        $estabelecimento2[++$x][] = $options;
    }else{
        $estabelecimento2[$x][] = $options;
    }
}

echo '<pre>';
print_r($estabelecimento);
print_r($estabelecimento2);

Browser other questions tagged

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