PHP Classes

PHP MySQL Full Text Search: Perform full text search on MySQL with autocorrect

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 912 This week: 1All time: 3,894 This week: 560Up
Version License PHP version Categories
full-text-search 1.0GNU General Publi...5PHP 5, Databases, Searching
Description 

Author

This class can perform full text searches on MySQL with auto-correct.

It takes a given keyword and performs a SQL query on a given MySQL database table field using full text search matching.

If the search returns no results, the class may fallback to pattern searching.

The class may optionally perform auto-correction of the search keyword using Felipe Ribeiro spell corrector class.

The class can also alter the MySQL table to add a full text search index to the field to be search. If necessary it may alter the table type to MyISAM in case the current table type does not support full text searching.

Innovation Award
PHP Programming Innovation award nominee
December 2014
Number 5


Prize: One downloadable copy of CodeLobster Professional
Full text search is a way to perform searches on a database table text field providing results with a given matching accuracy level.

This class can help setting up MySQL database table indexes to make them ready to perform full text searches on given fields.

It can also execute queries to perform the actual full text searches on given table fields for given search keywords. When no results are returned, it falls back performing regular pattern based search.

Manuel Lemos
Picture of Taiwo Peace
  Performance   Level  
Name: Taiwo Peace is available for providing paid consulting. Contact Taiwo Peace .
Classes: 3 packages by
Country: Nigeria Nigeria
Age: 29
All time rank: 16506 in Nigeria Nigeria
Week rank: 411 Up10 in Nigeria Nigeria Down
Innovation award
Innovation award
Nominee: 1x

Recommendations

What is the best PHP auto correct spelling class?
I need search class that suggests available keyword in database

Example

<!-- this is a complete example usage of the phpsearchautocorrect class
To run this example, import the test.sql database to you mysql server
if you have any problem using this class please do not hesitate to email me @ olorundaolaoluwa@gmail.com
and please dont forget to rate this class if it proves usefull to you
 -->


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET" />
<input type="text" name="search" />
<select name="time" />
<!-- maintain the values of the time select options -->
<option value="past24" >Past 24 hours </option>
<option value="pastweek" >Past Week </option>
<option value="pastmonth" >Past Month </option>
<option value="pastyear" >Past Year </option>
<option value="anytime" >Any Time </option>
</select>
<input type="submit" value="Search" />

</form>


<?php
/**for the spellcorrector to work correctly, go here https://www.mediafire.com/?jmmu7rwj4p90dgx to download the dictionary files. make sure you extract into the same directory as the class**/
if(isset($_GET['search'])){
include
'fulltextsearch.php';

$fulltext=new fulltextsearch();

//if you wan to integrate this with php bootstrap pagination you can download the class here http://www.phpclasses.org/browse/author/1217990.html , use this to supply total record to the pagin class $fulltext->total_record
//provide data for database connection

$fulltext->dbname='shareandsmile';
$fulltext->dbusername='root';
$fulltext->dbpassword='';

//specify the table name that you want to perrform a search on

$fulltext->tablename='country';

// specify the column name of the table you want to perform the search on

$fulltext->searchcolumn='Name';

//switch on auto correct if your the search string is goin to be an english word . use $fulltext->autocorrect=1 to enable auto correct and $fulltext->autocorrect=0 to set autocorrect off
$fulltext->autocorrect=1;



/**make sure the column name for date is int(8) i.e integer of eight character long
if you want a date based search then use this
for the date search to work, you must create a column for date and write the datecolumn here
when inserting a value into the date column make sure you date value should be equals to date("Ymd") for the date based search to work properly
in this case , my date column name is 'date' **/
//$fulltext->datecolumnname='date';



//you need to call this function only the first time you run the script, the function converts the table to supported format for the search and it also add a fulltext index to the table


//$fulltext->converttable();

//get search keyword

$fulltext->keyword=$_GET['search'];;

/**for a timebased result use $fulltext->dates=$_GET['time'];
you dont need to specify date if you dont want a timebased search **/



//this is needed if you need to break result into chunks ...specify the starting point

$fulltext->startfrom=0;

//specify the number of records you wish to display per page
//download my bootstrap pagin class to make it easier to paginate result. supply the pagination class with this $fulltext->total_record for the total record

$fulltext->resgroup=15;

//check if operation successfull
if($fulltext->search()==true){
//$row=$fulltext->result;
//foreach($row as $rr){

//loop through result

while($row=$fulltext->querys->fetch()){
echo
'Search result: '.$row[1].'<br><br>';
$check=$row[0];
}

//to show how the what the search string is corrected to echo $fulltext->correctedto;

echo 'Searched For:'.$_GET['search'].'<br>Corrected to: '.$fulltext->correctedto;

//check if result is found

if(!isset($check)){

echo
'no result found';

}

}
else{
echo
'error';
}
}


Screenshots  
  • search.JPG
  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example Example Usage
Plain text file fulltextsearch.php Class Main Class
Plain text file SpellCorrector.php Class Spellcorrector class
Accessible without login Plain text file test.sql Test test sql

 Version Control Unique User Downloads Download Rankings  
 0%
Total:912
This week:1
All time:3,894
This week:560Up
User Comments (1)
Thats a very good and useful class ;-)
9 years ago (José Filipe Lopes Santos)
70%StarStarStarStar