I have a search scripts with some basic functions like pagination, highlight the search quiree term. But what I am looking to have in the script is additional following three options.

1- When search result comes empty then it displays the Message "There is no result for the term "XYZ"" where XYZ would be a search term.

2- I should be able to identify minimum number of alphabets used in the search term. e.g. if someone search "ab" then it displays the message that "there should be minimum of 3 characters in a search term"

3- The World we search should be metioned at top & should be of different color then rest of the text in search result e.g. "result 1-10 of about 159 for "XYZ"" and on 3 page it would be e.g. "result 21-30 of about 159 for "XYZ""

4- This is not compusory but if I can have verification image option for running search then it would be amaizing...

Can someone please help me with these. Script that I have is somewhat like following

<?php 
    include_once("search.php"); 
//check to make sure that the $_GET['search'] variable is set and equal to "search" - this'll stop the server from throwing an error on first visit to the page 
    if((isset($_GET['search'])) || (isset($_GET['offset']))){ 
        $keyword = $_GET['keyword'];
//check to see if an offset has been pased through - if not, this is our first results page, so we need to set the offset to 0 
        if(isset($_GET['offset'])){ 
            $offset = $_GET['offset']; 
        }else{ 
            $offset = 0; 
        } 
//set up an incrementor variable to create an associative array we can crawl in the main html portion of the page later 
        $i=0; 
        $query = "SELECT * FROM ABCDEF WHERE '$VWXYZ' OR '$url' OR LMNOPQ LIKE '%$keyword%' LIMIT $offset, 10;"; 
//check to make sure that there are results - if not, print the error message for debugging 
        if($recordset = mysql_query($query)){ 
//find out how many records are the in the table that meet the criteria 
            $query2 = "SELECT COUNT(id) AS rws FROM ABCDEF WHERE '$VWXYZ' OR '$url' OR LMNOPQ LIKE '%$keyword%';"; 
            $recordset2 = mysql_query($query2); 
            $line2 = mysql_fetch_assoc($recordset2); 
            $number_records = $line2['rws']; 
//retrieve the records from our first query (with the limit clause) 
            while($line = mysql_fetch_assoc($recordset)){ 
//the foreach loop is simpler to code than manually associating each resulting record to a variable of the same name 
                foreach($line as $key=>$var){ 
                    $results[$i][$key] = $var; 
                    if($key == "LMNOPQ"){ 
//make the keyword red and bold in the LMNOPQ result 
                        $results[$i]['LMNOPQ'] = preg_replace("/$keyword/i", "<b><font color="red">$keyword</font></b>", $var); 
                    } 
                }
//next row in the return array 
                $i++; 
            } 
        }else{ 
            print(mysql_error()); 
        } 
//now we can figure out how many pages of results there are - we set the LIMIT to 10 records, so this page will always return at most 10 records 
//    Note that we're rounding up with the ceil() function - this will give us an additional page if there aren't a number of rows divisible evenly by 10 
        $pages = ceil($number_records / 10); 
//now figure out which page we're on currently 
        $current_page = ($offset / 10) + 1; 
    } 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title>Mushy Search</title> 
</head> 
<body> 
<form action="ABCDEF_Search.php" method="get" name="search"> 
    <input name="search" type="hidden" value="search"> 
    <input name="keyword" type="text" value="keyword" size="30" maxlength="30"> 
    <input name="search" type="submit" value="search"> 
</form> 
<br />
<?php 
    if(isset($_GET['offset']) || ($_GET['search'] == "search")){ 
        for($i=0; $i<count($results); $i++){ 
            print("<a href="".$results[$i]['url']."">".$results[$i]['title']."</a><br /><i>".$results[$i]['LMNOPQ']."</i>"."<hr />"); 
        } 
    } 
//here's where we set up the pagination - it's pretty simple, really. We'll use $current_page and $pages to figure out where we are and where we want to go 
//    check to make sure we're actually outputting a search result and this isn't the first time the page is being hit 
    if(isset($current_page)){ 
//    if the page we're on is not the first page of results, display the previous link 
//        (subtract 10 from the currently set offset to get the new offset to pass to the query) 
        if($current_page > 1){ 
            print("<a href="ABCDEF_Search.php?offset=".($offset - 10)."&keyword=$keyword">[Previous]</a>&nbsp;"); 
        } 
//    now we spit out page numbers for each page of the result and set up the offset to pass to the query 
        for($i=1; $i<=$pages; $i++){ 
            if($i != $current_page){ 
//    because we're starting the incrementor ($i) at 1 instead of 0, we need to subtract one before multiplying by 10 to get the actual offset for the page in question 
                print("<a href="ABCDEF_Search.php?offset=".(($i - 1) * 10)."&keyword=$keyword">".$i."</a>&nbsp;"); 
            }else{ 
//    no need to have a link to the current page, right? 
                print("$current_page&nbsp;"); 
            } 
        } 
//    as long as the current page is not the last page in the results, display the next link 
//        (add 10 to the current offset to get the new offset to pass to the query) 
        if($current_page < $pages){ 
            print("<a href="ABCDEF_Search.php?offset=".($offset + 10)."&keyword=$keyword">[Next]</a>"); 
        } 
    } 
?> 
</body> 
</html>

I will be highly greatful for the help...

    [qoute]
    1- When search result comes empty then it displays the Message "There is no result for the term "XYZ"" where XYZ would be a search term.
    [/qoute]

    //if you doing multiple queries put them all in the array then count them
    if(count($search_array) > 0){ 
       $search = false;
       $msg = 'Search returned no results.';
    }
    //or if you are only getting one result set you can check it right after the query.
    if(mysql_num_rows($result) > 0){ 
       $search = false;
       $msg = 'Search returned no results.';
    }
    

    [qoute]
    2- I should be able to identify minimum number of alphabets used in the search term. e.g. if someone search "ab" then it displays the message that "there should be minimum of 3 characters in a search term"
    [/qoute]

    //check the search term length
    if(strlen($search_term) <= 3){
       //set a flag to false, so you can check against it later and echo the msg instead of the search resutls. 
       $search = false;
       //what gets echoed instead of search results.
       $msg = 'Search string must be greater then three characters.';
    }
    

    [qoute]
    3- The World we search should be metioned at top & should be of different color then rest of the text in search result e.g. "result 1-10 of about 159 for "XYZ"" and on 3 page it would be e.g. "result 21-30 of about 159 for "XYZ""
    [/qoute]

    //once you have all the search results count them and assign to a var.
    $search_resutls = count($results);
    //just combine all the vars into a header and echo it before the foreach that echos the actual results.
    $header = "Showing ".$offset1." - ".$offset2." of ."$search_results;
    

    I hope the comments will be enough to help you see where these chunks should go, if not let me know and I can break it down further. Hope this helps!

      Thanks for the reply. But I don't know how to put these codes in. I tried copy & pasting the codes you have mentioned into the script but it doesn't seem to work. Is there some other coding needs to be done as well?... I don't have any knowledge in php but I desperately need this script... If you can put the codes in the script that I copy & pasted above & give me the final script then I will be highly greatful... In the way, I will also have an idea how those things work... Or if you can tell me in modification style (step by step modification of script) then it will be even greater...

        I put my comments with a # so you can easily find what I've added and why.

        <?php 
            include_once("search.php"); 
            $error = '';
            $search = false;
        
           //check to make sure that the $_GET['search'] variable is set and equal to "search" - this'll stop the server from throwing an error on first visit to the page 
            if((isset($_GET['search'])) || (isset($_GET['offset']))){ 
                $keyword = $_GET['keyword'];
        
            #this checks the search string to see how long it is.
            if(strlen($keyword) <= 3){
               #less then or equal to 3, set flag to not search, and set an error msg to show user.
               $search = false;
               $error = "Your search term must be longer then 3 characters.<br>";
            }else{
               #else it's long enough, set flag to search.
               $search = true;
            }
           //check to see if an offset has been pased through - if not, this is our first results page, so we need to set the offset to 0 
                if(isset($_GET['offset'])){ 
                    $offset = $_GET['offset'];
                }else{ 
                    $offset = 0; 
                } 
        
            #check our flag to see if we shoudl search
            if($search == true){
                //set up an incrementor variable to create an associative array we can crawl in the main html portion of the page later 
               $i=0; 
               $query = "SELECT * FROM ABCDEF WHERE '$VWXYZ' OR '$url' OR LMNOPQ LIKE '%$keyword%' LIMIT $offset, 10;"; 
                //check to make sure that there are results - if not, print the error message for debugging 
               if($recordset = mysql_query($query)){ 
                   //find out how many records are the in the table that meet the criteria 
                   $query2 = "SELECT COUNT(id) AS rws FROM ABCDEF WHERE '$VWXYZ' OR '$url' OR LMNOPQ LIKE '%$keyword%';"; 
                   $recordset2 = mysql_query($query2); 
                   $line2 = mysql_fetch_assoc($recordset2); 
                   $number_records = $line2['rws']; 
        
                   #check to make sure there are records.
                   if($number_records > 0){
                      //retrieve the records from our first query (with the limit clause) 
                      while($line = mysql_fetch_assoc($recordset)){ 
                         //the foreach loop is simpler to code than manually associating each resulting record to a variable of the same name 
                          foreach($line as $key=>$var){ 
                              $results[$i][$key] = $var; 
                              if($key == "LMNOPQ"){ 
                                  //make the keyword red and bold in the LMNOPQ result 
                                  $results[$i]['LMNOPQ'] = preg_replace("/$keyword/i", "<b><font color="red">$keyword</font></b>", $var); 
                              } 
                          }
                            //next row in the return array 
                          $i++; 
                      }
                   }else{
                      #gonna use this flag for displaying rows too.
                      $search = false;
                      $error .= "No results were found for ".$keyword."<br>";
                   }
        
               }else{ 
                   print(mysql_error()); 
               } 
                //now we can figure out how many pages of results there are - we set the LIMIT to 10 records, so this page will always return at most 10 records 
                //    Note that we're rounding up with the ceil() function - this will give us an additional page if there aren't a number of rows divisible evenly by 10 
               $pages = ceil($number_records / 10); 
               //now figure out which page we're on currently 
               $current_page = ($offset / 10) + 1; 
               //setup the search header (display blah of blah with blah resutls)
               #page 3 first result is number 30
               $record_start = $current_page * 10;
               #page 3 last result is the first + 10, or 40.
               $record_end = $record_start + 10;
        
               $search_header = "Display ".$record_start." - ".$record_end." of ".$number_records;
           }
        
        } 
        ?> 
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
        <html> 
        <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
        <title>Mushy Search</title> 
        </head> 
        <body> 
        <form action="ABCDEF_Search.php" method="get" name="search"> 
            <input name="search" type="hidden" value="search"> 
            <input name="keyword" type="text" value="keyword" size="30" maxlength="30"> 
            <input name="search" type="submit" value="search"> 
        </form> 
        <br />
        <?php 
            #search is still our flag, check it to make sure we have search results.
            if($search){ 
                #print out the "showing x - y of z" deal.
                print($search_header);
                for($i=0; $i<count($results); $i++){ 
                    print("<a href=\"".$results[$i]['url']."\">".$results[$i]['title']."</a><br /><i>".$results[$i]['LMNOPQ']."</i>"."<hr />"); 
                } 
            }else{
               #so search is false, that means we have an error to show the user.
               print($error);
            }
           //here's where we set up the pagination - it's pretty simple, really. We'll use $current_page and $pages to figure out where we are and where we want to go 
           //    check to make sure we're actually outputting a search result and this isn't the first time the page is being hit 
            if(isset($current_page)){ 
        //    if the page we're on is not the first page of results, display the previous link 
        //        (subtract 10 from the currently set offset to get the new offset to pass to the query) 
                if($current_page > 1){ 
                    print("<a href=\"ABCDEF_Search.php?offset=".($offset - 10)."&keyword=$keyword\">[Previous]</a>&nbsp;"); 
                } 
        //    now we spit out page numbers for each page of the result and set up the offset to pass to the query 
                for($i=1; $i<=$pages; $i++){ 
                    if($i != $current_page){ 
        //    because we're starting the incrementor ($i) at 1 instead of 0, we need to subtract one before multiplying by 10 to get the actual offset for the page in question 
                        print("<a href=\"ABCDEF_Search.php?offset=".(($i - 1) * 10)."&keyword=$keyword\">".$i."</a>&nbsp;"); 
                    }else{ 
        //    no need to have a link to the current page, right? 
                        print("$current_page&nbsp;"); 
                    } 
                } 
        //    as long as the current page is not the last page in the results, display the next link 
        //        (add 10 to the current offset to get the new offset to pass to the query) 
                if($current_page < $pages){ 
                    print("<a href=\"ABCDEF_Search.php?offset=".($offset + 10)."&keyword=$keyword\">[Next]</a>"); 
                } 
            } 
        ?> 
        

          This is sooooo great. It's working like a charm but there is one problem with the script. Can you help me with that also? The thing is when I am running a search & if it comes out 280 (or any big number) of pages of results. Then all the pagination is coming in one line & distroy all the template of the page.... For example I have used term "Form" multiple times in the DB. So when I run a search term for "form" then it came with > 200 page results in pagination. All the pagination appearing at the bottom made the page toooo wide, destroying the look of the template. Can we make it like if search result have > 50 pages in pagination then it makes new rows for every fifty. For example in result of 286 page, 1st,2nd,3rd,4th & 5th row will have 50, 6th row will have 36 pagination number... Thanks for great help, if you can help me with this one too the it will be highly greatful...

            From the code above:

            //    now we spit out page numbers for each page of the result and set up the offset to pass to the query
                    for($i=1; $i<=$pages; $i++){
                        if($i != $current_page){ 
            

            Modify the above to look like this:

            //    now we spit out page numbers for each page of the result and set up the offset to pass to the query
                    for($i=1; $i<=$pages; $i++){
                        if (($i % 50) == 0)
                            echo "<br>";
                        if($i != $current_page){ 

            The way it works is as follows...
            %-operator (modulo-operator) returns the remainder of a division so that
            0%3 = 0 (not a problem since $i starts at 1, not 0)
            1%3 = 1
            2%3 = 2
            3%3 = 0 (btw, you read it as 3 modulo 3)

            So, for every 50th page we print a line break: <br>

              Thanks. It's great. But if I like it that it won't divide in fiftys but else it just follow the html table size & just put extra ones in the new lines. Then how can that be done? Because when I divide it with 50 it still ruin the page layout since 1-9 is only one digit, 10-99 are 2 digits & 100 onwards are 3 digits... It still is destroying a template structure & making the html table wider..

                Above problem is solved. I made use of the code that johanafm mentioned & i used it as

                //    now we spit out page numbers for each page of the result and set up the offset to pass to the query 
                        for($i=1; $i<=$pages; $i++){ 
                            if (($i % 1) == 0) 
                                echo " "; 
                            if($i != $current_page){

                It took care of the pagination very well this way.....

                Thanks both of you, you had been really great help... I really appreciate it. This thing is I like about phpbuilder that when ever I need help, I get here... I really love this forum...

                  7 months later

                  I am having the problem with the following code from the above Protato post http://phpbuilder.com/board/showpost.php?p=10817663&postcount=4

                  //now we can figure out how many pages of results there are - we set the LIMIT to 10 records, so this page will always return at most 10 records 
                              //    Note that we're rounding up with the ceil() function - this will give us an additional page if there aren't a number of rows divisible evenly by 10 
                             $pages = ceil($number_records / 10); 
                             //now figure out which page we're on currently 
                             $current_page = ($offset / 10) + 1; 
                             //setup the search header (display blah of blah with blah resutls) 
                             #page 3 first result is number 30 
                             $record_start = $current_page * 10; 
                             #page 3 last result is the first + 10, or 40. 
                             $record_end = $record_start + 10; 
                  
                         $search_header = "Display ".$record_start." - ".$record_end." of ".$number_records;

                  I never realized the problem before until one of my user contacted me. The problem is when we run some search quiree then it's showing "Display 10-20 of 59" on first result page instead of "Display 1-10 of 59" and in similar fashion it's showing "Display 60-70 of 59" on last page instead of "Display 50-59 of 59"... Similarly if some search word shows < 10 number of result e.g. 7 words the it's showing "Display 10-20 of 7" insatead of "Display 1-7 of 7"...

                  I really need the fix for this, if someone can help me with that then it will be great help...

                    Well, look at what happens when you're on page 1. $offset==0 in that case.

                    $current_page = ($offset/10)+1; // $current_page = 1;
                    $record_start = $current_page*10; // $record_start = 10;
                    $record_end = $record_start+10; // $record_end=20
                    

                    There are two significant problems here.

                    The first is that you add one and then multiply by ten, which is the same as multiplying by ten and then adding ten (think about it: ($c+1)10 == $c10+10) so that's why page 1 starts with "10" instead of "1".

                    The second is that $record_end is ten more than $record_start when it should only be nine. (think about it: 10-1==9. Thinking is good. Helps prevent mistakes.)

                    $pages = ceil($number_records/10);
                    // If we count pages starting from 0, then record 153 will be on page 15.
                    $current_page = floor($offset/10);
                    // And page 4 has records 40..49
                    $record_start = $current_page * 10;
                    $record_end = $record_start + 9;
                    // But Muggles don't like to start counting at zero.
                    $current_page = $current_page + 1;
                    

                      Nice, Thanks. But one problem remains. Which is that on the last page it shows all the range in 10s. For example it's showing "Display 20-29 of 25" instead of "Display 20-25 of 25" when result has 25 entries. Similarly is result has less than 10 entries then on first page it's showing 0-10 instead of 0-7 or 1-7 when result has seven entries. If someone can help me with that also then it will be great help for me.

                        Then add 1 to $record_start. In fact, it's easier to work exclusively with [0..n-1] and only use [1..n] for display.

                        echo "Display ".($record_start+1)." to ".($record_end+1)." of ".$number_of_records;

                        If you change the index range any sooner you'll only need to change it back again anyway before you can do anything more with it.

                          Well, I got hold of the above problem with

                                     $pages = ceil($number_records/10);
                                     // If we count pages starting from 0, then record 153 will be on page 15.
                          		   $current_page = floor($offset/10);
                          		   // And page 4 has records 40..49
                          		   $record_start = $current_page * 10 + 1;
                          		   $record_end = $record_start + 9;
                          		   // But Muggles don't like to start counting at zero.
                          		   $current_page = $current_page + 1;

                          But that's not my problem any more. I am facing other problem i.e. page entries range are shown according to the page number irrespective of number of entires. Whatever the last page is it will show the range from 11-20 or 21-30 or something like that but what I want it on last page it gives us the range according to the number of entries i.e. if there are 15 entries then it will show 11-15 on last page instead of 11-20. I hope you understand what I am trying to stay here. Any help in this regard will be highly greatful.

                            Write a Reply...