Redfern_89

search_model.php (NEW!!!)

Jul 7th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.70 KB | None | 0 0
  1. <?php
  2.  
  3.     header('content-Type: text/plain');
  4.    
  5.     function create_sql_taxonomy($table_prefix, $link_table, $taxonomy_table, $terms_table, $taxonomies) {
  6.         $t_count = 1;
  7.         $sql = [];
  8.         $sql_str = '';
  9.         $terms_ = [];
  10.         $terms = [];
  11.        
  12.         for ($i = 0; $i < count($taxonomies); $i++) {
  13.             for ($j = 0; $j < count($taxonomies[$i]); $j++) {
  14.                
  15.                 $t = $taxonomies[$i][$j];
  16.                
  17.                 switch ($j) {
  18.                     case 0:
  19.                         $logic = $t;
  20.                         break;
  21.                     case 1:
  22.                         $taxonomy = $t;
  23.                         break;
  24.                     case 2:
  25.                         if (is_string($t) && strlen($t) > 0) {
  26.                             $t = explode(',', $t);
  27.                         } else {
  28.                             $t = [];
  29.                         }
  30.                         if (is_array($t) && count($t)) {
  31.                             $terms = $t;
  32.                         } else {
  33.                             $terms = [];
  34.                         }
  35.                         break;
  36.                 }
  37.             }
  38.  
  39.             if (count($terms)) {
  40.                 if ($logic == 'AND') {
  41.                     for ($x = 0; $x < count($terms); $x++) {
  42.                         $term = $terms[$x];
  43.                         if (strlen($term)) {
  44.                             $sql_str = "JOIN `$table_prefix$link_table` AS `tr$t_count` ON `tr$t_count`.`object_id` = `p`.`id` ";
  45.                             $sql_str .= " JOIN `$table_prefix$taxonomy_table` AS `tt$t_count` ON `tt$t_count`.`term_id` = `tr$t_count`.`term_taxonomy_id` ";
  46.                             $sql_str .= " JOIN `$table_prefix$terms_table` AS `t$t_count` ON `t$t_count`.`id` = `tt$t_count`.`term_id` ";
  47.                             $sql_str .= " AND (`tt$t_count`.`taxonomy` = '$taxonomy' AND `t$t_count`.`name` = '$term') ";
  48.                             $sql[] = $sql_str;
  49.                             $t_count++;
  50.                         }
  51.                     }
  52.                 }
  53.                 if ($logic == 'OR') {
  54.                     for ($x = 0; $x < count($terms); $x++) {
  55.                         $term = $terms[$x];
  56.                         if (strlen($term))
  57.                             $terms_[$x] = "'{$terms[$x]}'";
  58.                     }
  59.                     $sql_str = " JOIN `$table_prefix$link_table` AS `tr$t_count` ON `tr$t_count`.`object_id` = `p`.`id` ";
  60.                     $sql_str .= "JOIN `$table_prefix$taxonomy_table` AS `tt$t_count` ON `tt$t_count`.`term_id` = `tr$t_count`.`term_taxonomy_id` ";
  61.                     $sql_str .= "JOIN `$table_prefix$terms_table` AS `t$t_count` ON `t$t_count`.`id` = `tt$t_count`.`term_id` ";
  62.                     $sql_str .= "AND (`tt$t_count`.`taxonomy` = '$taxonomy' AND `t$t_count`.`name` IN (" . implode(',', $terms_) . ")) ";
  63.                     $sql[] = $sql_str;
  64.                     $t_count++;
  65.                 }
  66.             }
  67.            
  68.         }
  69.         return implode(" ", $sql);
  70.        
  71.     }
  72.    
  73.     function create_sql_search($table, $between_cells_logic, $cells_data, $words) {
  74.         $match_expr = [];
  75.         $cells_expr = [];
  76.         $words = strlen($words) ? explode(' ', $words) : [];
  77.        
  78.         if (count($words)) {
  79.             for ($i = 0; $i < count($cells_data); $i++) {
  80.                 $cell_logic = $cells_data[$i][0];
  81.                 $cells = explode(',', $cells_data[$i][1]);
  82.                
  83.                 for ($j = 0; $j < count($cells); $j++) {
  84.                    
  85.                     if (count($words)) {
  86.                         for ($x = 0; $x < count($words); $x++) {
  87.                             $match_expr[$i][] = "(MATCH `$table`.`{$cells[$j]}` AGAINST ('\"{$words[$x]}\"'))";
  88.                         }
  89.                     }
  90.                     $cells_expr[] = '(' . implode(" $cell_logic ", $match_expr[$i]) . ')';
  91.                
  92.                 }
  93.                
  94.             }
  95.             return '(' . implode(" $between_cells_logic ", $cells_expr) . ')';
  96.         } else {
  97.             return '';
  98.         }
  99.     }
  100.        
  101.     function create_sql_query($q, $words_in, $words_logic, $category, $tags, $tags_logic, $limit) {
  102.         $taxonomy = create_sql_taxonomy('ex_', 'term_relationships', 'term_taxonomy', 'terms',
  103.             array(
  104.                 [$tags_logic, 'post_tag', $tags],
  105.                 ['AND', 'category', $category]
  106.                 )
  107.             );
  108.         $where = [];
  109.        
  110.         $where = create_sql_search('p', 'OR', array(['AND', 'post_title'], ['AND', 'post_content']), $q);
  111.        
  112.         if (strlen($where)) $where = "WHERE $where";
  113.        
  114.         $query = "SELECT SQL_CALC_FOUND_ROWS `p`.`id` FROM `ex_posts` AS `p` $taxonomy$where GROUP BY `p`.`id` LIMIT $limit";
  115.        
  116.         return $query;
  117.     }
  118.  
  119.     echo create_sql_query('Ключевые слова для поиска', 'all', 'AND', 'programming', 'php,mysql,delphi', 'AND', '0, 10');
  120. ?>
Advertisement
Add Comment
Please, Sign In to add comment