...

View Full Version : Help with tpl troubleshooting



skydiver_morgan
08-29-2006, 11:37 PM
Hi, I am have a custom calendar application in use on our website. On the calendar, we have a search box that submits a text field to the search.php file which I am including here. When I enter a search term in which there is an occurance in the database, the php file works as expected, listing all the event names (the array value {result_name}) and times (array value {result_date}). When I enter no text (nul) it lists all of events in the database, again as designed. But when I enter a search term which does not occur in the database, the value printed in the htm template is {results_row_th}. It is outputting the explicit text of the tpl block.

As I looked at this code, I realized that there is no empty set error handling. Since I am still learning php and I am maintaining this application I need to know where/how to check for an empty result set and where to add a default value to inform the user of the Empty Result.

Here is the search.php code:

<?
$GLOBAL_RIGHT_NOW = date("Y-m-d H:i:s");
include("template.inc.php");
include("funclib.inc.php");
$tpl = new Template("templates/");
$tpl->set_file("Template", "search_results_template.htm");
$tpl->set_block("Template", "results_row", "results_row_th");
$search_text = $GLOBALS['search_text'];
$tpl->set_var("old_search", $search_text);
$sql = "SELECT EVT_id, EVT_name, EVT_start FROM events WHERE ";
$search_words = explode(" ", $search_text);
$search_matrix = array();
while ( list ($key, $value) = each ($search_words) ) {
array_push($search_matrix, "EVT_name LIKE '%".$value."%'");
}
$sql .= implode (" OR ", $search_matrix);
$link = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_db);
$result = mysql_query($sql);
while ( $row = mysql_fetch_object($result) ) {
$epoch_start = glib_convert_to_epoch($row->EVT_start);
$full_date = date("l F j Y", $epoch_start);
$tpl->set_var(array("result_name" => "<a href=\"javascript:popup('http://www.website.com/austin/calendar/popup.php?mbid=".$row->EVT_id."',400,620);\">".strtoupper($row->EVT_name)."</a>",
"result_date" => $full_date));
$tpl->parse("results_row_th", "results_row", true);
}
mysql_close($link);
$tpl->parse("HTML", "Template");
print $tpl->subst("HTML");
?>


Thanks in advanced for the help!
Skydiver

skydiver_morgan
08-31-2006, 02:03 PM
Any suggestions would be helpful....

skydiver_morgan
09-12-2006, 04:29 PM
I was able to solve the problem lke this:


<?
$GLOBAL_RIGHT_NOW = date("Y-m-d H:i:s");
include("template.inc.php");
include("funclib.inc.php");
$tpl = new Template("templates/");
$tpl->set_file("Template", "search_results_template.htm");
$tpl->set_block("Template", "results_row", "results_row_th");
$search_text = $GLOBALS['search_text'];
$tpl->set_var("old_search", $search_text);
$sql = "SELECT EVT_id, EVT_name, EVT_start FROM events WHERE ";
$search_words = explode(" ", $search_text);
$search_matrix = array();
$result_count = 0;
while ( list ($key, $value) = each ($search_words) ) {
array_push($search_matrix, "EVT_name LIKE '%".$value."%'");
}
$sql .= implode (" OR ", $search_matrix);
$link = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_db);
$result = mysql_query($sql);
while ( $row = mysql_fetch_object($result) ) {
$epoch_start = glib_convert_to_epoch($row->EVT_start);
$full_date = date("l F j Y", $epoch_start);
$tpl->set_var(array("result_name" => "<a href=\"javascript:popup('http://www.website.com/austin/calendar/popup.php?mbid=".$row->EVT_id."',400,620);\">".strtoupper($row->EVT_name)."</a>",
"result_date" => $full_date));
$tpl->parse("results_row_th", "results_row", true);
++$result_count;
}
++$result_count;
}
if ($result_count == 0) {
$tpl->set_var(array(
"result_name" => "Sorry, your search resulted in no events.",
"result_date" => '',
"result_stage" => ''));
$tpl->parse("results_row_th", "results_row", true);
}
mysql_close($link);
$tpl->parse("HTML", "Template");
print $tpl->subst("HTML");
?>


Hope this helps anyone else with a similar problem. I was unimpressed with the help on this boards.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum