PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/gamecrawler/wp/youxisoso/dataresult.php

http://collectgame.googlecode.com/
PHP | 317 lines | 248 code | 22 blank | 47 comment | 78 complexity | f028ae2285f0ef3fcd8f3f75cb1feaff MD5 | raw file
  1. <?php
  2. /**
  3. Template Name:data_result
  4. * @package WordPress
  5. * @subpackage Default_Theme
  6. * @author: shiwei
  7. */
  8. /**
  9. * ?????????1????
  10. * @param $str
  11. * @param $lenth
  12. * @param $start
  13. * @return unknown_type
  14. */
  15. function subString_UTF8($str, $lenth, $start=0){
  16. $len = strlen($str);
  17. $r = array();
  18. $n = 0;
  19. $m = 0;
  20. for($i = 0; $i < $len; $i++) {
  21. $x = substr($str, $i, 1);
  22. $a = base_convert(ord($x), 10, 2);
  23. $a = substr('00000000'.$a, -8);
  24. if ($n < $start){
  25. if (substr($a, 0, 1) == 0) {
  26. }elseif (substr($a, 0, 3) == 110) {
  27. $i += 1;
  28. }elseif (substr($a, 0, 4) == 1110) {
  29. $i += 2;
  30. }
  31. $n++;
  32. }else{
  33. if (substr($a, 0, 1) == 0) {
  34. $r[ ] = substr($str, $i, 1);
  35. }elseif (substr($a, 0, 3) == 110) {
  36. $r[ ] = substr($str, $i, 2);
  37. $i += 1;
  38. }elseif (substr($a, 0, 4) == 1110) {
  39. $r[ ] = substr($str, $i, 3);
  40. $i += 2;
  41. }else{
  42. $r[ ] = '';
  43. }
  44. if (++$m >= $lenth){
  45. break;
  46. }
  47. }
  48. }
  49. return join('',$r);
  50. }
  51. /**
  52. * ??????????
  53. * @param $str
  54. * @param $words
  55. * @return unknown_type
  56. */
  57. function highlight_word($str, $words){
  58. if(is_array($words)) {
  59. foreach($words as $k => $word){
  60. $pattern[$k] = '/(?!<[^>]*)'.
  61. '('.$word.')'.
  62. '(?![^<]*>)/ui';
  63. $replace[$k] = "<font style='color:red'>\\1</font>";
  64. }
  65. } else {
  66. $pattern = '/(?!<[^>]*)'.
  67. '('.$word.')'.
  68. '(?![^<]*>)/ui';
  69. $replace = "<font color='red'>\\1</font>";
  70. }
  71. return preg_replace($pattern,$replace,$str);
  72. }
  73. //????()?
  74. $pageSize = 10 ;
  75. $pageModel= 10 ; //???????
  76. $selfPageLink = "/" ; //?????
  77. $autoLink = "" ;
  78. $siteurl = get_bloginfo('template_url');
  79. //??????????????
  80. $cat = $_POST['cat'] ;
  81. if($cat == null || "" == $cat){
  82. $cat = $_GET['cat'] ;
  83. }
  84. //$categories = "" ;
  85. //$catStr = "" ;
  86. if($cat != null && "" != $cat){
  87. $category_id = get_cat_ID($cat);
  88. //$categories = get_categories ('child_of='.$category_id) ;
  89. //$categories = $wpdb->get_results("select p.name,p.term_id as pterm_id ,t.name,t.term_id as term_id ".
  90. // " from wp_terms t ,wp_term_taxonomy m , wp_terms p ".
  91. // " where t.term_id = m.term_id and p.term_id = m.parent and p.term_id ='".$category_id."'") ;
  92. //foreach ($categories as $category) {
  93. // $catStr =$catStr."'".$category->term_id."'," ;
  94. //}
  95. }
  96. $fixdiv = $_POST['fixdiv'] ;
  97. if($fixdiv == null || "" == $fixdiv){
  98. $fixdiv = $_GET['fixdiv'] ;
  99. }
  100. if($fixdiv == null ){
  101. $fixdiv = "" ;
  102. }
  103. //?????
  104. $keyword = $_POST['keywords'] ;
  105. if($keyword == null || "" == $keyword ){
  106. $keyword = $_GET["keywords"];
  107. }
  108. //$keyword = "??" ;
  109. //????????????
  110. $keyword = trim($keyword) ;
  111. $keyword = preg_replace("/\s+/" ," " ,$keyword) ;
  112. keywordStat($keyword) ;
  113. //????
  114. $paged = $_POST["paged"];
  115. if($paged == null || "" == $paged ){
  116. $paged = $_GET["paged"];
  117. if($paged == null || "" == $paged ){
  118. $paged = 0 ;
  119. }
  120. }
  121. $statNum = $pageSize * ($paged) ;
  122. //??sphinx
  123. require 'sphinxapi.php';
  124. //////////??sphinx?????///////////////
  125. $cl = new SphinxClient ();
  126. $cl->SetServer ( 'localhost', 9312);
  127. $cl->SetConnectTimeout ( 3 );
  128. //$catArray = array() ;
  129. //$catArray[0] = $cat ;
  130. if($category_id != null && "" != $category_id){
  131. $catArray = array() ;
  132. $catArray[0] = $category_id ;
  133. $cl->setFilter("post_category_id",$catArray);
  134. }
  135. $cl->SetArrayResult ( true );
  136. $cl->SetMatchMode ( SPH_MATCH_ANY);
  137. $cl->SetLimits ( $statNum, $pageSize ,1000);
  138. $cl->SetFieldWeights(array("post_title"=>1000, "post_content"=>1));
  139. //$cl->SetSelect("*") ;
  140. //$cl->SetSortMode ( SPH_SORT_ATTR_ASC, "gamepi" );
  141. $res = $cl->Query ( $keyword, "*" );
  142. $opts = array(
  143. 'before_match' => '<span style="color:red">',
  144. 'after_match' => '</span>',
  145. 'chunk_separator' => '...'//,
  146. //'limit'=>300
  147. );
  148. ////////??sphinx?????///////////////
  149. //????post?id?
  150. $unionSql = "" ;
  151. foreach($res['matches'] as $postidsArray){
  152. //$postids .= $postidsArray['id'].",";
  153. $unionSql = $unionSql." select ".$postidsArray['id']." as postid ,".$postidsArray['weight']." as weight from dual union all ";
  154. }
  155. //????
  156. $postQue = "" ;
  157. if(""==$unionSql){
  158. $postQue = "select ID,post_title, post_content,post_game_url,post_game_image,post_category_name from wp_posts where 1!=1 " ;
  159. }else {
  160. $unionSql = substr($unionSql,0,strlen($unionSql)-11) ;
  161. $postQue = "select post.ID,post.post_title, post.post_content,post.post_game_url,post.post_game_image,post.post_category_name from wp_posts post, ".
  162. "(".$unionSql.") temp where temp.postid = post.ID order by temp.weight desc " ;
  163. }
  164. $callback = $_GET['callback'];
  165. $myposts = $wpdb->get_results($postQue);
  166. $keyArray = explode(" ",$keyword) ;
  167. $html = "<div id='list_center'> ";
  168. $html .= "<span id='soso_result' class='list_centtopspan'>&nbsp;&nbsp;&nbsp;&nbsp;??".$res['total_found']."????? ????:'".$res['time']."' </span>" ;
  169. foreach($myposts as $mypost){
  170. $gamePic = $mypost->post_game_image;
  171. $gameLink = $mypost->post_game_url;
  172. $postLink = get_permalink($mypost->ID);
  173. $postTitle = $mypost->post_title ;
  174. $desContent = $mypost->post_content ;
  175. if($keyword !=null && ""!=$keyword){
  176. $postTitle = highlight_word($postTitle,$keyArray) ;
  177. $desContent = highlight_word(subString_UTF8($desContent,150),$keyArray) ;
  178. }
  179. $cat_name = $mypost->post_category_name ;
  180. $cat_title = "" ;
  181. if($cat_name == 'andriod' ||$cat_name == 'iphone' || $cat_name == 'windows phone' ){
  182. $cat_title = "????:";
  183. }else {
  184. $cat_title = "??:";
  185. }
  186. $html .=
  187. "<div class='list'>".
  188. " <span class='list_img'><a href='".$postLink."'>".
  189. " <img src='".$gamePic."' width='104' height='79' alt='".$mypost->post_title."' title='".$mypost->post_title."' />".
  190. " </a></span>".
  191. " <h3 class='list_title'><a href='".$postLink."'>".$postTitle."</a>".
  192. " </h3>".
  193. " <span class='list_cat'>".$cat_title."<samp>".$cat_name."</samp></span>".
  194. " <p class='list_neirong'>".$desContent."</p>".
  195. " <span class='list_add'>??????<samp>".$gameLink."</samp></span>".
  196. "</div>";
  197. }
  198. $html .= "<div id='scroll'>";
  199. $totalPage = ceil($res['total_found']/$pageSize) ;
  200. if($totalPage >0){
  201. $i = 0 ;
  202. if(intval($paged) > 0 ){
  203. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".(intval($paged)+1).",&#34".$fixdiv."&#34)' ><img src='".$siteurl."/images/next-top.gif' alt='???' title='???' class='f_l'/></a>" ;
  204. }
  205. if($totalPage<=$pageModel){
  206. for(;$i<$totalPage;$i++){
  207. if($i == intval($paged)){
  208. $html.="<font class='linknone'>".($i +1)."</font>";
  209. }else{
  210. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".$i.",&#34".$fixdiv."&#34)' class='linkstyle03'>".($i +1)."</a>";
  211. }
  212. }
  213. }elseif($totalPage < $pageModel + 3){
  214. $halfPageModel = intval($pageModel /3) ;
  215. if(intval($paged) < $halfPageModel*2){
  216. for($i=0;$i < $halfPageModel*2+1;$i++){
  217. if($i == intval($paged)){
  218. $html.="<font class='linknone'>".($i +1)."</font>";
  219. }else {
  220. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".$i.",&#34".$fixdiv."&#34)' class='linkstyle03'>".($i +1)."</a>";
  221. }
  222. }
  223. $html.="<a class='linkstyle03'>...</a>";
  224. for($i = $totalPage-($pageModel-$halfPageModel*2 -2);$i<$totalPage;$i++){
  225. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".$i.",&#34".$fixdiv."&#34)' class='linkstyle03'>".($i +1)."</a>";
  226. }
  227. }elseif(intval($paged) > $halfPageModel - 1) {
  228. for($i =0 ;$i< $halfPageModel-1;$i++){
  229. $html .= "<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".$i.",&#34".$fixdiv."&#34)' class='linkstyle03'>".($i +1)."</a>";
  230. }
  231. $html.="<a class='linkstyle03'>...</a>";
  232. for($i=$totalPage-($pageModel-$halfPageModel);$i < $totalPage;$i++){
  233. if($i == intval($paged)){
  234. $html.="<font class='linknone'>".($i +1)."</font>";
  235. }else{
  236. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".$i.",&#34".$fixdiv."&#34)' class='linkstyle03'>".($i +1)."</a>";
  237. }
  238. }
  239. }
  240. }else{
  241. $halfPageModel = intval($pageModel /2) ;
  242. if(intval($paged) < $halfPageModel-1){
  243. for($i =0 ;$i< $halfPageModel;$i++){
  244. if($i == intval($paged)){
  245. $html.="<font class='linknone'>".($i +1)."</font>";
  246. }else{
  247. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".$i.",&#34".$fixdiv."&#34)' class='linkstyle03'>".($i +1)."</a>";
  248. }
  249. }
  250. $html.="<a class='linkstyle03'>...</a>";
  251. for($i =$totalPage - ($pageModel - $halfPageModel - 1) ;$i< $totalPage ;$i++){
  252. $html .= "<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".$i.",&#34".$fixdiv."&#34)' class='linkstyle03'>".($i +1)."</a>";
  253. }
  254. }elseif(intval($paged) > $totalPage - ($halfPageModel) ) {
  255. for($i = 0 ;$i< $halfPageModel-1 ;$i++){
  256. $html .= "<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".$i.",&#34".$fixdiv."&#34)' class='linkstyle03'>".($i +1)."</a>";
  257. }
  258. $html.="<a class='linkstyle03'>...</a>";
  259. for($i = $totalPage - ($pageModel - $halfPageModel) ;$i< $totalPage;$i++){
  260. if($i == intval($paged)){
  261. $html.="<font class='linknone'>".($i +1)."</font>";
  262. }else{
  263. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".$i.",&#34".$fixdiv."&#34)' class='linkstyle03'>".($i +1)."</a>";
  264. }
  265. }
  266. }else {
  267. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,0,&#34".$fixdiv."&#34)' class='linkstyle03'>1</a>";
  268. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,1,&#34".$fixdiv."&#34)' class='linkstyle03'>2</a>";
  269. $uses = 3 ;
  270. if($paged > $totalPage/2){
  271. $uses = 2 ;
  272. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,2,&#34".$fixdiv."&#34)' class='linkstyle03'>3</a>";
  273. }
  274. $html.="<a class='linkstyle03'>...</a>";
  275. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".(intval($paged)-1).",&#34".$fixdiv."&#34)' class='linkstyle03'>".intval($paged)."</a>";
  276. $html.="<font class='linknone'>".(intval($paged) +1)."</font>";
  277. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".(intval($paged)+1).",&#34".$fixdiv."&#34)' class='linkstyle03'>".(intval($paged) +2)."</a>";
  278. $html.="<a class='linkstyle03'>...</a>";
  279. for($i = $totalPage - $uses ;$i< $totalPage;$i++){
  280. $html .= "<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".$i.",&#34".$fixdiv."&#34)' class='linkstyle03'>".($i +1)."</a>";
  281. }
  282. }
  283. }
  284. if(intval($paged)<$totalPage-1){
  285. $html.="<a href='javascript:getResult(&#34".$keyword."&#34,&#34".$cat."&#34,".(intval($paged)+1).",&#34".$fixdiv."&#34)' ><img src='".$siteurl."/images/next-bottom.gif' alt='???' title='???' class='f_l' /></a>";
  286. }
  287. }
  288. $html .= "</div>";
  289. $html .= "</div>";
  290. $jsonarray = array('html'=>$html,'fixdiv'=>$fixdiv) ;
  291. $ccc = json_encode($jsonarray);
  292. // $ccc = substr($ccc, 0 , $ccc.length -1) ;
  293. // $ccc = $ccc."]}" ;
  294. echo $callback.'('.$ccc.')' ;
  295. ?>