I'm trying to loop where every 5 records in the row, skip a row and so on until it reaches 6 rows, when it reaches 6 rows for the loop.
Example:
12345
12345
12345
12345
12345
12345
Para o laço
Thank you
I'm trying to loop where every 5 records in the row, skip a row and so on until it reaches 6 rows, when it reaches 6 rows for the loop.
Example:
12345
12345
12345
12345
12345
12345
Para o laço
Thank you
You can do this using a condition with %
module. It would be something like.
if($item%5==0){
// insira o item + <br/> <-- onde br representa a quebra de linha
else
// insira o item normalmente
The logic is as follows:% operator %
means module in programming, which checks the rest of the division for a certain value. In this case it is thus, whenever the value of $item
is divided by 5
and its remainder is 0
, then do something. As in the example, the <BR/>
tag is placed to skip a line. See in the documentation .
Now see a complete code here using PDO. See:
<?php
// Instancia o objeto PDO
$pdo = new PDO('mysql:host=localhost;dbname=db', 'root', '');
// define para que o PDO lance exceções caso ocorra erros
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$consulta = $pdo->query("SELECT id FROM category;");
$item = 1;
while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
// aqui eu mostro os valores de minha consulta
if($item%5==0)
echo "{$linha['id']}<br/>";
else
echo "{$linha['id']}";
}
$item++;
}
?>