I get this code from http://www.phpbuilder.com/columns/lep20020402.php3?page=1 (list pages 1 - 5) and cant get it working, my template is copyed from this tutorial too, I only changed variable $data_array to $recordset_array in some places in function template_parser(), because I think, thats mistake. The main problem can be in the reading of data from mysql, I the output of the script shows only first letters of names in the output table. I dont know, how to correctly load the data to the recordset_array. My sql table data has two columns, name and surname.
<?php
include("./common.php");
$linkid = db_connect();
if (!$linkid) error_message(sql_error());
$template_filename = "TEMPLATE.html";
$adresy=MySql_Query("Select * from data");
$recordset_array=MySql_fetch_array($adresy);
/* =============================================================== */
/* ====================== Replacement Rules ====================== */
/* =============================================================== */
$replacement_rules = array (
array ("<!--NAME-DATA-->", "0"),
array ("<!--ADDRESS-DATA-->", "1")
);
/* =============================================================== */
function template_parser ($template_filename, $replacement_rules, $recordset_array)
{
$START_FLAG = 0;
$start_anchor=0;
$end_anchor=0;
$res_arr="";
$fcontents = file ($template_filename);
while (list ($line_num, $line) = each ($fcontents))
{
if ($START_FLAG == 0)
{
if (ereg ('<!--START_ROW-->', $line))
{
$START_FLAG = 1;
$start_anchor = $line_num;
}
}
else
{
if (!ereg ('<!--END_ROW-->', $line))
{
$res_arr .= $line;
}
else
{
$START_FLAG = 0;
$end_anchor = $line_num;
}
}
}
/* Build String-Replacement Rules ... */
$tmp_res_arr = "";
$n = count($recordset_array);
for ($x=0; $x<$n; $x++)
{
$tmp = $res_arr;
for ($y=0; $y<count($replacement_rules); $y++)
{
$a = $replacement_rules[$y][0];
$b = $replacement_rules[$y][1];
if (ereg ("<!", $a))
{
$recordset_array[$x][$b] = stripslashes ($recordset_array[$x][$b]);
eval ("\$tmp = str_replace (\"$a\", \$recordset_array[\$x][$b], \$tmp);");
$tmp = stripslashes($tmp);
}
}
$tmp_res_arr .= $tmp;
}
$res_arr = $tmp_res_arr;
/* Re-constructing the file-contents ... */
$n = count($fcontents);
for ($x=0; $x<$n; $x++)
{
$y = $start_anchor + ($x - $end_anchor) ;
if (($x >= $start_anchor) and ($x < $end_anchor))
{
if ($n_fcontents[$start_anchor] == "")
$n_fcontents[$x] = $res_arr;
}
elseif ($x > $end_anchor)
$n_fcontents[$y] = $fcontents[$x];
else
$n_fcontents[$x] = $fcontents[$x];
}
return ($n_fcontents);
} // end function template_parser
$n_fcontents = template_parser ($template_filename, $replacement_rules, $recordset_array);
while (list ($line_num, $line) = each ($n_fcontents))
{
echo "$line";
}
?>