<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class Search extends Site_Controller
	{
		
		function __construct()
		{
			parent::__construct();
			$this->load->model("Mod_search");
			$this->load->helper(array('my_pagination','my_function','text'));
		}
	
		public function results()
		{
			// Retrieve the posted search term.
			//$search_term = $this->input->post('search');
			$search_term = $this->input->input_stream('search', TRUE); 
			if($this->input->post('search') !== NULL)
			{
				log_message('error', 'keywork(search), '.$search_term.'');
			}
			// Use a model to retrieve the results.
			$data['results'] = $this->Mod_search->get_results($search_term);
			$data['results'] = $data['results']->result_array(); 
			
			$rows = $this->Mod_search->search_countAll($search_term);
			$data['keywork'] = $search_term;
			$data["pagination"] = search_pagination($rows,10);		
			
			// Pass the results to the view.
			$this->load->view("header");
			$this->load->view("blog/blog-search",$data);
			$this->load->view("blog/blog_sidebar");
			$this->load->view("footer");	
		}
	
	
		public function ajax_refresh()
		{
			$keyword = $this->input->input_stream('search', TRUE);
			$data['results'] = $this->Mod_search->getdata($keyword);
			$results = $data['results']->result_array();
			if(count($results) > 0)
			{
				foreach($results as $v)
				{
					// put in bold the written text
					$country_name = str_replace($keyword, '<b>'.$keyword.'</b>', $v['postTitle']);
					// add new option
					echo '<li onclick="set_item(\''.str_replace("'", "\'", $v['postTitle']).'\')">'.$country_name.'</li>';
				}	
			}
			else
			{
				echo '<li>No data display</li>';	
			} 
		}
		
	}