473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

php code for searching phrase that is enclosed within double quote (“"”) characters

bethifUL13
php code for searching phrase that is enclosed within double quote (“"”) characters

I have a php code for searching words in mysql database using fulltext, it splits the phrases into words and shows all the possible result containing the words in the phrase.The paging is doing good but my problem is if I enter a phrase enclose in double quote (“"”) characters literally, the result also shows all the possible result containing the words in the phrase and the paging is malfunctioning. When I click the next button, it shows an alert "Search Error. Please enter a search keyword...".
What I want is if I enter a phrase enclose in double quote (“"”) characters, the result should be the exact phrase only and the paging should work too.Help please!.

php CODE: (search.php)
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3. $hostname_logon = "localhost" ;
  4. $database_logon = "XXXX" ;
  5. $username_logon = "root" ;
  6. $password_logon = "" ;
  7. //open database connection
  8. $connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unable to connect to the database" );
  9. //select database
  10. mysql_select_db($database_logon) or die ( "Unable to select database!" );
  11.  
  12. //specify how many results to display per page
  13. $limit = 5;
  14.  
  15. //get the search variable from URL
  16. $var = stripslashes($_REQUEST['q']);
  17. //get pagination
  18. $s = mysql_real_escape_string($_REQUEST['s']);
  19.  
  20. //set keyword character limit
  21. if(strlen($var) < 3){
  22.     $resultmsg =  "<p>Search Error</p><p>Keywords with less then three characters are considered invalid...Try again.</p>" ;
  23. }
  24. //trim whitespace from the stored variable
  25. $trimmed = trim($var);
  26. $trimmed1 = trim($var);
  27. //separate key-phrases into keywords
  28. $trimmed_array = explode(" ",$trimmed);
  29. $trimmed_array1 = explode(" ",$trimmed1);
  30.  
  31. // check for an empty string and display a message.
  32. if ($trimmed == "") {
  33.     $resultmsg =  "<p>Search Error</p><p>Please enter a search keyword...</p>" ;
  34. }
  35.  
  36. // check for a search parameter
  37. if (!isset($var)){
  38.     $resultmsg =  "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
  39. }
  40.  
  41. // Build SQL Query for each keyword entered
  42. foreach ($trimmed_array as $trimm){
  43. // EDIT HERE and specify your table and field names for the SQL query
  44. // MySQL "MATCH" is used for full-text searching. Please visit mysql for details.
  45. $query = "SELECT * , MATCH (title, author,yearpub,full_docs) AGAINST ('".$trimm."' IN BOOLEAN MODE) AS score FROM thesis WHERE MATCH (title, author,yearpub,full_docs) AGAINST ('+".$trimm."' IN BOOLEAN MODE) ORDER BY score DESC";
  46.  // Execute the query to  get number of rows that contain search kewords
  47.  $numresults=mysql_query ($query);
  48.  $row_num_links_main =mysql_num_rows ($numresults);
  49.  
  50.  //If MATCH query doesn't return any results due to how it works do a search using LIKE
  51.  if($row_num_links_main < 1){
  52.     $query = "SELECT * FROM thesis WHERE title like '%$trimm%' or author like '%$trimm%' or description like '%$trimm%' or yearpub like '%$trimm%' or adviser like '%$trimm%' or keyword like '%$trimm%' ORDER BY id DESC";
  53.     $numresults=mysql_query ($query);
  54.     $row_num_links_main1 =mysql_num_rows ($numresults);
  55.  }
  56.  
  57.  // next determine if 's' has been passed to script, if not use 0.
  58.  // 's' is a variable that gets set as we navigate the search result pages.
  59.  if (empty($s)) {
  60.      $s=0;
  61.  }
  62.  
  63.   // now let's get results.
  64.   $query .= " LIMIT $s,$limit" ;
  65.   $numresults = mysql_query ($query) or die ( "Couldn't execute query" );
  66.   $row= mysql_fetch_array ($numresults);
  67.  
  68.   //store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.
  69.   do{
  70.       $adid_array[] = $row[ 'id' ];
  71.   }while( $row= mysql_fetch_array($numresults));
  72. } //end foreach
  73.  
  74. //Display a message if no results found
  75. if($row_num_links_main == 0 && $row_num_links_main1 == 0){
  76.     $resultmsg = "<p>Search results for: ". $trimmed."</p><p>Sorry, your search returned zero results</p>" ;
  77. }
  78.  
  79. //delete duplicate record id's from the array. To do this we will use array_unique function
  80. $tmparr = array_unique($adid_array);
  81. $i=0;
  82. foreach ($tmparr as $v) {
  83.    $newarr[$i] = $v;
  84.    $i++;
  85. }
  86.  
  87. //total result
  88. $row_num_links_main = $row_num_links_main + $row_num_links_main1;
  89.  
  90.  
  91. // display an error or, what the person searched
  92. if( isset ($resultmsg)){
  93.     echo $resultmsg;
  94. }else{
  95. echo "</br>";
  96.     echo "<p align = center><h1>Displaying search results for: <em>" . $var."</em></h1></p>";
  97.     echo "<h4>Total number of result(s): $row_num_links_main</h4>";
  98.  
  99.     foreach($newarr as $value){
  100.  
  101.     // EDIT HERE and specify your table and field unique ID for the SQL query
  102.     $query_value = "SELECT * FROM thesis WHERE id = '".$value."'";
  103.     $num_value=mysql_query ($query_value);
  104.     $row_linkcat= mysql_fetch_array ($num_value);
  105.     $row_num_links= mysql_num_rows ($num_value);
  106.  
  107.     //create summary of the long text. For example if the field2 is your full text grab only first 165 characters of it for the result
  108.     $introcontent = strip_tags($row_linkcat[ 'description']);
  109.     $introcontent = substr($introcontent, 0, 165)."...";
  110.     $introdocs = strip_tags($row_linkcat[ 'full_docs']);
  111.     $introdocs = substr($introdocs, 50, 500)."...";
  112.  
  113.  
  114.     //now let's make the keywods bold. To do that we will use preg_replace function.
  115.     //Replace field
  116.       $title = preg_replace ( "'($var)'si" , "<strong>\\1</strong>" , $row_linkcat[ 'title' ] );
  117.       $desc = preg_replace ( "'($var)'si" , "<strong>\\1</strong>" , $introcontent);
  118.       $link = preg_replace ( "'($var)'si" , "<strong>\\1</strong>" ,  $row_linkcat[ 'author' ]  );
  119.       $docs = preg_replace ( "'($var)'si" , "<strong>\\1</strong>" ,  $introdocs);
  120.  
  121.         foreach($trimmed_array as $trimm){
  122.             if($trimm != 'b' ){
  123.                 $title = preg_replace( "'($trimm)'si" ,  "<strong>\\1</strong>" , $title);
  124.                 $desc = preg_replace( "'($trimm)'si" , "<strong>\\1</strong>" , $desc);
  125.                 $link = preg_replace( "'($trimm)'si" ,  "<strong>\\1</strong>" , $link);
  126.                 $docs = preg_replace( "'($trimm)'si" ,  "<strong>\\1</strong>" , $docs);
  127.              }//end highlight
  128.         }//end foreach $trimmed_array
  129.  
  130.         //format and display search results
  131.  
  132.              echo '<div class="search-result">';
  133.             echo" <h3> <a href='View.php?id=",$row_linkcat[id],"'><u>",$title,"</a></u></h3>
  134.             <ol><strong>AUTHOR:</strong>&nbsp;&nbsp;
  135.  
  136.  ",$link,"</ol><ol><strong>DESCRIPTION:</strong>&nbsp;&nbsp; ",$desc,"</ol>
  137.  <ol><strong>FULL DOCUMENTS:</strong>&nbsp;&nbsp; ",$docs,"</ol>";
  138.  
  139.  
  140.                 echo "<hr/>"; 
  141.                 echo '</div>';
  142.  
  143.     }  //end foreach $newarr
  144.  
  145.     if($row_num_links_main > $limit){
  146.     // next we need to do the links to other search result pages
  147.         if ($s >=1) { // do not display previous link if 's' is '0'
  148.             $prevs=($s-$limit);
  149.           echo '<a href="'.$PHP_SELF.'?s='.$prevs.'&q='.$var.'">&nbsp;&nbsp; Previous &nbsp;&nbsp;</a>';
  150.         }
  151.     // check to see if last page
  152.         $slimit =$s+$limit;
  153.         if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {
  154.             // not last page so display next link
  155.             $n=$s+$limit;
  156.            echo '<a href="'.$PHP_SELF.'?s='.$n.'&q='.$var.'">&nbsp;&nbsp; Next &nbsp;&nbsp;</a>';
  157.         }
  158.     }//end if $row_num_links_main > $limit
  159.  
  160.     // now you can display the results returned. But first we will display the search form on the top of the page
  161.  
  162.  
  163. }//end if search result
  164.  
  165.  
  166. ?>
FORM:
Expand|Select|Wrap|Line Numbers
  1. <form action="search.php" method="post">
  2. <input name="q" type="text" id="q" size="60" />
  3. <input name="search2" type="submit" id="search2" value="Search" />        
  4. </form>
Feb 29 '12 #1
1 2521
Dormilich
8,658 Expert Mod 8TB
does your compiled query look like described in the manual for that case?

(you can check with echo or var_dump())
Feb 29 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Joshua Beall | last post by:
Hi All, I have heard other people say that PHP can parse double quoted strings (e.g., "Hello, World") faster than it can parse single quoted strings (e.g., 'Hello, World'). This seems backwards...
2
by: Ramki | last post by:
Hi, I want to call a PERL program from MS-DOS batch file. Following is just an example, the string may inturn have double quote or single quote. I can't call with arg1, arg2 etc, as the input...
1
by: G.A.Mashaqbeh via DotNetMonster.com | last post by:
can anybody help me?? using the mouse i let the user to draw a shape about a set of points, i want to define the points enclosed within this shape, the shape is irregular, i have saved all the...
7
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem...
3
by: Terrence Chan | last post by:
I have try the below and the problem is my xmlstring is something like "<employee name="Joh"n"> strXML = Replace(strXML, "&", "&amp;") strXML = Replace(strXML, "<", "&lt;") strXML = Replace(strXML,...
4
by: Kevin Thomas | last post by:
Hi there, If I have a string var, strFoo that contains double-quotes such that it looks like this: I "love" VB What do I pass into the "replace" method to replace the double-quotes with...
0
NeoPa
by: NeoPa | last post by:
Background Whenever code is used there must be a way to differentiate the actual code (which should be interpreted directly) with literal strings which should be interpreted as data. Numbers don't...
6
by: Jay | last post by:
I need to convert from a string a double that is followed by a scaling character (k means *1e3, M=*1e6, etc) then apply the scaling character. Example: "-1.345k #comment" I know roughly how...
4
by: zacks | last post by:
I need to check the current value of a string value to see if it either starts with or ends with a double quote character, as in "123" (where the quotes are actual contens of the string). I thought...
1
by: Abhishek Bhatt | last post by:
Need to export table into csv file. Tried following: DoCmd.TransferText acExportDelim, "", TABLENAME, "C:\abc.csv", False, "" But the exported file contains data enclosed in double quotes i.e....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.