Yii2.0 PHP using Sphinx

Source: Internet
Author: User
Tags add time yii
This article introduces the content is about Yii2.0 PHP uses sphinx, has certain reference value, now shares to everybody, has the need friend can refer to



[PHP] view plain copy


  1. 1. First in Yii2.0 to introduce the Sphinxapi class file, the steps introduced:

  2. (1) In the root directory, create a new folder Libs, sphinxapi.php the class file into the Libs folder.

  3. (2.) Then in the root directory of the Config folder web.php configuration, add Yii:: $classMap [ ' sphinxclient ' Span class= "string" style= "Margin:0px;padding:0px;border:none;color:rgb (0,0,255); background-color:inherit;" > ' @app/libs/sphinxapi.php '

  4. (3) using the use sphinxclient in the controller ;

  5. (4.) I used two tables, blog Ex_blog, and blog post classification table Ex_blog_type




[PHP] view plain copy


  1. /* * Sphinx Search */Public Function Actionsearch () {//Determine if it is a post commit if (\yii:: $app->request->ispost) {          $title = \yii:: $app->request->post (' title ');          $author = \yii:: $app->request->post (' author ');          $type _name = \yii:: $app->request->post (' type_name ');          $sphinx = new Sphinxclient ();                    $sphinx->setserver (' 127.0.0.1 ', 9312);          Sph_match_all matches all query terms (default mode) $sphinx->setmatchmode (Sph_match_all);          Match any one of the query terms $sphinx->setmatchmode (sph_match_any);                    Consider the whole query as a phrase, requiring a complete match in order $sphinx->setmatchmode (sph_match_phrase);          $data = $sphinx->query ("$title", "*");          Determine if the key value exists if (!array_key_exists ("matches", $data)) {exit ("The information you need is not retrieved");          }//1.Matches is the result of the query, but it seems to be not the data we want.          2. According to the official note, Sphinx does not connect to MySQL to fetch data, but calculates it based on its own index content; 3. Therefore, if you want to use the API provided by Sphinx to obtain the data we want, it must also be based on the results of the query (alsoID is the basis);          4. Query MySQL again based on the ID to get the data we want.          Gets the primary key (where the primary key is the self-increment ID of the data in the database) by ID in accordance with the database query data $key = Array_keys ($data [' matches ']);          Combine array elements into a string $key = Join (', ', $key);          Based on the obtained ID in the query database $query = new \yii\db\query (); Use the in hashed format of the frame to retrieve the $blogInfo = $query->from ([' b ' = ' = ' ex_blog ', ' bt ' = ' ex_blog_type '])->where ([' id ' =] [$key]])          ->all ();      Converts an array to JSON type echo json_encode ($blogInfo);      }else{echo "Non-normal submission"; }  }

Processing of HTML pages




[PHP] view plain copy


  1. <script> function Search () {//get the data searched for var title = $ ("#title"). Val ();              var author = $ ("#author"). Val ();              var type_name = $ ("#type_name"). Val (); Send Ajax request $.ajax ({URL: "? R=sphinx/search", Data:{title:title,author:author , type_name:type_name}, type: "POST", Success:function (msg) {//alert (                      msg);                      Convert JSON data to object obj = eval ("(" +msg+ ")");                      var table = '; Table + = ' <table border= "1" align= "center" width= "+" style= "text-align:center" > ' table +                          = ' <tr id= ' tr ' > ';                          Table + = ' <td> number </td> ';                          Table + = ' <td> title </td> ';                          Table + = ' <td> author </td> '; Table + = ' <td> type </td> ';                          Table + = ' <td> cover </td> ';                          Table + = ' <td> add time </td> ';                          Table + = ' <td> operation </td> ';                           Table + = ' </tr> ';  for (var i in obj) {table + = "<tr class= ' content ' >" table                               + = "<td>" +obj[i].id+ "</td>";                               Table + = "<td>" +obj[i].title+ "</td>";                               Table + = "<td>" +obj[i].author+ "</td>";                               Table + = "<td>" +obj[i].type_name+ "</td>";                               Table + = ' <td></td>";                               Table + = "<td>" +obj[i].time+ "</td>"; Table + = "<td> <a href=" > Delete </a> | <a href= "> Edit </a> </td>";                      Table + = "</tr>"} table + = ' </table> ';                      Assigns a table to page $ ("#table"). html (table);                      Remove//$ (". Content") from the original content. Remove ();                  Appends the returned data to the original location//$ ("#tr"). After (TR);          }              }); } </script>

Display the data with the P




[PHP] view plain copy


    1. <p id= "table" ></P> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.