Buenos dias a todos!!
Pues bien, mi problema es el siguiente:
En una base de datos "X" tengo tres tablas "A, B, y C", con 20 datos cada uno, y necesito hacer una consulta para mostrar el contenido de A y de B, pero utilizando un template.
Este es class ke uso. (classTemplate.php)
Código PHP:
<?php
class Template {
var $vars; /// Holds all the template variables
function Template($file = null) {
$this->file = $file;
}
function set($name, $value) {
$this->vars[$name] = is_object($value) ? $value->fetch() : $value;
}
function fetch($file = null) {
if(!$file) $file = $this->file;
extract($this->vars); // Extract the vars to local namespace
ob_start(); // Start output buffering
include($file); // Include the file
$contents = ob_get_contents(); // Get the contents of the buffer
ob_end_clean(); // End buffering and discard
return $contents; // Return the contents
}
}
class CachedTemplate extends Template {
var $cache_id;
var $expire;
var $cached;
function CachedTemplate($cache_id = null, $expire = 900) {
$this->Template();
$this->cache_id = $cache_id ? 'cache/' . md5($cache_id) : $cache_id;
$this->expire = $expire;
}
function is_cached() {
if($this->cached) return true;
// Passed a cache_id?
if(!$this->cache_id) return false;
// Cache file exists?
if(!file_exists($this->cache_id)) return false;
// Can get the time of the file?
if(!($mtime = filemtime($this->cache_id))) return false;
// Cache expired?
if(($mtime + $this->expire) < time()) {
@unlink($this->cache_id);
return false;
}
else {
$this->cached = true;
return true;
}
}
function fetch_cache($file) {
if($this->is_cached()) {
$fp = @fopen($this->cache_id, 'r');
$contents = fread($fp, filesize($this->cache_id));
fclose($fp);
return $contents;
}
else {
$contents = $this->fetch($file);
// Write the cache
if($fp = @fopen($this->cache_id, 'w')) {
fwrite($fp, $contents);
fclose($fp);
}
else {
die('Unable to write cache.');
}
return $contents;
}
}
}
?>
Este es el template x ejemplo (index.tpl)
Código HTML:
<html>
<head>
<title><?=$title;?></title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?=$content;?>
</body>
</html>
y el index.php
Código PHP:
<?php
require_once('classTemplate.php');
//requiere('db.php');
$tpl = & new Template('index.tpl'); // this is the outer template
$tpl->set('title', 'User List');
// Aca la consulta para la base de datos
echo $tpl->fetch('index.tpl');
?>
Lo ke sucede es ke he probado con muchisimos bucles, y aun no puedo hacerlo funcionar!!!
si hago un foreach en index.tpl, me aparece solo el primer caracter de la tabla repetido el numero de veces de datos ke hay.... osea ke si hay 20 datos.... y son dos tablas.... 40 veces...
aun no me explico si el error esta en la forma en la ke hago la consulta (while) o estoy haciendo algo mal con el bucle...
Si alguien puede ayudarme, gracias!!!