Ciao a tutti!
Ho un problema nel gestire un loop che recupera i dati con la classe per il template engine.
In pratica invece di vedere tutti i risultati visualizzo solo l'ultimo.
La classe del template engine è questa:
Codice PHP:
<?php
error_reporting(E_ALL);
class Pagina {
private $page;
public function __construct($file) {
$this->page = file_get_contents($file);
}
public function assegna($vars) {
if(is_array($vars)) {
foreach($vars as $key => $value) {
if(preg_match_all("/\{([a-zA-Z0-9\-\.\_]*?)\}/", $value, $regs)) {
foreach($regs[0] as $reg) {
$reg = str_replace(array("{", "}"), "", $reg);
$vars[$key] = str_replace("{" . $reg . "}", $vars[$reg], $vars[$key]);
}
}
$this->page = str_replace("{" . $key . "}", $value, $this->page);
}
}
}
public function stampa() {
return $this->page;
}
}
?>
E io la vado ad utilizzare così:
Codice PHP:
<?php
require("./template/engine.inc.php");
$glob = glob("./data/articolo_*.xml");
$body = new Pagina("./template/body.tpl");
$vars = array();
$body_cont = null;
for($i = count($glob) - 1; $i >= 0; $i--) {
$xml = simplexml_load_file($glob[$i]);
foreach($xml->articolo as $articolo) {
$data = date("d/m/y", intval($articolo->data));
$tags = str_replace("-", " ", $articolo->tags);
$vars = array(
"a-id" => $articolo["id"],
"a-titolo" => $articolo->titolo,
"a-autore" => $articolo->autore,
"a-data" => $data,
"a-testo" => $articolo->testo,
"a-tags" => $tags
);
$body->assegna($vars);
$body_cont .= $body->stampa();
}
}
echo $body_cont;
?>
Putroppo stampa tutti gli articoli identici all'ultimo estratto.
Non riesco a capire dove sbaglio :S