A couple problems with a search page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

A couple problems with a search page

Post by Rovas »

A couple weeks ago I made a search page that for me worked great but I wanted to make some improvements: pagination and the results to be found even the user mispells the term. I made added them but unfortunetly they don' t work the way I wanted.
Let me elaborate I put the wildcard character % in SQL statement it show results for some mispelled words but not for all for targeted search (formed by 2 or more words in does give any results), and the pagination code doesn' t work i.e. the function responsible doesn' t display any results although the SQL statement is correct and no errors expect a notice (Undefined index) for the variable in which I put page number.
I have read the some (there were a lot of them) of the other posts on these matters but none provided me with a good answer.

Code: Select all

//variables
   $valid=false;
   //number of the page
   $nrp=1;
   //results shown
   $show=10;
  //current page 
   $nrc=0; 
   //number of rows in the query
    $nrt=0;
   //search term
   $term='';
   $urm='';
   $leg="";
   $sir="SELECT TABLE1.COLUMN1, TABLE2.COLUMN2, Table2.COLUMN3 FROM TABLE1 INNER JOIN TABLE2 ON Cid1=Cid2";
  if($_GET['page']==''){$pag=1;}
//verifing if the search button was pushed and the search term is correct
   if(!isset($_GET["Search"]) || $valid==false)
   {
	    header( "Expires: Mon, 20 Dec 2006 01:00:00 GMT" );
	    header( "Last-Modified: " .gmdate("D, d M Y H:i:s") . " GMT" );
	    header( "Cache-Control: no-cache, must-revalidate" );
	    header( "Pragma: no-cache" );
?>

  <form method='GET' action='<?php echo $_SERVER['PHP_SELF']?>'>
   <table>
   <tr>
       <td>The term which you want to search.<td>
       <td><input type='text' name='term' /><input type='submit' name='Search' value='Search' /></td>
       <!-- <td></td> -->
   </tr>

<?php

  	}
      //after a succesfull validation
     if ($valid==true)
     {      	
        $termen=addslashes($_GET['term']);
        //validate 
        $sir=$sir ." WHERE Column1 LIKE '%" .$term ."%' OR Column2 LIKE '%" .$term ."%' OR Column3 LIKE '%". $term."%' ";
         //count the results of the query  
        $nrt=countRows($sir);
        echo $nrt;
        $nrp=ceil($nrt/$shown);
        echo $nrp;
        if ($nrt==0)
        {
           echo "<h2>The term searched isn' t in our database</h2>";
 	}
	else 
       { 
          for ($i=0; $i<$nrp; $i++)
         {  				
	      if($pag==$i)     { $urm="<a href='search.php?page=" .$i . "'><strong>" .$i ."</strong></a>";  }
 	     else
 	     {
                   //the function that echo the results
                  selection($sir,$nrp);
 	         $urm .="<a href='search.php?page=" .$pag ."'>" .$i ."</a>";
              }
          }
        }
 //the function selection
   function selectie($s="", $n=0)
   {               
        $r=''; 
	$matAfis='';
        $show=10;
	$s=$s ." LIMIT " .(($n*$show)- $show) .", " .$show;
	$r=mysql_query($s) or die (mysql_error());
       while($r2=mysql_fetch_array($r))
       {
               echo "\t\t <tr>\r";
               echo "\t\t\t <td>" ."<a href='url1'>" .$r2['Column1'] ."</a>" ."</td>\r";
               echo "\t\t\t <td>" ."<a href='url2'>" .$r2["Column2"] ."</a>" ."</td>\r";
               echo "\t\t</tr>\r";
       }
   }
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Realized some mistakes made and modified some of the code but I was not able to solve pagination (it won' t display the links or the results than the first page) and adding a second word in the search term will not give a single result even if the data is there.
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Just some comments that might help.

Post by churt »

Based off your second post you may have already fixed these.

Where does $valid get set to a true condition? ( It kind of sounds like it is but I don't see it. Curious about this one.)

The selection function call is differnet name than the function.

How are you splitting up the words in the $term variable?

Might try:

Code: Select all

//Split word selections
list($term1, $term2, $term3)=split(' ', $term);

//Set where condition
"Where Column1 like '%$term1%' or Column2 like '%$term2%'  or Column3 like '%$term3%'";
The last thing I noticed is that the $urm variable gets set but not used anywhere.

Hope some of this helps.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

The valid variable becomes true once the validation is complete. I fix for two terms searching by corectly putting the character %.
But i still can' t make a corect pagination and another problem appeared the page doesn' t work in IE seems that IE doesn' t transmit GET for a button. I haven' t change the code for pagination because I can' t see the error.
Post Reply