The first and the second template variable ($page_title and $letter) are set in
samplate2.php (executive file), but not the third(rows.addresses), because that variable cannot be predefined.
It get its value, after processing in address_sql function (WHILE loop).
So, first 2 variables are replaced, and third is not.
I think the defect of this code is that the $complete_table variable (from address_sql) is not forwarded to WHILE loop in file_parser function (all from template.class) for replacement of
3.template variable {$rows.addresses} (which is the name of corresponding template file).
I need to make rows.addresses global, something like:
PHP Code:
VAR $rows.addresses = '';
in the global part of template.class
and subsequently:
PHP Code:
. . .
$complete_table .= $new_row;
endwhile;
$rows.addresses = $complete_table;
but this doesn't work (blank page), so I'm wondering, is the rows.addresses a valid variable name ?
Well, what can I do to forward $complete_table to replacement loop (file_parser function) but with the name 'rows.addresses' because this is the name of template variable (also of corresponding template file).
template.class
PHP Code:
<?php
class template {
VAR $files = array();
VAR $variables = array();
VAR $sql = array();
VAR $opening_escape = '{';
VAR $closing_escape = '}';
VAR $host = "localhost";
VAR $user = "root";
VAR $pswd = "123456";
VAR $db = "book";
VAR $address_table = "addressbook";
VAR $rows.addresses = '';
function address_sql($file_id, $variable_name, $letter) {
// Connect to MySQL server and select database
mysql_connect($this->host, $this->user, $this->pswd)
or die("Couldn't connect to MySQL server!");
mysql_select_db($this->db) or die("Couldn't select MySQL database!");
// Query database
$query = "SELECT last_name, first_name, tel, email
FROM $this->address_table WHERE last_name LIKE '$letter%'";
$result = mysql_query($query);
// Open "rows.addresses" file and read contents into variable.
$fh = fopen("$variable_name", "r");
$file_contents = fread($fh, filesize("rows.addresses") );
// Perform replacements of delimited variable names with table data
while ($row = mysql_fetch_array($result)) :
$new_row = $file_contents;
// add the key to the variables array for later lookup
$this->variables[$file_id][] = $variable_name;
// Close the filehandle
fclose($fh);
} // end address_sql
// Function: register_file()
// Purpose: Store contents of file specified by $file_id
function register_file($file_id, $file_name) {
// Open $file_name for reading, or exit and print an error message.
$fh = fopen($file_name, "r") or die("Couldn't open $file_name!");
// Read in the entire contents of $file_name.
$file_contents = fread($fh, filesize($file_name));
// Assign these contents to a position in the array.
// This position is denoted by the key $file_id
$this->files[$file_id] = $file_contents;
// We're finished with the file, so close it.
fclose($fh);
} // end register_file;
// Function: register_variables()
// Purpose: Store variables passed in via $variable_name under the corresponding
// array key, specified by $file_id
function register_variables($file_id, $variable_name) {
// attempt to create array from passed in variable names
$input_variables = explode(",", $variable_name);
// assign variable name to next position in $file_id array
while (list(,$value) = each($input_variables)) :
// assign the value to a new position in the $this->variables array
$this->variables[$file_id][] = $value;
endwhile;
} // end register_variables
// Function: file_parser()
// Purpose: Parse all registered variables in file contents
// specified by input parameter $file_id
function file_parser($file_id) {
// How many variables are registered for this particular file?
$varcount = count($this->variables[$file_id]);
// How many files are registered?
$keys = array_keys($this->files);
// If the $file_id exists in the $this->files array and it
// has some registered variables…
if ( (in_array($file_id, $keys)) && ($varcount > 0) ) :
// reset $x
$x = 0;
// while there are still variables to parse…
while ($x < sizeof($this->variables[$file_id])) :
// retrieve the next variable
$string = $this->variables[$file_id][$x];
// retrieve this variable value! Notice that I'm using a
// variable variable to retrieve this value. This value
// will be substituted into the file contents, taking the place
// of the corresponding variable
// name.
GLOBAL $$string;
// What exactly is to be replaced in the file contents?
$needle = $this->opening_escape.$string.$this->closing_escape;
// Perform the string replacement.
$this->files[$file_id] = str_replace(
$needle, // needle
$$string, // string
$this->files[$file_id]); // haystack
// increment $x
$x++;
endwhile;
endif;
} // end file_parser
// Function: print_file()
// Purpose: Print out the file contents specified by input parameter $file_Id
function print_file($file_id) {
// print out the contents pointed to by $file_id
print $this->files[$file_id];
}
} // end template.class
?>