user1835767
user1835767

Reputation: 29

How to reorder the Search Result?

When I search a word "apple" the script is showing the result in the following order:

(1) appletree (2) juiceapple (3) apple

But I want to reorder the search result in the following order:

(1) apple (2) appletree (3) juiceapple

As you can see the above order is exactly what I want in which the result is showing according to the searched term which was "apple"

//get date
$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)
   echo "Please fill out the form";
else
{
    if (strlen($search)<=2)
   echo "The item you searched for was to small";
    else
   {
     echo "You searched for <b>$search</b> <hr size='1'>";

     //connect to database

     mysql_connect('localhost','wnumber','passowrd');
     mysql_select_db('wnumber');


 //explode search term
           $search_exploded = explode(" ",$search);
           foreach($search_exploded as $search_each)
{
    $str = mysql_real_escape_string(substr($search_each, 0, 4));
    //construct query
    $x++;
    if ($x==1) $construct .= "keywords LIKE '$str%'";
    else       $construct .= " OR keywords LIKE '$str%'";
}




     //echo out construct
    $construct = "SELECT * FROM word WHERE $construct";
    $run = mysql_query($construct);
     $foundnum = mysql_num_rows($run);

     if ($foundnum==0)
        echo "No results found.";
     else
     {

       echo "$foundnum results found.<p><hr size='1'>";

       while ($runrows = mysql_fetch_assoc($run))
       {


        //get data
       $meaning = $runrows['meaning'];
       $keywords = $runrows['keywords'];

        echo "<b>$keywords</b><br>
       <b>$meaning</b><br>

       }

     }



    }
}

Upvotes: 1

Views: 162

Answers (5)

Buksy
Buksy

Reputation: 12238

I assume that you have multiple keywords for every row. You are getting all these results because you are searching for any words that contains word apple.

To get exact results first, and then only results that contain the word, do it like this:

  1. search for results that match exactly the word "apple"
  2. search for any results that contains word "apple"

As you have multiple keywords for every row, you can't use = operator. But you must use some separator in your table to separate the keywords from each other. Let's assume its "" (whitespace).

So if your table and column "keywords" looks like this for example

| id | name  | keywords |
| 1  | apple | apple fruit sweet |

You can search for results like this:

SELECT * FROM table t1
WHERE
 keywords LIKE "% $keyword %" /* Notice whitespaces around the variable */
 OR
 keywords LIKE "$keyword %" /* This is for keywords that arent separated from left */
 OR
 keywords LIKE "% $keyword" /* This is for keywords that arent separated from right */

UNION DISTINCT /* Distinct is there because we want to show every result just once */

SELECT * FROM table t1
WHERE
 keywords LIKE "%$keyword%"

PS: separator could be any string or character, it doesnt have to be whitespace

Upvotes: 1

Abhi
Abhi

Reputation: 1115

try

ORDER BY keyword Asc

To get the desired result

Upvotes: 2

senK
senK

Reputation: 2802

Other than Ascending if your concern about the length means try LENGTH()

$construct = "SELECT * FROM word WHERE $construct order by LENGTH(`keywords`)

Upvotes: 1

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

use ORDER BY keywords ASC it will give you appropriate result as expected.

Upvotes: 1

freaky
freaky

Reputation: 294

use order by asc

$construct = "SELECT * FROM word WHERE $construct ORDER BY [Field Name from which you want ascend your search ] ASC";

Upvotes: 2

Related Questions