search.php 1.64 KB
<?php
/**
 * Created by PhpStorm.
 * User: aleksandarhristov
 * Date: 14.09.15
 * Time: 09:37
 */

//include the smarty class and the global functions
require "libs/Smarty.class.php";
require "global_functions.php";

//initialize smarty
$smarty = new Smarty;

//connect to the database
$db = new SQLite3('wiki.db');



if (isset($_GET['s']))
{
	$intCtr=null;
	$current_page=$db->querySingle("SELECT MIN (id) FROM pages");

	if (!empty($_GET['s']))
	{
		$strSearch = " ".$_GET['s'];
		$sql = "SELECT id, page_id, title, plain_text
 				FROM sections
 				WHERE upper(plain_text) LIKE upper('%$strSearch%')";

		$result = $db->query($sql);

		$i=0;
		while ($row = $result->fetchArray())
		{
			$arrResult[$row[$i]] = array();
			$arrResult[$row[$i]]['section_id'] = $row['id'];
			$arrResult[$row[$i]]['page_id'] = $row['page_id'];
			$arrResult[$row[$i]]['section_title'] = $row['title'];
			$arrResult[$row[$i]]['section_text'] = "...".substr(stristr($row['plain_text'], $strSearch), 0, 300)."...";
			$arrResult[$row[$i]]['section_text'] = preg_replace("/".preg_quote($strSearch)."\w*/i", "<mark><u><strong>$0</strong></u></mark>", $arrResult[$row[$i]]['section_text']);
		}
		if(empty($arrResult))
		{
			$error = "No result found!";
			$arrResult = null;
		}
		else
		{
			$error = null;
		}
		$intCtr = count($arrResult);

	}

	//assign smarty variables and display the search.tpl
	$smarty->assign("current_page", $current_page);
	$smarty->assign("pages", get_pages());
	$smarty->assign("pages_id", get_pages_id());
	$smarty->assign("arrResult", $arrResult);
	$smarty->assign("counter", $intCtr);
	$smarty->assign("error", $error);
	$smarty->display("tpl/search.tpl");
}