N
NoobPHP

  • Sep 19, 2005
  • Joined Jul 12, 2005
  • seriously are there any other ways of doing it other than using FULLTEXT search.

    • I'm working on a search keyword textfield where user inputs the value and does a search.
      How should i do it in a way that the results returned are displayed with the most matches to the least..

      that is to say.. user does a search on "1mm crack"
      results should return in a way that all "1mm crack" are displayed first, followed by results of all "1mm" or "crack"

      where the search is done

      $Keyword = (isset($_GET['Keyword'])) ? $_GET['Keyword'] : '';
      $trimmedKeyword = trim($Keyword);
      //trim whitespace from the stored variable
      $trimmedKeyword = preg_replace('#\s+#',' ',$trimmedKeyword);
      $trimmed_arrayKeyword = explode(" ",$trimmedKeyword);
      
      
      // Build SQL Query for each keyword entered
      foreach ($trimmed_arrayKeyword as $trimmKeyword => $word){
      if(strtolower($word ) == 'b'){
      $trimmed_arrayKeyword[$trimmKeyword] = "/\b(?<!<)($word)(?!>)\b/i";
      }//end if
      else $trimmed_arrayKeyword[$trimmKeyword] = '/\b('.preg_quote($word,'/').')\b/i';
      
      $queryKeyword = "SELECT * FROM report WHERE AircraftTailNo REGEXP '[[:<:]]{$word}[[:>:]]' OR DefectInfo REGEXP '[[:<:]]{$word}[[:>:]]' OR Rectifications REGEXP '[[:<:]]{$word}[[:>:]]' OR Comments REGEXP '[[:<:]]{$word}[[:>:]]' OR RelatedDoc REGEXP '[[:<:]]{$word}[[:>:]]'";
      $numresultsKeyword =mysql_query ($queryKeyword);
      $rowKeyword= mysql_fetch_array ($numresultsKeyword);
      
      do{
      $adid_array[] = $rowKeyword[ 'ReportID' ];
      }
      while( $rowKeyword= mysql_fetch_array($numresultsKeyword));
      } //end foreach
      

      where the results are displayed

      while ($row = mysql_fetch_array($resultAircraftDisplay)) {
      $aircraftType     =  $row['1'];
      $aircraftTailNo   = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['2']);
      $airFrameHours    =  $row['3'];
      $servicingType    =  $row['4'];
      $date = explode('-', $row['5']);
      $actual_date = $date[2].'-'.$date[1].'-'.$date[0];
      $defectInfo       = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['6']);
      $rectifications   = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['7']);
      $comments         = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['8']);
      $relatedDoc       = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['9']);
      
      //if there are 7 cols, write the <tr> tag and reset the counter
      if ($cntr == 2) {
      echo "</tr><tr>";
      $cntr =1;
      }
      echo "
      <td>$aircraftType   </td>
      <td>$aircraftTailNo </td>
      <td>$airFrameHours  </td>
      <td>$servicingType  </td>
      <td>$actual_date    </td>
      <td>$defectInfo     </td>
      <td>$rectifications </td>	
      <td>$comments     </td>
      <td>$relatedDoc    </td>
      
      • Does filtering of certain characters make a search engine more efficient?

        Speical characters such as ~!@#$%&*()_+

        • to add on to the above :
          Wads the ='b' for ? :if(strtoupper($searchword ) == 'b')

          foreach ($trimmed_arrayKeyword as $trimmKeyword => $searchword)
          {
          
          if(strtoupper($searchword ) == 'b')
          {
          $trimmed_arrayKeyword[$trimmKeyword] = "/\b(?<!<)$searchword(?!<)\b/i";
          continue;
          }
          
          else $trimmed_arrayKeyword[$trimmKeyword] = '/\b('.preg_quote($searchword,'/').')\b/i';
          
          • Anyone knows which part of the codes is causing such problem

            • I'm sorry hope this would help

              Warning: preg_replace(): Compilation failed: missing ) at offset 27 in c:\inetpub\wwwroot\sip\review.php on line 427
              Warning: preg_replace(): Compilation failed: missing ) at offset 27 in c:\inetpub\wwwroot\sip\review.php on line 432 to Line 435

              Line 432 to Line 435
              Line 432 - $date = explode('-', $row['5']);
              Line 433 - $actual_date = $date[2].'-'.$date[1].'-'.$date[0];
              Line 434 - $defectInfo = preg_replace($trimmed_arrayKeyword, '<b>\1</b>' , $row['6']);
              Line 435 - $rectifications = preg_replace($trimmed_arrayKeyword, '<b>\1</b>' , $row['7']);

              $Keyword = (isset($_GET['Keyword'])) ? $_GET['Keyword'] : '';
              $trimmedKeyword = trim($Keyword);
              $trimmedKeyword = preg_replace('#\s+#',' ', trim($trimmedKeyword));//trim extra whitespaces from the stored variable
              $trimmed_arrayKeyword = explode(" ",$trimmedKeyword);
              
              foreach ($trimmed_arrayKeyword as $trimmKeyword => $word){
              if(strtolower($word)){
              $trimmed_arrayKeyword[$trimmKeyword] = "/\b(?<!<)($word)(?!>)\b/i";
              }//end if
              else $trimmed_arrayKeyword[$trimmKeyword] = '/\b('.preg_quote($word,'/').')\b/i'; 
              
              while ($row = mysql_fetch_array($resultAircraftDisplay)) { 
              $aircraftType     =  $row['1'];       
              $aircraftTailNo = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['2']); $airFrameHours = $row['3']; $servicingType = $row['4']; $date = explode('-', $row['5']); $actual_date = $date[2].'-'.$date[1].'-'.$date[0]; $defectInfo = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['6']); $rectifications = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['7']); $comments = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['8']); $relatedDoc = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['9']);
              • Anyone knows what error this is?

                Warning: preg_replace(): Compilation failed: missing ) at offset 27 in c:\inetpub\wwwroot\review.php on line 429

                The errors are coming from those lines below

                $aircraftType  =  $row['1'];       
                $aircraftTailNo = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['2']); $airFrameHours = $row['3']; $servicingType = $row['4']; $date = explode('-', $row['5']); $actual_date = $date[2].'-'.$date[1].'-'.$date[0]; $defectInfo = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['6']); $rectifications = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['7']); $comments = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['8']); $relatedDoc = preg_replace($trimmed_arrayKeyword, '<b>\\1</b>' , $row['9']);
                • i guess else if would do the job

                  • How should i allow the display of Date of Occurence in such a way

                    If user keys in a value range between $beginDate and $endDate
                    it displays this :
                    Date Of Occurence between: 23-03-2002 to 24-02-2005

                    if (!empty($beginDate)) {
                    echo "<b>Date Of Occurence between: $beginDate1 to $endDate1</b><br>";  
                    }

                    and if user keys in value for the $endDate
                    it displays this:
                    Date Of Occurence before: 24-02-2005

                    if (!empty($endDate)) {
                    echo "<b>Date Of Occurence before:  $endDate1</b><br>";  
                    }

                    i tried using else if but the codes are within a else{} so can't use else again

                    else{
                    
                    // display of search results
                    }
                    if (!empty($beginDate) && !empty($endDate)) {
                    echo "<b>Date Of Occurence between: $beginDate1 to $endDate1</b><br>";  
                    } if (!empty($endDate)) { echo "<b>Date Of Occurence before: $endDate1</b><br>";
                    } }//end else
                    • I'm doing a search between the AirframeHours records..

                      Why is it that my query allows me to search values below or between the AirframeHours.. but it doesnt allow me to do a search above that value.

                      That is to say.. user keys in 2000 onto AirframeHours2(the second textfield) and it returns records that are below 2000

                      But when user keys in 100 onto AirframeHours and it returns no records that are more than 100

                      if (!empty($_GET['AirFrameHours'])){ 
                      $queryAircraftDisplay  .= " and AirFrameHours>='".$_GET['AirFrameHours']."' and AirFrameHours <= '".$_GET['AirFrameHours2']."'"; 
                      }
                      
                      if (!empty($_GET['AirFrameHours2'])){ 
                      $queryAircraftDisplay  .= " and AirFrameHours>='".$_GET['AirFrameHours']."' and AirFrameHours <= '".$_GET['AirFrameHours2']."'"; 
                      }
                      
                      • Oh.. i'm working on a optional search.. so it is not required that all of the fields any to be filled in..

                        Anyway thanks.. i got that figured out.. =)

                        • I'm doing a search between dates here.
                          I'm having trouble restricting the display of my records..
                          The problem i'm having here is when user selects the year of the endDate and sends the form..
                          all records were displayed which should not be the case
                          What it should have been is "Please enter a valid search.." even if the the year of endDate been selected

                          i tried adding the ($day =="") && ($month =="") but just couldnt get it working..

                          $month = isset($REQUEST['month1']) ? $REQUEST['month1'] : '';
                          $day = isset($REQUEST['day1']) ? $REQUEST['day1'] : '';

                          if(($AircraftType =="") && ($ServicingType == "") &&  ($AirFrameHours == "") && ($AirFrameHours2 == "") && ($Keyword == "") && ($endDate =="") && ($day =="") && ($month =="")) { 
                             //set the result message display to Search Error Please enter a search if all values are empty 
                             $resultmsg = " Please enter a valid search.." ; 
                          } 
                          
                          
                          
                          $day = isset($_REQUEST['day']) ? $_REQUEST['day'] : ''; 
                          $month = isset($_REQUEST['month']) ? $_REQUEST['month'] : ''; 
                          $year = isset($_REQUEST['year']) ? $_REQUEST['year'] : ''; 
                          
                          //$beingDate is set to empty space if the variables $day,$month and $year are empty 
                          $beginDate = ''; 
                          if(!empty($day) && !empty($month) && !empty($year)) { 
                          //include the dash if the variables $day,$month and $year are not empty 
                             $beginDate = $year . '-' . $month . '-' . $day; 
                          
                          } // end if 
                          
                          
                          
                          
                          $year = date("Y"); 
                          $selected_year = isset($_REQUEST['year1']) ? $_REQUEST['year1'] : ''; 
                          echo "<select name='year1'>\n"; 
                          echo "<option value='' >-</option>"; 
                          for ($n=$year;$year-10<=$n;$n--) { // gets the current year and do a subtraction of 10 to display the array of descending 10 years 
                          if($n==$selected_year){ 
                          echo " <option value=\"$n\" selected>$n</option>"; } 
                          else { echo " <option value=\"$n\">$n</option>"; } 
                          } // end for 
                          echo "</select>\n"; 
                          
                          
                          $day = isset($_REQUEST['day1']) ? $_REQUEST['day1'] : ''; 
                          $month = isset($_REQUEST['month1']) ? $_REQUEST['month1'] : ''; 
                          $year = isset($_REQUEST['year1']) ? $_REQUEST['year1'] : ''; 
                          
                          /****************************************************** 
                          include the dash if the variables are not empty 
                          
                          ******************************************************/ 
                          $endDate = ''; 
                          if(!empty($day) && !empty($month) && !empty($year)) { 
                             $endDate = $year . '-' . $month . '-' . $day; 
                          } // end if 
                          
                          
                          • so is this the way i should do it

                            
                            
                            $year = date("Y"); //get the year from $today 
                            echo "<select name='year1'>\n"; 
                            echo '<option value="" selected">-</option>'; 
                            echo <option if($year == $year-10){ echo " selected"; } value=" echo $year-10"> echo $year-10</option>;
                            echo <option if($year == $year-9){ echo " selected"; }  value=" echo $year-9"> echo $year-9</option>;
                            echo "</select>\n"; 
                            
                            

                            or

                            // Displaying the Year in the DateOfOccurence drop down box
                            $year = date("Y"); //get the current year from the system
                            echo "<select name='year'>\n"; 
                            echo '<option value="" selected">-</option>'; 
                            for ($n=$year;$year-10<=$n;$n--) { // gets the current year and do a subtraction of 10 to display the array of descending 10 years 
                            echo " <option value=\"$n\">$n</option>"; 
                            } // end for
                            echo "</select>\n";
                            
                            
                            <?php 
                              $c_year = date("Y"); 
                            ?> 
                            
                            <select name="year"> 
                              <option <?php if($year == $c_year-10){ echo " selected"; } ?> value="<?php echo $c_year-10 ?>"><?php echo $c_year-10 ?></option> 
                              <option <?php if($year == $c_year-9){ echo " selected"; } ?> value="<?php echo $c_year-9?>"><?php echo $c_year-9?></option> 
                            </select> 
                            
                            • chrislive .. How should work with a loop that would allow me to use your method?

                              <select name="year"> 
                                <option <?php if($year== "2000"){ echo " selected"; } ?> value="2000">2000</option> 
                                <option <?php if($year== "2001"){ echo " selected"; } ?> value="2001">2001</option> 
                                <option <?php if($year== "2002"){ echo " selected"; } ?> value="2002">2002</option> 
                                <option <?php if($year== "2003"){ echo " selected"; } ?> value="2003">2003</option> 
                                <option <?php if($year== "2004"){ echo " selected"; } ?> value="2004">2004</option> 
                                <option <?php if($year== "2005"){ echo " selected"; } ?> value="2005">2005</option> 
                              </select> 
                              

                              As my year is captured from the system date

                              Could something like this work?

                              
                              <option <?php if($year== "($_GET['year']"){ echo " selected"; } ?> value="$_GET['year']">$year</option> 
                              
                              
                              • Or you could simply set the variables in the forms to what you recieved in your POST Arguments, in your case of drop down boxes that would be setting the selected attribute in the right place,

                                $month = isset($_REQUEST['month']) ? $_REQUEST['month'] : '';	
                                if ($key == $month){
                                $select .= " selected>".$val."\n";
                                }// end if
                                

                                So this is what i did to retain the values of the month
                                but why is it after i sent the form the value minus 1.

                                that is to say.. i sent 12 the value displayed is 11

                                $month = array (1=>"-","01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
                                $select = "<select name=\"month1\">\n";
                                foreach ($month as $key => $val) {
                                $select .= "\t<option val=\"".$key."\"";
                                $month = isset($_REQUEST['month']) ? $_REQUEST['month'] : '';	
                                if ($key == $month){
                                $select .= " selected>".$val."\n";
                                }// end if
                                else{
                                $select .= ">".$val."\n";
                                }// end else
                                }// end foreach
                                $select .= "</select>";
                                echo $select;
                                

                                As for my year, how should it be done?

                                // Displaying the Year in the DateOfOccurence drop down box
                                $year = date("Y"); //get the current year from the system
                                echo "<select name='year'>\n"; 
                                echo '<option value="" selected">-</option>'; 
                                for ($n=$year;$year-10<=$n;$n--) { // gets the current year and do a subtraction of 10 to display the array of descending 10 years 
                                echo " <option value=\"$n\">$n</option>"; 
                                } // end for
                                echo "</select>\n";  
                                }// end function
                                • You mean when this <select> list is redisplayed?

                                  Yes it is.. I wish to display the values selected after the form been sent

                                  just test $key against whatevever month was supplied instead of just 1 (but continue to use 1 if it hasn't been supplied)

                                  You means this
                                  $key == 1
                                  But the 1 is for the array.. So what should i do?

                                  • I'm trying to retain the values of the month and year after the form been sent.. Anyone know how i should do it?

                                    User gets to select 02 for month and 2001 for year..
                                    After it is been sent the values go back to - , what i want here is to retain the values of both month and year, 02 and 2001 that is.

                                    $month = array (1=>"-","01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
                                    $select = "<select name=\"month\">\n";
                                    foreach ($month as $key => $val) {
                                    $select .= "\t<option val=\"".$key."\"";
                                    if ($key == 1){
                                    $select .= " selected>".$val."\n";
                                    }//end if
                                    else{
                                    $select .= ">".$val."\n";
                                    }// end else
                                    }// end foreach
                                    $select .= "</select>";
                                    echo $select;
                                    
                                    // Displaying the Year in the DateOfOccurence drop down box
                                    $year = date("Y"); //get the current year from the system
                                    echo "<select name='year'>\n"; 
                                    echo '<option value="" selected">-</option>'; 
                                    for ($n=$year;$year-10<=$n;$n--) { // gets the current year and do a subtraction of 10 to display the array of descending 10 years 
                                    echo " <option value=\"$n\">$n</option>"; 
                                    } // end for
                                    echo "</select>\n";  
                                    }// end function
                                    • I tried to do something like this But it is still not working ..

                                      $filter = array("\"" , "\\" , "!", "@" , "$" , "%" , "^" , "&" , "*" , "(", ")" , "+" , "{" , "}" , "[" , "]" ,  "<" , ">" ,  "?" , "/" , "|" , "=");
                                      $trimmedKeyword = str_replace($filter, '', $trimmedKeyword);
                                      
                                      if (isset($filter)){
                                      	$trimmedKeyword = preg_replace('#\s+#','',$trimmedKeyword);
                                      }
                                      $trimmedKeyword = str_replace('"', '', $trimmedKeyword);
                                      
                                      
                                      if (empty($filter)){
                                      	$trimmedKeyword = preg_replace('#\s+#',' ',$trimmedKeyword);
                                      }	
                                      $trimmed_arrayKeyword = explode(" ",$trimmedKeyword);
                                      

                                      I searched "1mm Crack" but the keywords were joined together "1mmCrack"..
                                      I only want to trim the white spaces when $filter array special charcters were used

                                      • Thank i tried doing it your way

                                        $Keyword = (isset($_GET['Keyword'])) ? $_GET['Keyword'] : '';
                                        $trimmedKeyword = trim($Keyword);
                                        $trimmedKeyword = trim(preg_replace('/[\\\]~`!@$%^&*()+{}[<>?\/|=]/', '', $Keyword));
                                        

                                        I returned an error statement:

                                        Got error 'repetition-operator operand invalid' from regexp