...

View Full Version : Cant understand what happen!!



feras_wilson
08-18-2007, 01:06 PM
Hi!
I have search code here but i am not good at php so i need help to know where to enter the keywords



<?
/*
* PHP Freaks Code Library
* http://www.phpfreaks.com/quickcode.php
*
* Title: Cool PHP 5 Search Class
* Version: 1.0
* Author: Will Fitch aka(willfitch)
* Date: Wednesday, 08/31/2005 - 09:06 PM
*
*
*
* NOTICE: This code is available from PHPFreaks.com code Library.
* This code is not Copyrighted by PHP Freaks.
*
* PHP Freaks does not claim authorship of this code.
*
* This code was submitted to our website by a user.
*
* The user may or may not claim authorship of this code.
*
* If there are any questions about the origin of this code,
* please contact the person who submitted it, not PHPFreaks.com!
*
* USE THIS CODE AT YOUR OWN RISK! NO GUARANTEES ARE GIVEN!
*
* SHAMELESS PLUG: Need WebHosting? Checkout WebHost Freaks:
* http://www.webhostfreaks.com
* WebHosting by PHP Freaks / The Web Freaks!
*/

// * Description / Example:
// *
// * This PHP5 search class runs with MySQL. It highlights, bolds, colorizes, and places a background color behind keywords in the search results, paginates, and all the while is HTML 4.01 and 508 compliant! You can also turn all of these features on and off.
// *
// * The really cool thing is that this class can search external tables based off of foreign keys. Take a look at the example usage for more details.
// *
// * If you have any questions, or think of a cool idea and want to work on it together, email me at will [at] phpfever [dot] com.
?>
<?
######################## PHP Search Class (PHP5) ############################
## SOURCE NAME: Search.inc.php
## SOFTWARE REQUIREMENTS: PHP5, MySQL 3.23+ (Prefer 4+)
## USAGE: Please refer to the README file with your text editor for
## instructions.
#############################################################################
## AUTHOR: Will Fitch
## EMAIL: will [at] phpfever [dot] com
## DATE CREATED: 08/18/05
## NOTE: If any improvements or features are added to the engine, please
## post them on the A&WS Portal, or email them to me and I will do
## it.
#############################################################################
## Revision History:
## Date Developer Reason for change
## -------- ----------- ---------------------------------------
#############################################################################
#############################################################################
class Search {
public $Keywords; // Keywords provided by user
public $MainTable; // Main table to search. If you need more than one, make a drop down as to which to search!
public $InternalSearchFields; // Fields that are within the MainTable itself, not linked to another table
public $ExternalTables; // External table,main table linked key => external_field
public $DetailsLink; // Link to a more detailed report (e.g. data_entry.php?aid= )
public $IdFieldLink; // Id field passed to the details page (e.g. ApplicationId)
public $FieldHeader; // Link for each search (e.g. AppName)
public $FieldDescription; // Description field
public $DescriptionLength = 250; // Maximum amount of characters to display on the description
public $SearchTitle = \'Search\'; // The default title of the search page
public $MaxResults = 10; // The default maximum number of results per page
public $DebugMode = 0; // Turn this on to see debugging information
public $ScrollOn; // Turn this on to use a scrolling effect for large results
public $ScrollNumber = 15; // The number of result pages to show at one time
private $TextHighlight = 0; // Highlight the text
private $HighlightColor = \'#F9EDED\'; // Default highlight color
private $ErrorMessage; // Private (used in ShowError method)
private $ErrorTitle; // Private (used in ShowError method)
private $TextColor = \'#FF0000\'; // Color to highlight the text
private $MakeBold = 1; // Bold the text
private $WhereClause; // Private (used in navigation and SQL)
# Used only for error messages within the code (private)
private function ShowError() {
$error_message = \'
<html>
<head>
<title>Error \'.$this->ErrorTitle.\'</title>
</head>
<body>
<h2 align=\"center\" style=\"font-family:Arial;\">\'.$this->ErrorTitle.\'</h2>
<table width=\"400\" align=\"center\" border =\"1\" style=\"border-collapse:collapse;\" bordercolor=\"#ff9900\">
<tr><td bgcolor=\"#EBEBEB\">
<div align=\"left\" style=\"font-family:Arial;font-size:12px;\">\'.$this->ErrorMessage.\'</div>
</td></tr>
</table>
</body>
</html>
\';
die($error_message);
} // End private ShowError
# Method that shows debugging information
public function TextSetup($make_bold=1,$text_color=\'#FF0000\',$highlight=0,$highlight_color) {
$this->TextColor = $text_color;
$this->TextHighlight = $highlight;
$this->MakeBold = $make_bold;
$this->HighlightColor = $highlight_color;
return true;
} // End public TextSetup
# Private method used to present debugging information
private function Debugger($internal_fields,$external_fields,$debug_info) {
$debug_info = str_replace($this->MainTable,\'<span style=\"color:#009900;font-weight:bold;\">\'.$this->MainTable.\'</span>\',$debug_info);
$debug_content = \'
<b>Main Table to Search: </b><span style=\"color:#009900;font-weight:bold;\">\'.$this->MainTable.\'</span><br>
<b>Keywords Provided: </b>\'.$this->Keywords.\'<br>
<b>Linked Header to Show: </b>\'.$this->FieldHeader.\'<br>
<b>Description Field: </b>\'.$this->FieldDescription.\'<br>
<b>Internal Fields To Search: </b>\'.$internal_fields.\'<br>
<b>External Tables To Search: </b>\'.$external_fields.\'<br>
<b>Use Bold Text: </b>\'.$this->MakeBold.\'<br>
<b>Text Color: </b>\'.$this->TextColor.\'<br>
<b>Use Text Highlight: </b> \'.$this->TextHighlight.\'<br>
<b>Highlight Color: </b> \'.$this->HighlightColor.\'<br>
<b>SQL Code Produced:</b><br>
\'.$debug_info.\'
\';
$debug_status = \'
<html>
<head>
<title>Search Debugger</title>
</head>
<body>
<h2 align=\"center\" style=\"font-family:Arial;\">Debugging Information</h2>
<table align=\"center\" border =\"1\" style=\"border-collapse:collapse;\" bordercolor=\"#ff9900\">
<tr><td bgcolor=\"#EBEBEB\">
<div align=\"left\" style=\"font-family:Arial;font-size:12px;\">\'.$debug_content.\'</div>
</td></tr>
</table>
</body>
</html>\';
die($debug_status);
} // End private Debugger
# Method that creates the SQL code
public function MakeSQL($word_verify=NULL) {
if ($this->DebugMode == 1) {
$line_break = \'<br>\';
} else {
$line_break = \'\';
} // End else
if (isset($word_verify)) {
$this->Keywords = $word_verify;
}
$select_fields = \'SELECT \'.$line_break;
$where_clause = \' WHERE \'.$line_break;
if (isset($this->ExternalTables)) {
# If the provided ExternalFields is not an array, kill the script and show the error
if (!is_array($this->ExternalTables)) {
$this->ErrorTitle = \'The ExternalFields specified are not in an array.\';
$this->ErrorMessage = \'In order to use external fields, the field associated with the external table within the main table
must be the key of the array, with the field to search as the element. (e.g. array(\"MyFieldId\" => \"ExternalFieldtoSearch\"))\';
$this->ShowError();
} // End if
# Loop through the ExternalFields and build the JOINS
foreach ($this->ExternalTables as $ext_table => $external_primary_key) {
# If the provided $ext_table doesn\'t include the external table and linked field id, kill the script
if (!preg_match(\'/,/\',$ext_table)) {
$this->ErrorTitle = \'Error creating SQL\';
$this->ErrorMessage = \'The provided ExternalFields is incorrect. For each array, you must specify the key as as the following:
array(\"external_table,maintable_linkedid\" => \"field1,field2,field3\"). Please correct this issue.\';
$this->ShowError();
} // End if
$ext_table_split = explode(\',\',$ext_table);
# Count the number of keys
$num_fields_to_search = count($external_primary_key);
for ($i = 0; $i < $num_fields_to_search; $i++) {
$field_external = $i + 1;
$external_fields .= $ext_table_split[$i].\' \';
$select_fields .= $ext_table_split[$i].\"__$i.$ext_table_split[$field_external] AS $ext_table_split[0]__$i,$line_break\";
$joins .= \" LEFT JOIN $ext_table_split[$i] AS $ext_table_split[$i]__$i ON $this->MainTable.$external_primary_key = $ext_table_split[$i]__$i.$external_primary_key $line_break\";
$where_clause .= \"{$ext_table_split[$i]}__$i.$ext_table_split[$field_external] LIKE \'%$this->Keywords%\' OR $line_break\";
} // End for loop
} // End foreach loop
} // End if
# Loop through the given Internal Fields
$count_internal_fields = count($this->InternalSearchFields);
$select_fields .= $this->MainTable.\'.\'.$this->IdFieldLink.\', \'.$line_break;
$select_fields .= $this->MainTable.\'.\'.$this->FieldHeader.\', \'.$line_break;
for ($i = 0; $i < $count_internal_fields; $i++) {
$internal_fields .= $this->InternalSearchFields[$i].\' \';
$select_fields .= \"$this->MainTable.{$this->InternalSearchFields[$i]}, $line_break\";
$where_clause .= \"$this->MainTable.{$this->InternalSearchFields[$i]} LIKE \'%$this->Keywords%\' OR $line_break\";
} // End for loop
# If you need to debug
if ($this->DebugMode == 1) {
$select_setup = substr($select_fields,0,-6).$line_break;
$where_clause = substr($where_clause,0,-7).$line_break;
$select .= $select_setup.\' FROM \'.$this->MainTable.$line_break;
$this->Debugger($internal_fields,$external_fields,$select.$joins.$where_clause);
} else {
# Strip the ending comma
$select_setup = substr($select_fields,0,-2);
# Strip the last OR
$where_clause = substr($where_clause,0,-3);
# Add the rest of the select statment
$select .= $select_setup.\' FROM \'.$this->MainTable;
$this->WhereClause = $joins.$where_clause;
return $select.$joins.$where_clause;
}
} // End private MakeSQL
private function WordVerify($keywords,$no_keywords=FALSE) {
# Count the number of words inside the Keywords object and put into an array
$word_count = str_word_count($keywords,1);
# In case the there are less than 2 words
if ((count($word_count) < 2) || ($no_keywords == TRUE)) {
$message = \'<div align=\"center\" style=\"margin-top:20px;margin-bottom:20px;\">There are no results for your search. Please try a broader search term.</div>\';
} else {
# If there is more than one word....
$message .= \'<div align=\"center\"><b>There were no matches for your search.<br> Please try a broader search or use one of these:</b></div>\';
foreach($word_count as $words) {
# Making the WHERE clause
$where_clause = $this->MakeSQL($words);
# Query the number of results for each word
$query = mysql_query($where_clause);
$count = mysql_num_rows($query);
# If there is less than one result on this word....
if ($count < 1) {
$message .= \'<div align=\"center\">\'.$words.\' (0 Results Matching)</div>\';
# If not, print out the number of results per word
} else {
$message .= \'<div align=\"center\"><b><a href=\"\'.$_SERVER[\'PHP_SELF\'].\'?q=\'.$words.\'\" title=\"Search Using \'.$words.\'\">\'.$words.\'</a></b> (<b>\'.$count.\'</b> Results Matching)</div>\';
} // End else
} // End foreach loop
} // End else
return $message;
}
private function HighlightFindings($findings) {
# Create a local var for the Keywords object
$keywords = $this->Keywords;
# Create another local var for the Keywords object
$keywords2 = $this->Keywords;
# Place html chars into the database info
$string = htmlspecialchars($string);
# If you want bold text
$this->MakeBold ? $bold = \'font-weight:bold;\' : $bold = \'\';
# If you want highlighted text
$this->TextHighlight ? $highlight = \'background: \'.$this->HighlightColor.\';\' : \'\';
# Setup the span tag
$span = \'<span style=\"color:\'.$this->TextColor.\';\'.$highlight.$bold.\'\">\';
# Replace the keywords with a more bold, colored look...
$substituted_field = substr($findings,0,$this->DescriptionLength);
$replace = @eregi_replace($keywords,$span.$keywords2.\'</span>\',$substituted_field);
return $replace;
} // End private HighlightFindings
# Method that makes the pagination
private function QueryNavigation() {
if(!isset($_GET[\'page\'])){
$page = 1;
} else {
$page = $_GET[\'page\'];
} // End else
# Create a local variable that doesn\'t affect the object
$max_results = &$this->MaxResults;
# Currnt page multiplied by the max number of results then subtract the max number of results
$from = (($page * $max_results) - $max_results);
/* Count the total results for the navigation */
$total_results = mysql_result(mysql_query(\"SELECT COUNT(*) as Num FROM $this->MainTable $this->WhereClause\"),0);
/* Calculate the total results divided by the max results to produce to number of records per page. Use
ceil() to round up to the nearest whole number */
$total_pages = ceil($total_results / $max_results);
/* Start the HTML production for the navigation system */
$navigation = \'<div align=\"center\">\';
/* Product the Previous Link */
if($page > 1){
$prev = ($page - 1); // Subtract one from the current page
$navigation .= \'<a href=\"\'.$_SERVER[\'PHP_SELF\'].\'?page=$prev&q=\'.$this->Keywords.\'\" title=\"Previous Page\" onMouseOver=\"self.status=\\\'Previous Page\\\'; return true;\" onFocus=\"self.status=\\\'Previous Page\\\'; return true;\"> Previous</a>&nbsp;\';
} // End if
if ($this->ScrollOn == 1) { // See if the scroll effect is on
if ($total_pages > $this->ScrollNumber) {
$first = $page;
$last = ($this->ScrollNumber - 1) + $page;
} else {
$first = $page;
$last = $total_pages;
} // end $scroll if else()
} else {
$first = 1;
$last = $total_pages;
} // end $scroll if else()
# This starts the numbers in the navigation system.
if ($total_results != $max_results) {
$first = 1;
$last = $total_pages;
# Start the for loop for the numbers.
for ($i = $first;$i <= $last;$i++) {
if ($page == $i) {
$navigation .= $i.\'&nbsp;\';
} else {
$navigation .= \'<a href=\"\'.$_SERVER[\'PHP_SELF\'].\'?page=\'.$i.\'&q=\'.$this->Keywords.\'\" title=\"Page \'.$i.\'\" onMouseOver=\"self.status=\\\'Page \'.$i.\'\\\'; return true;\" onFocus=\"self.status=\\\'Page \'.$i.\'\\\'; return true;\">\'.$i.\'</a>&nbsp;\';
} // end $i if else()
} // end for loop
} // end $total_results if()
# Building the Next link
if($page < $total_pages){
$next = ($page + 1);
$navigation .= \'<a href=\"\'.$_SERVER[\'PHP_SELF\'].\'?page=\'.$next.\'&q=\'.$this->Keywords.\'\" onMouseOver=\"self.status=\\\'Next Page\\\'; return true;\" onFocus=\"self.status=\\\'Next Page\\\'; return true;\" title=\"Next Page\">Next</a>\';
} // End if
# Make a First and Last Page link for the user to navigate
if ($total_pages > 1) {
$navigation .= \'<br><a href=\"\'.$_SERVER[\'PHP_SELF\'].\'?page=1&q=\'.$this->Keywords.\'\" title=\"Go to the First Page\" onMouseOver=\"self.status=\\\'Go to the First Page\\\'; return true;\" onFocus=\"self.status=\\\'Go to the First Page\\\'; return true;\">First Page</a>&nbsp;&nbsp;&nbsp;\';
$navigation .= \'<a href=\"\'.$_SERVER[\'PHP_SELF\'].\'\"?page=\'.$total_pages.\'&q=\'.$this->Keywords.\'\" title=\"Go to the Last Page\" onMouseOver=\"self.status=\\\'Go to the Last Page\\\'; return true;\" onFocus=\"self.status=\\\'Go to the Last Page\\\'; return true;\">Last Page</a>&nbsp;<br><br>\';
} // End if
$navigation .= \'</div>\';
return $navigation;
}// End private QueryNavigation

final public function MakeSearch() {
if ((is_null($this->Keywords)) && ($this->DebugMode != 1)) {
$no_value = $this->WordVerify(\'\',TRUE);
return $no_value;
} // End if
$sql_code = self::MakeSQL();
# Find out what page we are on
if(!isset($_GET[\'page\'])) {
$page = 1;
} else {
$page = $_GET[\'page\'];
} // End else
# Define the number of results per page
$max_results = $this->MaxResults;
$from = (($page * $max_results) - $max_results);
$query = mysql_query($sql_code.\" LIMIT $from,$max_results;\")or die(mysql_error());
# Current page\'s query
# Count the number of rows for this page\'s query
$total_count = mysql_num_rows($query);
# Total count of all applications with search WHERE clause
$total_applications = mysql_result(mysql_query(\"SELECT COUNT(*) FROM $this->MainTable $this->WhereClause\"),0);
if ($from < 1) { // Check to see if 0
$this->FromLabel = 1;
} else { // Go with $from
$this->FromLabel = $from;
} // End else
if ($page == 1) {
$end_label = $from + $total_count;
} else {
$end_label = $from + ($total_count - 1); // Current record + total number counted on page
}
# Create the table for the title of the search and the results information
$results .= \'<table border=\"1\" width=\"100%\" style=\"border-collapse:collapse;\" bordercolor=\"#EBEBEB\">\'.\"\\r\\n\".\' (file://\\r\\n\&quot;.\')
<tr bgcolor=\"#E5ECF9\">\'.\"\\r\\n\".\'<td>\'.\"\\r\\n\".\'<div (file://\\r\\n\&quot;.\'<td>\'.\&quot;\\r\\n\&quot;.\'<div) align=\"left\" style=\"font-size:20px\"><b>\'.$this->SearchTitle.\'</b><br></div>\'.\"\\r\\n\".\'</td>\'.\"\\r\\n\".\'\' (file://\\r\\n\&quot;.\'</td>\'.\&quot;\\r\\n\&quot;.\'\');
$results .= \'\'.\"\\r\\n\".\'<td>\'.\"\\r\\n\".\'<div (file://\\r\\n\&quot;.\'<td>\'.\&quot;\\r\\n\&quot;.\'<div) align=\"right\"> Results <b>\'.$this->FromLabel.\' - \'.$end_label.\'</b> of <b>\'.$total_applications.\'</b> using &quot;<b>\'.urldecode($this->Keywords).\'</b>&quot;</div>\'.\"\\r\\n\".\'</td>\'.\"\\r\\n\".\'</tr>\'.\"\\r\\n\".\'</table>\' (file://\\r\\n\&quot;.\'</td>\'.\&quot;\\r\\n\&quot;.\'</tr>\'.\&quot;\\r\\n\&quot;.\'</table>\');
if ($total_count < 1) {
# In case the resulting query leave us with no results.... Send the keywords over to the WordVerify method
$results .= self::WordVerify($this->Keywords);
} else {
# There must be some results for us......YAY!
$i = $this->FromLabel;
# Create the results for the page
while ($row = mysql_fetch_object($query)) {
# Rename FieldHeader object locally
$header = $this->FieldHeader;
# Rename FieldDescription object locally
$description = $this->FieldDescription;
# Rename the IdFieldLink object locally
$field_link = $this->IdFieldLink;
# Loop and paint the resulting information
$results .= \'
<p>\'.\"\\r\\n (file://\\r\\n) <b>\".$i.\'.</b> <a href=\"\'.$this->DetailsLink.$row->$field_link.\'\" title=\"\'.$row->$header.\'\">\'.$this->HighlightFindings($row->$header).\'</a>\'.\"\\r\\n\".\'<br (file://\\r\\n\&quot;.\'<br)>
\'.$this->HighlightFindings($row->$description).\"\\r\\n\".\'</p>\'.\"\\r\\n\ (file://\\r\\n\&quot;.\'</p>\'.\&quot;\\r\\n\)";
$i++;
} // end while
$results .= self::QueryNavigation();
} // end if
return $results;
} // End public MakeSearch
}
?>

Where to enter the keywords? example: Cat, Fish, Clothes, Home, etc.

kbluhm
08-18-2007, 01:12 PM
After a quick perusal of the class and its properties... namely this line:


public $Keywords; // Keywords provided by user

...it'd appear be something like this:

<?php

/* ... */

$search = new Search;

$search->Keywords = 'my keywords here';

/* ... */

?>

feras_wilson
08-18-2007, 01:52 PM
After a quick perusal of the class and its properties... namely this line:


...it'd appear be something like this:

<?php

/* ... */

$search = new Search;

$search->Keywords = 'my keywords here';

/* ... */

?>
Thank you a lot!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum