By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,847 Members | 2,402 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,847 IT Pros & Developers. It's quick & easy.

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

P: 62
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.....some 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
Sep 8 '07 #1
Share this Question
Share on Google+
12 Replies


pbmods
Expert 5K+
P: 5,821
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.
Sep 9 '07 #2

kovik
Expert 100+
P: 1,044
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.
Sep 9 '07 #3

P: 62
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
Sep 9 '07 #4

pbmods
Expert 5K+
P: 5,821
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?
Sep 9 '07 #5

P: 62
I use php 5.2.2 ,But i dont know hwat is that version..
Sep 9 '07 #6

P: 62
Yes you are right....that is my really problem.....
Sep 9 '07 #7

pbmods
Expert 5K+
P: 5,821
Heya, Paitoon.

You might want to look into working with fulltext searching.
Sep 9 '07 #8

P: 62
thank you so much...i willl try...:-)
Sep 9 '07 #9

Staria
P: 7
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
Sep 9 '07 #10

P: 62
The c is here.....
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $q=$_GET[q];
  3. $page=$_GET[page];
  4. if ($page=="") {
  5.         $page=1;
  6. }
  7. $each=20; // specify number of image per one page//
  8. ?>
  9.  
  10.           &nbsp;&nbsp;<table border="0" cellpadding="0" cellspacing="0">
  11.   <td ><form method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>" >
  12.     <table bgcolor="#F8F8F8" class="ExtraSearch">
  13.         <td>
  14.                 <input name="q" type="text" value="<?php echo $q; ?>" size="25" />
  15.                 <input name="submit" type="submit" id="BlueFormbtn" value="Search" />
  16.         </table>
  17.   </form></td>
  18.     </table>
  19.             <?php 
  20. //This is a working script
  21. //Make sure to go through it and edit database table filelds that you are seraching
  22. //This script assumes you are searching 3 fields
  23.  
  24. $hostname_logon = "" ;   
  25. $database_logon = "" ;  
  26. $username_logon = "" ;  
  27. $password_logon = "" ;   
  28. //open database connection
  29.  $connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unabale to connect to the database" );
  30.  //select database
  31.  mysql_select_db($database_logon) or die ( "Unable to select database!" );
  32.  
  33.  
  34. // Get the search variable from URL
  35.   $var = @$_GET['q'] ;
  36. //trim whitespace from the stored variable
  37.   $trimmed = trim($var);  
  38. //separate key-phrases into keywords
  39.   $trimmed_array = explode(" ",$trimmed); 
  40.  
  41. // check for an empty string and display a message.
  42. if ($trimmed == "") {
  43.   $resultmsg =  "<center><p>Search Error</p><p>Please enter a search...</p></center><br><br>" ;
  44.   }
  45.  
  46. // Build SQL Query for each keyword entered 
  47. foreach ($trimmed_array as $trimm){
  48.  
  49. // EDIT HERE and specify your table and field names for the SQL query
  50.      $query = "SELECT * FROM users WHERE username LIKE \"%$trimm%\"  ORDER BY username   DESC" ; 
  51.      // Execute the query to  get number of rows that contain search kewords
  52.      $numresults=mysql_query ($query);
  53.      $row_num_links_main =mysql_num_rows ($numresults);
  54.  
  55.  
  56.        $adid_array[] = $row[ 'fieldid' ];
  57.       }while( $row= mysql_fetch_array($numresults));
  58. //end foreach
  59.  
  60. if($row_num_links_main == 0 && $row_set_num == 0){
  61.    $resultmsg = "<center><p>Search results for:" . $trimmed  ."</p><p>Sorry, your search returned zero results</p></center><BR><br>" ;
  62. }
  63.    //delete duplicate record id's from the array. To do this we will use array_unique function
  64.    $tmparr = array_unique($adid_array); 
  65.    $i=0; 
  66.    foreach ($tmparr as $v) { 
  67.        $newarr[$i] = $v; 
  68.        $i++; 
  69.    } 
  70.  
  71. // now you can display the results returned. But first we will display the search form on the top of the page
  72. ?>
  73.             <?php
  74. include "provincelist.php";
  75. // display what the person searched for.
  76.  if( isset ($resultmsg)){
  77.   echo $resultmsg;
  78.   exit();
  79.  }else{
  80.  
  81. foreach($newarr as $value){
  82.  
  83. // EDIT HERE and specify your table and field names for the SQL query
  84. $query_value = "SELECT * FROM users WHERE username = '$value'";
  85.  $num_value=mysql_query ($query_value);
  86.  $row_linkcat= mysql_fetch_array ($num_value);
  87.  $row_num_links= mysql_num_rows ($num_value);
  88.  
  89. //now let's make the keywods bold. To do that we will use preg_replace function. 
  90. //EDIT parts of the lines below that have fields names like $row_linkcat[ 'field1' ]
  91. //This script assumes you are searching only 3 fields. If you are searching more fileds make sure that add appropriate line. 
  92.   $titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'username' ] );
  93. foreach($trimmed_array as $trimm){
  94.  
  95. //end highlight
  96.     $connections="select * from users where username like'%$trimm%'";
  97.     $result=mysql_db_query($database_logon,$connections);
  98.     $total=mysql_num_rows($result);
  99.     $totalpages=ceil($total/$each);
  100.     $goto = ($page-1)*$each;
  101.  
  102. $connections="select * from users where username like'%$trimm%'  order by username  limit $goto,$each";
  103. $result=mysql_db_query($database_logon,$connections);
  104. $num=mysql_num_rows($result);
  105.  
  106.             while($rs=mysql_fetch_array($result)) {
  107. $username=$rs[username];
  108. $password=$rs[password];
  109. $userid=$rs[userid];
  110. $userlevel=$rs[userlevel];
  111. $email=$rs[email];
  112. $timestamp=$rs[timestamp];
  113. $photo=$rs[photo];
  114. $sex=$rs[sex];
  115. $province=$rs[province];
  116. $aboutme=$rs[aboutme];
  117. $vote=$rs[vote];
  118. $lookfor=$rs[lookfor];
  119. $figure=$rs[figure];
  120.  
  121.        echo "<table width=\"580\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\" align=\"center\" id='search_page'><tr>";
  122.          if ($photo=="") { 
  123.       echo "<td ><IMG SRC='site_img/nophoto.gif' width='95' height='95' border='1'/></td>";
  124.      }else {
  125.       echo"<td><a href='#' onclick=\"displayStaticMessage('<img src=prof_photo/$photo width=400 height=400/>  <a href=\'#\' onclick=\'closeMessage();return false\'><img src=c.gif /></a>','modalDialog_contentDiv_error');return false\">
  126. <IMG SRC=\"prof_photo/$photo\" width=\"95\" height=\"95\" BORDER=\"1\" ></A></td>";}
  127.       echo"<td><div id='searchDivUserName'>username : <a href=\"seeprof.php?id=$username\"> $username </a><br>";
  128.                 if ($sex=="M") {
  129.                     echo "เพศ : ชาย<br>";
  130.                  } else if ($sex=="W") {
  131.                     echo "เพศ : หญิง<br>";
  132.                  }  else if($sex=="G") {
  133.                     echo "เพศ : เกย์<br>";
  134.                  }  else if ($sex=="T") {
  135.                     echo "เพศ : ทอม<br>";
  136.                  }  else if($sex=="D") {
  137.                     echo "เพศ : ดี้<br>";
  138.                  }    else if($sex=="B") {
  139.                     echo "เพศ : Bisexual<br>";
  140.                  }
  141. echo"จังหวัด : $p[$province]
  142.       </div></td>";
  143.       echo" <td ><div id='searchDivLink'>
  144.       <a href ='seeprof.php?id=$username'>ดูโปรไฟล์</a>
  145.       <a href ='sendmail.php?id=$username'>ส่งอีเมลย์</a>
  146.  
  147.  
  148.       </div></td>
  149.   </tr>
  150. </table><tr>";
  151.  
  152.  
  153. }
  154.  
  155.  }
  156.  
  157. }
  158. }
  159.  
  160. ?><br />
  161.           <br />
  162.           </div>
  163.           <div id="imgpage">            
  164. <?
  165.  
  166.     if ($totalpages>1) { 
  167.         echo "<B>หน้า $page</B><BR>";
  168.          for ($i=1;$i<=$totalpages;$i++) {
  169.         echo "| <A HREF='search_name.php?q=$q&submit=Search&page=$i'>$i</A> ";
  170.         }
  171.     }
  172. ?>
  173. </div>
  174.           <br />
  175.           <br />
  176.         </div>
  177.       </div>
  178.       </div>
  179.     </div>
  180.     <br />
  181.     <div id="footer">
  182. </div>
  183.   </div>
  184. </div>
  185. </body>
  186. </html>
Sep 11 '07 #11

pbmods
Expert 5K+
P: 5,821
Heya, Alex.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]
Sep 11 '07 #12

P: 62
Oh Im sorry sir. I will remember now.
Sep 11 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.