CREATE TABLE pages (
id int (10) UNSIGNED NOT NULL AUTO_INCREMENT,
url text NOT NULL ,
title text NOT NULL ,
content text NOT NULL ,
updated datetime NOT NULL ,
PRIMARY KEY (id),
FULLTEXT KEY content (content)
) ENGINE=MyISAM
* This source code was highlighted with Source Code Highlighter .
SELECT *
FROM pages
WHERE MATCH (content) AGAINST ( 'test' ) > 0
* This source code was highlighted with Source Code Highlighter .
class Page_model extends Model
{
function Page_model()
{
parent::Model();
//
$ this ->load->database();
}
function search($terms)
{
//
$sql = "SELECT url, title
FROM pages
WHERE MATCH (content) AGAINST (?) > 0" ;
$query = $ this ->db->query($sql, array($terms, $terms));
return $query->result();
}
}
* This source code was highlighted with Source Code Highlighter .
<? php $ this- > load- > helper('form'); ? >
<? php echo form_open ($ this- > uri- > uri_string); ? >
<? php echo form_label ( 'Search:' , 'search-box' ); ? >
<? php echo form_input ( array ( 'name' = > 'q', 'id' = > 'search-box', 'value' = > $search_terms)); ? >
<? php echo form_submit ( 'search' , 'Search' ); ? >
<? php echo form_close (); ? >
<? php if ( ! is_null ($ results )) : ? >
<? php if ( count ($ results )) : ? >
< ul >
<? php foreach ($ results as $ result ) : ? >
< li >< a href =" 60 ;? php echo $ result- > url; ? > " ><? php echo $ result- > title; ? ></ a ></ li >
<? php endforeach ? >
</ ul >
<? php else: ? >
< p >< em > There are no results for your query. </ em ></ p >
<? php endif ? >
<? php endif ? >
* This source code was highlighted with Source Code Highlighter .
class Pages extends Controller {
function search($search_terms = '' )
{
// URL
// ,
// .
if ($ this ->input->post( 'q' ))
{
redirect( '/pages/search/' . $ this ->input->post( 'q' ));
}
if ($search_terms)
{
//
$ this ->load->model( 'page_model' );
$results = $ this ->page_model->search($search_terms);
}
//
$ this ->load->view( 'search_results' , array(
'search_terms' => $search_terms,
'results' => @$results
));
}
}
* This source code was highlighted with Source Code Highlighter .
$autoload[ 'helper' ] = array( 'url' );
* This source code was highlighted with Source Code Highlighter .
class Page_model extends Model {
function search($terms, $start = 0, $results_per_page = 0)
{
//
//
if ($results_per_page > 0)
{
$limit = "LIMIT $start, $results_per_page" ;
}
else
{
$limit = '' ;
}
// SQL
$sql = "SELECT url, title, content
FROM pages
WHERE MATCH (content) AGAINST (?) > 0
$limit" ;
$query = $ this ->db->query($sql, array($terms, $terms));
return $query->result();
}
function count_search_results($terms)
{
// Run SQL to count the total number of search results
$sql = "SELECT COUNT(*) AS count
FROM pages
WHERE MATCH (content) AGAINST (?)" ;
$query = $ this ->db->query($sql, array($terms));
return $query->row()->count;
}
}
* This source code was highlighted with Source Code Highlighter .
class Pages extends Controller {
function search($search_terms = '' , $start = 0)
{
// URL
// ,
// .
if ($ this ->input->post( 'q' ))
{
redirect( '/pages/search/' . $ this ->input->post( 'q' ));
}
if ($search_terms)
{
//
//
$results_per_page = $ this ->config->item( 'results_per_page' );
// , ,
//
$ this ->load->model( 'page_model' );
$results = $ this ->page_model->search($search_terms, $start, $results_per_page);
$total_results = $ this ->page_model->count_search_results($search_terms);
//
$ this ->_setup_pagination( '/pages/search/' . $search_terms . '/' , $total_results, $results_per_page);
//
$first_result = $start + 1;
$last_result = min($start + $results_per_page, $total_results);
}
//
$ this ->load->view( 'search_results' , array(
'search_terms' => $search_terms,
'first_result' => @$first_result,
'last_result' => @$last_result,
'total_results' => @$total_results,
'results' => @$results
));
}
function _setup_pagination($url, $total_results, $results_per_page)
{
//
$ this ->load->library( 'pagination' );
$uri_segment = count(explode( '/' , $url));
//
//
$ this ->pagination->initialize(array(
'base_url' => site_url($url),
'uri_segment' => $uri_segment,
'total_rows' => $total_results,
'per_page' => $results_per_page
));
}
}
* This source code was highlighted with Source Code Highlighter .
$config[ 'results_per_page' ] = 10;
* This source code was highlighted with Source Code Highlighter .
<? php $ this- > load- > helper(array('form', 'search')); ? >
<? php echo form_open ($ this- > uri- > uri_string); ? >
<? php echo form_label ( 'Search:' , 'search-box' ); ? >
<? php echo form_input ( array ( 'name' = > 'q', 'id' = > 'search-box', 'value' = > $search_terms)); ? >
<? php echo form_submit ( 'search' , 'Search' ); ? >
<? php echo form_close (); ? >
<? php if ( ! is_null ($ results )) : ? >
<? php if ( count ($ results )) : ? >
< p > Showing search results for ' <? php echo $ search_terms ; ? > ' ( <? php echo $ first_result ; ? > – <? php echo $ last_result ; ? > of <? php echo $ total_results ; ? > ): </ p >
< ul >
<? php foreach ($ results as $ result ) : ? >
< li >< a href =" 60 ;? php echo $ result- > url; ? > " ><? php echo search_highlight ($ result- > title, $search_terms); ? ></ a >< br /><? php echo search_extract ($ result- > content, $search_terms); ? ></ li >
<? php endforeach ? >
</ ul >
<? php echo $ this- > pagination- > create_links(); ? >
<? php else: ? >
< p >< em > There are no results for your query. </ em ></ p >
<? php endif ? >
<? php endif ? >
* This source code was highlighted with Source Code Highlighter .
// Mark the start of search
$ this ->benchmark->mark( 'search_start' );
// Load the model, perform the search and establish the total
// number of results
$ this ->load->model( 'page_model' );
$results = $ this ->page_model->search($search_terms, $start, $results_per_page);
$total_results = $ this ->page_model->count_search_results($search_terms);
// Mark the end of search
$ this ->benchmark->mark( 'search_end' );
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/110557/
All Articles