par
Genova » 17 janv. 2008, 18:20
Bonjour,
je déteste ce genre d'affichage moi aussi, je passe mon temps à m'embrouiller entre les <tr> à ajouter ou pas.
Mais il y a un moyen simple : il suffit déjà de réorganiser ton tableau en PHP, en faisant un tableau à deux dimensions comme ceci :
<?php
// On considère ce tableau initial
$tab = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Combien de "cellules" par ligne
$cell = 3;
// Réorganisation en tableau à deux dimensions
$count = count($tab);
$new_tab = array();
for ($i = 0; $i < $count; $i++)
{
$new_tab[floor($i / $cell)][] = $tab[$i];
}
// Structure de ton tableau réorganisé ($new_tab)
echo '<pre>';
print_r($new_tab);
?>
tu obtiens en sortie un tableau à deux dimensions donc :
Code : Tout sélectionner
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
[3] => Array
(
[0] => 10
)
)
Après c'est un jeu d'enfant de l'afficher, il suffit d'avoir un code du genre, et ça marche à tous les coups :
echo '<table width="100%" border="1">';
foreach ($new_tab AS $ligne)
{
echo '<tr>';
foreach ($ligne AS $col)
{
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';
le tout sans se prendre la tête avec des modulos

Bonjour,
je déteste ce genre d'affichage moi aussi, je passe mon temps à m'embrouiller entre les <tr> à ajouter ou pas.
Mais il y a un moyen simple : il suffit déjà de réorganiser ton tableau en PHP, en faisant un tableau à deux dimensions comme ceci :
[php]<?php
// On considère ce tableau initial
$tab = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Combien de "cellules" par ligne
$cell = 3;
// Réorganisation en tableau à deux dimensions
$count = count($tab);
$new_tab = array();
for ($i = 0; $i < $count; $i++)
{
$new_tab[floor($i / $cell)][] = $tab[$i];
}
// Structure de ton tableau réorganisé ($new_tab)
echo '<pre>';
print_r($new_tab);
?>[/php]
tu obtiens en sortie un tableau à deux dimensions donc :
[code]Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
[3] => Array
(
[0] => 10
)
)[/code]
Après c'est un jeu d'enfant de l'afficher, il suffit d'avoir un code du genre, et ça marche à tous les coups :
[php]echo '<table width="100%" border="1">';
foreach ($new_tab AS $ligne)
{
echo '<tr>';
foreach ($ligne AS $col)
{
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';[/php]
le tout sans se prendre la tête avec des modulos :D