The HtmlTemplate.class.php page below is the class that processes the HTML and inserts the $var variables into the {$var} tags on the template page. The CreateInstancePage.php page below incorporates the HtmlTemplate class, identifies the template to be used, extracts the data from the database, sets the parameters for the {$var} tages on the TemplatePage.inc.php page, and then creates the page.
I got the concept for the code below from the book "PHP Advanced For The World Wide Web" written by Larry Ullman. It all works fine except for one little hitch - the $Page->SetParameter ("COUNTY", "$Reference"); on the CreateInstancePage.php below won't iterate through the "for" loop. It will output one time and that's it - it won't loop through and output any more.
I'm just now starting to learn about classes and templates. As far as I can tell this is a "nested" loop or array problem. That is, the "for" loop on the CreateInstancePage.php is nested within the "foreach ($this->Parameters as $Key => $Value)" loop within the CreatePage function on the HtmlTemplate.class.php page. A nexted loop may not be possible with PHP.
Can anyone spot the problem and provide insight about how the problem might be solved?
//---------- HtmlTemplate.class.php -------------------
class HtmlTemplate {
// Set the attributes or properties.
var $Template;
var $Html;
var $Parameters = array();
function IdentifyTemplate ($Template) { // This function sets which template will be used.
$this->Template = $Template;
}
function SetParameter ($Variable, $Value) { // This function sets the particular values.
$this->Parameters[$Variable] = $Value;
}
function CreatePage () { // This function does the bulk of the work.
$this->Html = implode ("", (file($this->Template))); // Read the template into an array, then create a string.
foreach ($this->Parameters as $Key => $Value) { // Loop through all the parameters and set the variables to values.
$TemplateName = "{" . $Key . "}";
$this->Html = str_replace ($TemplateName, $Value, $this->Html);
}
echo $this->Html;
}
}
//------------ CreateInstancePage.php
require_once "HtmlTemplate.class.php"; // Include the class.
$Page = new HtmlTemplate(); // Create an instance.
$Page->IdentifyTemplate ("TemplatePage.inc.php"); // Identify the template.
$Page->SetParameter ("DIRECTORY_PAGE", "$PHP_SELF");
require_once "../quote/conf/config.inc.php";
$TableName = "State"."$State";
$Result = mysql_query("SELECT * FROM $TableName", $db_connection);
for($i=17; $i<=mysql_num_fields($Result); $i++){
$Reference = mysql_field_name($Result, $i);//Works ok here. Loops through and will echo (62) counties.
$Page->SetParameter ("COUNTY", "$Reference");//Here's where the problem is. Won't loop through and output.
}
$Page->CreatePage(); // Send the page to the browser.
mysql_close( $db_connection );
//------------- TemplatePage.inc.php
<form method="post" action="{DIRECTORY_PAGE}">
<select name="County">
<option value="{COUNTY}">{COUNTY}</option>//Needs to iterate or loop through (62) counties.
</select>
<input type="hidden" name ="State" value="">
<input type="submit" name="SetCountyState" value="Click Here To Proceed">
</form>
Thank you in advance.
Volitics