Below are three scripts that create one page. The "TopNewsPageTwo.php" script creates an instance, the "HtmlTemplateClass.php" is the class used with the instance script, and the "TopNewsTemplateP2.php" template below displays the output in the browser.
My problem is this: How do I insert a "for" loop so that the output will display on the page (see "TopNewsTemplateP2.php" below for the desired positioning)?
The "for" loop that I'm needing to incorporate into the process is as follows:
$TextBoxes = "$Paragraphs";
for($i=1; $i<=$TextBoxes; $i++)
{
echo "<textarea rows=\"3\" cols=\"40\" name=\"Textfield\"" . "[$i]" ."></textarea><p>" . "\n";
}
What I would like to do is somehow incorporate the "for" loop in the "HtmlTemplateClass.php" class module, if possible, so that I may separate the PHP code from the HTML code.
Does anyone have any suggestions about how I might accomplish my task?
// -------- TopNewsPageTwo.php ---------------
// Creates an instance "TopNewsPageTwo.php"
// This page receives the "$Year"."$Month"."$Day"."$Number" variables from an input
// form on a previous page, checks them (ref. $CheckReference) to see if there is
// an existing news article in the "TopNews" table. If no article with the same
// reference number in the "TopNews" table then this page creates an instance.
require_once "./conf/config.inc.php";
$CheckReference = "$Year"."$Month"."$Day"."$Number";
$Date = "$Year"."$Month"."$Day";
$TableName = "TopNews";
$Query="SELECT Reference FROM $TableName";
$Result = mysql_db_query ($db_info[dbname], $Query, $db_connection);
while ($Row = mysql_fetch_array($Result))
{
if ($CheckReference === $Row["Reference"])
{
$TakenInUse="That news reference is currently in use. Please make another choice.";
}
}
mysql_close ($db_connection);
if (isset($TakenInUse))
{
echo $TakenInUse;
}else{ // If the $CheckReference variable does not exist in database do the following:
require_once "HtmlTemplateClass.php"; // Include the class.
$page = new HtmlTemplate(); // Create an instance.
$page->IdentifyTemplate ("TopNewsTemplateP2.php"); // Identify the template.
//----------------------
$page->SetParameter ("PAGE_TITLE", "Welcome to OOP!");
$page->SetParameter ("PAGE_CONTENT", "Here is the main part of the page.");
$page->SetParameter ("NEWS_REFERENCE_NUMBER", "$Year"."$Month"."$Day"."$Number");
$page->SetParameter ("MONTH", "$Month"); // Set the month.
$page->SetParameter ("DAY", "$Day"); // Set the day.
$page->SetParameter ("YEAR", "$Year"); // Set the year.
$page->SetParameter ("DATE", "$Year"."$Month"."$Day"); // Set the date.
//----------------------
$page->CreatePage(); // Send the page to the browser.
}
// -------- HtmlTemplateClass.php ---------------
// This is the class that is used with the "TopNewsPageTwo.php" script above.
class HtmlTemplate {
// Set the attributes.
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 () { // Read the template into an array, then create a string.
$this->Html = implode ("", (file($this->Template)));
// Loop through all the parameters and set the variables to values.
foreach ($this->Parameters as $Key => $Value) {
$TemplateName = '{' . $Key . '}';
$this->Html = str_replace ($TemplateName, $Value, $this->Html);
}
echo $this->Html;
}
}
// -------- TopNewsTemplateP2.php ---------------
// This is the template that is used with the "TopNewsPageTwo.php" instance
// and the "HtmlTemplateClass.php" class above.
<html>
<head>
<title>{PAGE_TITLE}</title>
</head>
<body><center>
<table border="1" cellspacing="5" cellpadding="5">
<tr><td><p>
<span class="text5">News Reference <b>{NEWS_REFERENCE_NUMBER}</b></span><p>
<form action="PageThree.php" method="post"><p>
<input type="hidden" name="Reference" value="{NEWS_REFERENCE_NUMBER}"><p>
<input type="hidden" name="Month" value="{MONTH}"><p>
<input type="hidden" name="Day" value="{DAY}"><p>
<input type="hidden" name="Year" value="{YEAR}"><p>
<input type="hidden" name="Date" value="{DATE}"><p>
// I need to insert the output from this loop here.
// The $Paragraphs variable is received from a form on a previous page.
$TextBoxes = "$Paragraphs";
for($i=1; $i<=$TextBoxes; $i++)
{
echo "<textarea rows=\"3\" cols=\"40\" name=\"Textfield\"" . "[$i]" ."></textarea><p>" . "\n";
}
// Insert output from loop immediately above.
</form></td></tr><p>
</table>
</center></body>
</html>
//-------------------------
Thanks.
Volitics