evo saljem sve kodove pa ko voli nek izvoli:
parser.class.php
Code:
<?php
//Make sure no one tries to access this file directly
if(!defined("ROOT"))
exit();
class parser {
var $params;
var $path = "templates/";
function parser($parameters) {
$this->params = $parameters;
}
function setParams($params) {
$this->params = $params;
}
function parsePage($fileName) {
$return='';
$file = fopen($this->path.$fileName,"r");
if (!$file) {
error_log("File not found $fileName");
return "";
}
while (! feof($file)) {
$line = fgets($file,2048);
/*if (preg_match('/\[%([\w\-]+?)%\]/e', $line))
{
foreach ($this->params as $key => $value)
{
}
}*/
$return .= preg_replace('/\[%([\w\-]+?)%\]/e',"isset (\$this->params['\\1']) ? \$this->params['\\1'] : \"\"",$line);
}
return $return;
}
/**
* Function for parsing a table.
*
* @param string $tableLabel name of table
* @param 2D array $arr_data data to be added
* @param string $strToParse string for parsing
*/
function parseTable($tableLabel, $arr_data, $strToParse)
{
$doc = new DOMDocument();
if (!$doc->loadHTML($strToParse))
{
echo "ERROR LOADING HTML STRING";
exit();
}
$xpath = new DOMXPath($doc);
$hrefs = $xpath->evaluate("/html/body//table");
foreach ($hrefs as $value)
{
$attribute = $value->getAttribute("name");
if ($attribute != $tableLabel)
continue;
$tr = $value->getElementsByTagName("tr");
$trLength = $tr->length;
$arr_dataLength = count($arr_data);
$indexTr = 0;
while($trLength < $arr_dataLength )
{
$node = $tr->item($indexTr++)->cloneNode(true);
$value->appendChild($node);
$trLength++;
}
while($trLength > $arr_dataLength )
{
$node = $tr->item($trLength-1);
$value->removeChild($node);
$trLength--;
}
$indexTr = 0;
foreach ($arr_data as $dataValues)
{
$trChildNodes = $tr->item($indexTr)->childNodes;
$dataValues = array_values($dataValues);
for ($indexTrChildNodes = 0;($indexTrChildNodes < $trChildNodes->length) &&
($trChildNodes->item($indexTrChildNodes)->nodeName != "#text"); $indexTrChildNodes++)
{
$trChildNodes->item($indexTrChildNodes)->nodeValue = $dataValues[$indexTrChildNodes];
}
$indexTr++;
}
}
$output = $doc->saveHTML();
echo $output;
}
}
?>
search.php
Code:
<?php
//header('Content-type: text/plain');
require_once("../cfg.inc/include.php");
//print_r($_GET);
//exit();
extract($_GET);
$db_handler = new db_handler();
$smarty = new Smarty();
if ($check_bean_info == "false" && $check_roast_info == "false" && $check_blend_info == "false" && $check_taste_info == "false")
{
exit("-1");
}
//bean info
if ($check_bean_info == "true")
{
$sql_where = "";
foreach ($_GET as $key=>$value)
{
$pos = strpos($key, "1_");
if ($pos === false)
continue;
$table_column_name = str_replace("1_", "", $key);
if ($value != "") $sql_where .= $table_column_name . " like '%" . $value . "%' and ";
}
$sql_where = trim($sql_where, " and ");
$sql_query = sprintf("select id, bean_name, archival, share, notes, wholesaler, crop_year, purchase_year,
supplier, product_weight, shipping_weight, sale_price, bean_country, the_lot, member_id, ins_time
from
w2c_beans where %s", $sql_where);
$result = $db_handler->get_result_db($sql_query);
while($rs = mysql_fetch_assoc($result))
{
$items[] = $rs;
}
}
//creating output
$template = "search_results_roast.tpl";
$param = array();
$parser = new parser($param);
$parser->path = "../templates/";
$strToParse = $parser->parsePage($template);
$parser->parseTable("search_results_1", $items, $strToParse);
//echo $strToParse;
?>
search_results_roast.tpl
Code:
<form name="search_frm" id="search_frm" method="get" action="modules/search.php" onsubmit=" submit_search_frm(); return false;">
<table align="center" width="1000px;" bgcolor="#5E3718" bordercolor="#F19D3D" border="0">
<tr>
<td align="center" style="font: bold 12px Verdana; padding-top:1em;">
SEARCH DATABASE FOR PARTICULAR BEAN-ROAST-BLEND combination</td>
</tr>
<tr>
<td>
<table border=1 name="search_results_1">
<tr>
<td bgcolor=silver class='medium'>id</td>
<td bgcolor=silver class='medium'>bean_name</td>
<td bgcolor=silver class='medium'>archival</td>
<td bgcolor=silver class='medium'>share</td>
<td bgcolor=silver class='medium'>notes</td>
<td bgcolor=silver class='medium'>wholesaler</td>
<td bgcolor=silver class='medium'>crop_year</td>
<td bgcolor=silver class='medium'>purchase_year</td>
<td bgcolor=silver class='medium'>supplier</td>
<td bgcolor=silver class='medium'>product_weight</td>
<td bgcolor=silver class='medium'>shipping_weight</td>
<td bgcolor=silver class='medium'>sale_price</td>
<td bgcolor=silver class='medium'>bean_country</td>
<td bgcolor=silver class='medium'>the_lot</td>
<td bgcolor=silver class='medium'>member_id</td>
<td bgcolor=silver class='medium'>ins_time</td></tr>
<tr>
<td class='normal' valign='top'>311</td>
<td class='normal' valign='top'>test_serch_bean_wert_23</td>
<td class='normal' valign='top'>0</td>
<td class='normal' valign='top'>1</td>
<td class='normal' valign='top'> </td>
<td class='normal' valign='top'>test</td>
<td class='normal' valign='top'>2008</td>
<td class='normal' valign='top'>2008</td>
<td class='normal' valign='top'>test</td>
<td class='normal' valign='top'>200kg</td>
<td class='normal' valign='top'>200kg</td>
<td class='normal' valign='top'>(NULL)</td>
<td class='normal' valign='top'>Brasil</td>
<td class='normal' valign='top'>(NULL)</td>
<td class='normal' valign='top'>8</td>
<td class='normal' valign='top'>2008-05-14 05:50:12</td>
</tr>
</table></td>
</tr>
</table></form>
Ja stranicu search.php pozivam preko ajax.
Ta stranica nalazi rezultate u bazi i na osnovu rezultata i search_results_roast.tpl generise html tabelu.
Parsira search_results_roast.tpl uzimajuci obe vrste iz tabele na toj stranici i generise novu tabelu samo sa rezultatima iz search-a
(ako je potrebno dadaje nove vrste ili brise visak).
Tu tabelu (ajax odgovor) stavljam na bilo koje mesto na stranici koja je generisala ajax poziv.
Sve to lepo radi.
Medjutim nervira me to sto generise one elemente odozgo. Tako da ja sada na stranici imam dva html i body taga.