je savais pas trop où poster...
Bon en fait, ça fait envirron 2 jours (voire 3) que je galère sur un problème.
J'ai fait une classe qui construit la structure (balises tr et td) d'un tableau
html à partir d'un tableau php bidimentionnel (lignes, colonnes).
Le tout à l'air de fonctionner à l'exception de certaines configurations, par
exemple lorsque je met un colspan et un rowspan sur une même cellule.
Si je commence à tout expliquer ça va prendre trois pages alors je n'ai
pas d'autre solution que de vous claquer le code... désolé. Par cotnre, si
vous avez la moindre question je suis là !
J'espère, si ce code devient fonctionnel, pouvoir le poster dans les contributions.
Voila, assez parlé, voici les codes.
Page d'index, qui appèle les mathodes de la classe tpl qui va construire le tableau.
En base de données tout ce présente de la manière suivante :
une table avec dedans des entrées contenant les attributs suivants :
id titre ligne colonne nblignes nbcolonne
ligne et colonne correspondant aux coordonnées de la cellule et nblignes et nbcolonnes
à sa hauteur et sa largeur :
page d'index ici :
<style>
td{
border:1px solid #000;
width:50px;
height:50px;
}
</style>
<?php
require("./sql/connexion.php");
require("./class/tpl.php");
$sql = new SQL(); // Connexion à la bdd.
$tpl = new tpl(); // Création du template.
$id = (empty($_GET['id'])) ? 0 : intval($_GET['id']);
if($id == 0) // On affiche la liste de bâtiments.
{
// Récupération des extrema de la "boite".
$rExtrema = $sql->query("SELECT MAX(b.colonne+b.nbcolonnes), MAX(b.ligne+b.nblignes) FROM batiments AS b");
$extrema = $rExtrema->fetch(PDO::FETCH_NUM);
$cMax=$extrema[0]-1; // Dernière colonne.
$lMax=$extrema[1]-1; // Dernière ligne.
// Récupération des bâtiments.
$bat = $sql->query("SELECT b.* FROM batiments AS b ORDER BY b.ligne ASC");
$bat->setFetchMode(PDO::FETCH_ASSOC);
$tb = $tpl->openTab($lMax, $cMax); // Création d'un tableau de $lMax lignes et $cMax colonnes.
// Boucle de traitement.
while($array = $bat->fetch())
{
if($array['nbcolonnes'] == 1)
{
if($array['nblignes'] > 1)
{
$tpl->addRowSpan($array['ligne'], $array['colonne'], $array['nblignes']);
$tb[$array['ligne']][$array['colonne']] = $tpl->addImage($array['id']);
}
else
{
$tb[$array['ligne']][$array['colonne']] = $tpl->addImage($array['id']);
}
}
else
{
if($array['nblignes'] == 1)
{
$tpl->addColSpan($array['ligne'], $array['colonne'], $array['nbcolonnes']);
$tb[$array['ligne']][$array['colonne']] = $tpl->addImage($array['id']);
}
else
{
$tpl->addRowSpan($array['ligne'], $array['colonne'], $array['nblignes']);
$tpl->addColSpan($array['ligne'], $array['colonne'], $array['nbcolonnes']);
$tb[$array['ligne']][$array['colonne']] = $tpl->addImage($array['id']);
}
}
}
$bat->closeCursor();
$tpl->makeTab($tb);
$tpl->parse();
}
// Fermeture implicite de la connexion.
$sql = null;
?>
page de la classe template :
<?php
class tpl
{
// Déclaration des variables.
private $out = '';
private $tab = " ";
private $tabLine =0;
private $tbLignes=0;
private $tbCols = 0;
private $currentTag = array();
private $rowSpan = array();
private $colSpan = array();
private $rowSpanArray=array();
// Méthode permettant d'ajouter une image.
public function addImage($id)
{
return '<img src="'.$id.'.jpg" />';
}
// Méthode permettant d'ajouter un lien.
public function addLink($link, $text='')
{
if(strLen($text) == 0) $text = $link;
return '<a href="'.$link.'">'.$text.'</a>';
}
// Méthode d'initialisation d'un tableau.
public function initTab()
{
$array = array();
$rSA = array();
for($i = 1; $i <= $this->tbLignes; $i++)
{
for($j = 1; $j <= $this->tbCols; $j++)
{
$array[$i][$j] = ' ';
$rSA[$i][$j] = 0;
}
}
$this->rowSpanArray = $rSA;
return $array;
}
// Ouverture d'un tableau de $l lignes et $c colonnes.
public function openTab($l,$c)
{
$this->openTag("table");
$this->tbLignes = $l;
$this->tbCols = $c;
$this->tbLine = 0;
return $this->initTab();
}
// Ferme le tableau et efface les colSpan et rowSpan.
public function closeTab()
{
$endTab = array();
$this->closeTag();
$this->rmRowSpan();
$this->rmColSpan();
$this->tbLine = 1;
}
// Ajouter une ligne au tableau courant.
public function addRow2Tab($cols)
{
// Incrément de la ligne et ouverture de la balise tr.
$this->tabLine++;
$this->openTag("tr");
// Le bloc "if" qui suit sert à supprimer les balises "td" en trop s'il y a des colspan sur la ligne.
$cs = $this->colSpanSum();
$rs = $this->rowSpanSum();
if($cs > 0)
{
for($i = 1; $i <= $cs; $i++)
{
$this->deleteFromArray($cols, ' ');
}
}
if($rs > 0)
{
for($i = 1; $i <= $rs; $i++)
{
$this->deleteFromArray($cols, ' ');
}
}
// On parcours les différentes cellules de la ligne.
foreach($cols as $key => $val)
{
// Le bloc "if" qui suit traite les rowSpan et colSpan.
if($this->getRowSpan($key) === TRUE || $this->getColSpan($key) === TRUE)
{
if($this->getRowSpan($key) === TRUE && $this->getColSpan($key) === TRUE)
{
$this->openTag("td", "rowspan=".$this->curRowSpan($key)." colspan=".$this->curColSpan($key));
}
elseif($this->getRowSpan($key) === TRUE)
{
$this->openTag("td", "rowspan=".$this->curRowSpan($key));
}
else
{
$this->openTag("td", "colspan=".$this->curColSpan($key));
}
}
else
{
$this->openTag("td");
}
// Ajout du contenu de la cellule.
$this->addContent($val);
// Fermeture de la balise (td).
$this->closeTag();
}
// Fermeture de la balise tr.
$this->closeTag();
}
// Ajout d'un rowSpan.
public function addRowSpan($l, $c, $nb)
{
$this->rowSpan[$l][$c] = (int)$nb;
}
// Renvoie TRUE s'il y a un rowSpan dans la colonne $c de la ligne courante, FALSE sinon.
private function getRowSpan($c)
{
return ($this->curRowSpan($c)>0)? TRUE:FALSE;
}
// Renvoie le nombre de rowSpan de la cellule voulue.
private function curRowSpan($c)
{
return $this->rowSpan[$this->tabLine][$c];
}
// Remplissage du tableau de rowSpan.
private function fillRowSpanArray()
{
foreach($this->rowSpan as $ligne => $val)
{
foreach($this->rowSpan[$ligne] as $col => $nb)
{
for($i = 2; $i <= $nb; $i++)
{
$this->rowSpanArray[$ligne+$i-1][$col]++;
}
}
}
}
private function rowSpanSum()
{
if($this->rowSpanArray[$this->tabLine])
return array_sum($this->rowSpanArray[$this->tabLine]);
else
return 0;
}
// Suppression de tous les rowSpan.
private function rmRowSpan()
{
$this->rowSpan = array();
}
// Ajout d'un colSpan à la ligne $l et colonne $c.
public function addColSpan($l,$c,$nb)
{
$this->colSpan[$l][$c] = (int)$nb;
}
// Y'a t-il ou non un colSpan dans la cellule selectionnée.
private function getColSpan($c)
{
return ($this->curColSpan($c)>0)? TRUE:FALSE;
}
// Renvoie la valeur du colSpan de la cellule courante.
private function curColSpan($c)
{
return $this->colSpan[$this->tabLine][$c];
}
// Renvoie la somme de tous les colSpan de la ligne courante.
private function colSpanSum()
{
if($this->colSpan[$this->tabLine])
return (array_sum($this->colSpan[$this->tabLine]) - count($this->colSpan[$this->tabLine]));
else
return 0;
}
// Suppression de tous les colSpan.
private function rmColSpan()
{
$this->colSpan = array();
}
// Ouverture d'une balise.
private function openTag($tag, $args = '')
{
if(strLen($args)>0) $args=' '.$args;
array_push($this->currentTag, $tag);
$this->out.=str_repeat($this->tab, count($this->currentTag)-1).'<'.$tag.$args.'>'.chr(13);
}
// Ajout d'un contenu entre deux balises.
private function addContent($content)
{
$this->out.=str_repeat($this->tab, count($this->currentTag)).$content.chr(13);
}
private function deleteFromArray(&$array, $deleteIt)
{
$tmpArray = array();
$found = FALSE;
foreach($array as $key => $value)
{
if($value !== $deleteIt || $found === TRUE)
{
$tmpArray[$key] = $value;
}
else
{
$found = TRUE;
}
}
$array = $tmpArray;
return $found;
}
// Fermeture automatique du dernier tag ouvert.
private function closeTag()
{
$openTags = count($this->currentTag);
if($openTags>0)
$this->out.=str_repeat($this->tab, count($this->currentTag)-1).'</'.trim($this->currentTag[$openTags-1]).'>'.chr(13);
array_pop($this->currentTag);
}
// Création du tableau ligne par ligne.
public function makeTab($tb)
{
$this->fillRowSpanArray();
foreach($tb as $k => $v)
{
$this->addRow2Tab($v);
}
// Fermeture du tableau (balise...).
$this->closeTab();
}
// Ecriture de la sortie du moteur de templates.
public function parse()
{
echo $this->out;
}
}
?>
Merci d'avance et désolé pour la longueur du code.Jérémy.