How to order the alphabet in search result order by the keyword ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paitoon
    New Member
    • Aug 2007
    • 62

    How to order the alphabet in search result order by the keyword ?

    Hi,
    I got a little bit problem about search result in my site. When i put the keyword and click on search ....everything work fine i got the correct result but they not order by the keyword,but they order by the alphabet.....so me and the problem is happen sure if there are many result to display...the real result that people put keyword for will be in the last page...because the computer order by the alphabet.
    for example...i put the keyword IN
    The search result display .... Anin Chinmai Intel .....instead of order by what people search for..

    How can i set up this?

    Thank you so much sir.
    Paitoon
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Paitoon.

    What do you want your code to do? Give an example.
    What is your code doing that you don't want it to do? Give an example.
    What is your code *not* doing that it is supposed to? Give an example.

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      You are after relevance searching, which is no simple task. You must first decided exactly *what* counts as more relevant to the user than something else, then exactly *how* relevant it should be, and finally how to *calculate* that relevance.

      You should get your search results, send them through your relevance algorithm, sort the data, and then save / display it.

      Comment

      • paitoon
        New Member
        • Aug 2007
        • 62

        #4
        I still cunfuse with this......but i can give you the example:
        i want to find the people who have the name start with I so i put the key word i and click to search.....then the search result come with the word which have i in side....for example Adipong Adichart Bird Chinmai Diorgirl
        but they not order by what i search for.It order by a b c d e f g........

        Seem like the search knows that i want to find people who have name i in their name but it dont order it by i ..in the opposite way it order by a b c d e
        f......

        I see from google or facebook when i put key word and click search ...it will show the word which begin with the keyword i search for...


        but in my site the search will find the name which have the keyword that i search for but not order with that key word but it order by a b c d e f g.........

        so stress :-/
        Paitoon

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Paitoon.

          So what you're saying is that you want results that start with your keyword to show up before results that merely contain your keyword. Is that correct?

          What version of MySQL are you running?

          Comment

          • paitoon
            New Member
            • Aug 2007
            • 62

            #6
            I use php 5.2.2 ,But i dont know hwat is that version..

            Comment

            • paitoon
              New Member
              • Aug 2007
              • 62

              #7
              Yes you are right....that is my really problem.....

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, Paitoon.

                You might want to look into working with fulltext searching.

                Comment

                • paitoon
                  New Member
                  • Aug 2007
                  • 62

                  #9
                  thank you so much...i willl try...:-)

                  Comment

                  • Staria
                    New Member
                    • Sep 2007
                    • 7

                    #10
                    Hi...

                    Could you please post the code you are using? I recently rewrote the code for the search page on one of my websites, and I might be able to help you with yours if I were able to have a look at the code you are using.

                    Staria

                    Comment

                    • paitoon
                      New Member
                      • Aug 2007
                      • 62

                      #11
                      The c is here.....
                      [code=php]
                      <?
                      $q=$_GET[q];
                      $page=$_GET[page];
                      if ($page=="") {
                      $page=1;
                      }
                      $each=20; // specify number of image per one page//
                      ?>

                      &nbsp;&nbsp;<ta ble border="0" cellpadding="0" cellspacing="0" >
                      <td ><form method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>" >
                      <table bgcolor="#F8F8F 8" class="ExtraSea rch">
                      <td>
                      <input name="q" type="text" value="<?php echo $q; ?>" size="25" />
                      <input name="submit" type="submit" id="BlueFormbtn " value="Search" />
                      </table>
                      </form></td>
                      </table>
                      <?php
                      //This is a working script
                      //Make sure to go through it and edit database table filelds that you are seraching
                      //This script assumes you are searching 3 fields

                      $hostname_logon = "" ;
                      $database_logon = "" ;
                      $username_logon = "" ;
                      $password_logon = "" ;
                      //open database connection
                      $connections = mysql_connect($ hostname_logon, $username_logon , $password_logon ) or die ( "Unabale to connect to the database" );
                      //select database
                      mysql_select_db ($database_logo n) or die ( "Unable to select database!" );


                      // Get the search variable from URL
                      $var = @$_GET['q'] ;
                      //trim whitespace from the stored variable
                      $trimmed = trim($var);
                      //separate key-phrases into keywords
                      $trimmed_array = explode(" ",$trimmed) ;

                      // check for an empty string and display a message.
                      if ($trimmed == "") {
                      $resultmsg = "<center><p>Sea rch Error</p><p>Please enter a search...</p></center><br><br> " ;
                      }

                      // Build SQL Query for each keyword entered
                      foreach ($trimmed_array as $trimm){

                      // EDIT HERE and specify your table and field names for the SQL query
                      $query = "SELECT * FROM users WHERE username LIKE \"%$trimm%\" ORDER BY username DESC" ;
                      // Execute the query to get number of rows that contain search kewords
                      $numresults=mys ql_query ($query);
                      $row_num_links_ main =mysql_num_rows ($numresults);


                      $adid_array[] = $row[ 'fieldid' ];
                      }while( $row= mysql_fetch_arr ay($numresults) );
                      //end foreach

                      if($row_num_lin ks_main == 0 && $row_set_num == 0){
                      $resultmsg = "<center><p>Sea rch results for:" . $trimmed ."</p><p>Sorry, your search returned zero results</p></center><BR><br> " ;
                      }
                      //delete duplicate record id's from the array. To do this we will use array_unique function
                      $tmparr = array_unique($a did_array);
                      $i=0;
                      foreach ($tmparr as $v) {
                      $newarr[$i] = $v;
                      $i++;
                      }

                      // now you can display the results returned. But first we will display the search form on the top of the page
                      ?>
                      <?php
                      include "provincelist.p hp";
                      // display what the person searched for.
                      if( isset ($resultmsg)){
                      echo $resultmsg;
                      exit();
                      }else{

                      foreach($newarr as $value){

                      // EDIT HERE and specify your table and field names for the SQL query
                      $query_value = "SELECT * FROM users WHERE username = '$value'";
                      $num_value=mysq l_query ($query_value);
                      $row_linkcat= mysql_fetch_arr ay ($num_value);
                      $row_num_links= mysql_num_rows ($num_value);

                      //now let's make the keywods bold. To do that we will use preg_replace function.
                      //EDIT parts of the lines below that have fields names like $row_linkcat[ 'field1' ]
                      //This script assumes you are searching only 3 fields. If you are searching more fileds make sure that add appropriate line.
                      $titlehigh = preg_replace ( "'($var)'si " , "<b>\\1</b>" , $row_linkcat[ 'username' ] );
                      foreach($trimme d_array as $trimm){

                      //end highlight
                      $connections="s elect * from users where username like'%$trimm%'" ;
                      $result=mysql_d b_query($databa se_logon,$conne ctions);
                      $total=mysql_nu m_rows($result) ;
                      $totalpages=cei l($total/$each);
                      $goto = ($page-1)*$each;

                      $connections="s elect * from users where username like'%$trimm%' order by username limit $goto,$each";
                      $result=mysql_d b_query($databa se_logon,$conne ctions);
                      $num=mysql_num_ rows($result);

                      while($rs=mysql _fetch_array($r esult)) {
                      $username=$rs[username];
                      $password=$rs[password];
                      $userid=$rs[userid];
                      $userlevel=$rs[userlevel];
                      $email=$rs[email];
                      $timestamp=$rs[timestamp];
                      $photo=$rs[photo];
                      $sex=$rs[sex];
                      $province=$rs[province];
                      $aboutme=$rs[aboutme];
                      $vote=$rs[vote];
                      $lookfor=$rs[lookfor];
                      $figure=$rs[figure];

                      echo "<table width=\"580\" cellspacing=\"2 \" cellpadding=\"2 \" border=\"0\" align=\"center\ " id='search_page '><tr>";
                      if ($photo=="") {
                      echo "<td ><IMG SRC='site_img/nophoto.gif' width='95' height='95' border='1'/></td>";
                      }else {
                      echo"<td><a href='#' onclick=\"displ ayStaticMessage ('<img src=prof_photo/$photo width=400 height=400/> <a href=\'#\' onclick=\'close Message();retur n false\'><img src=c.gif /></a>','modalDialo g_contentDiv_er ror');return false\">
                      <IMG SRC=\"prof_phot o/$photo\" width=\"95\" height=\"95\" BORDER=\"1\" ></A></td>";}
                      echo"<td><div id='searchDivUs erName'>usernam e : <a href=\"seeprof. php?id=$usernam e\"> $username </a><br>";
                      if ($sex=="M") {
                      echo "เพศ : ชาย<br>";
                      } else if ($sex=="W") {
                      echo "เพศ : หญิง<br>";
                      } else if($sex=="G") {
                      echo "เพศ : เกย์<br>";
                      } else if ($sex=="T") {
                      echo "เพศ : ทอม<br>";
                      } else if($sex=="D") {
                      echo "เพศ : ดี้<br>";
                      } else if($sex=="B") {
                      echo "เพศ : Bisexual<br>";
                      }
                      echo"จังหวัด : $p[$province]
                      </div></td>";
                      echo" <td ><div id='searchDivLi nk'>
                      <a href ='seeprof.php?i d=$username'>ดู โปรไฟล์</a>
                      <a href ='sendmail.php? id=$username'>ส ่งอีเมลย์</a>


                      </div></td>
                      </tr>
                      </table><tr>";


                      }

                      }

                      }
                      }

                      ?><br />
                      <br />
                      </div>
                      <div id="imgpage">
                      <?

                      if ($totalpages>1) {
                      echo "<B>หน้า $page</B><BR>";
                      for ($i=1;$i<=$tota lpages;$i++) {
                      echo "| <A HREF='search_na me.php?q=$q&sub mit=Search&page =$i'>$i</A> ";
                      }
                      }
                      ?>
                      </div>
                      <br />
                      <br />
                      </div>
                      </div>
                      </div>
                      </div>
                      <br />
                      <div id="footer">
                      </div>
                      </div>
                      </div>
                      </body>
                      </html>[/code]
                      Last edited by pbmods; Sep 11 '07, 02:16 AM. Reason: Added CODE tags.

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        Heya, Alex.

                        Please use CODE tags when posting source code:

                        &#91;CODE=ph p]
                        PHP code goes here.
                        &#91;/CODE]

                        Comment

                        • paitoon
                          New Member
                          • Aug 2007
                          • 62

                          #13
                          Oh Im sorry sir. I will remember now.

                          Comment

                          Working...