Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('content-Type: text/plain');
- function create_sql_taxonomy($table_prefix, $link_table, $taxonomy_table, $terms_table, $taxonomies) {
- $t_count = 1;
- $sql = [];
- $sql_str = '';
- $terms_ = [];
- $terms = [];
- for ($i = 0; $i < count($taxonomies); $i++) {
- for ($j = 0; $j < count($taxonomies[$i]); $j++) {
- $t = $taxonomies[$i][$j];
- switch ($j) {
- case 0:
- $logic = $t;
- break;
- case 1:
- $taxonomy = $t;
- break;
- case 2:
- if (is_string($t) && strlen($t) > 0) {
- $t = explode(',', $t);
- } else {
- $t = [];
- }
- if (is_array($t) && count($t)) {
- $terms = $t;
- } else {
- $terms = [];
- }
- break;
- }
- }
- if (count($terms)) {
- if ($logic == 'AND') {
- for ($x = 0; $x < count($terms); $x++) {
- $term = $terms[$x];
- if (strlen($term)) {
- $sql_str = "JOIN `$table_prefix$link_table` AS `tr$t_count` ON `tr$t_count`.`object_id` = `p`.`id` ";
- $sql_str .= " JOIN `$table_prefix$taxonomy_table` AS `tt$t_count` ON `tt$t_count`.`term_id` = `tr$t_count`.`term_taxonomy_id` ";
- $sql_str .= " JOIN `$table_prefix$terms_table` AS `t$t_count` ON `t$t_count`.`id` = `tt$t_count`.`term_id` ";
- $sql_str .= " AND (`tt$t_count`.`taxonomy` = '$taxonomy' AND `t$t_count`.`name` = '$term') ";
- $sql[] = $sql_str;
- $t_count++;
- }
- }
- }
- if ($logic == 'OR') {
- for ($x = 0; $x < count($terms); $x++) {
- $term = $terms[$x];
- if (strlen($term))
- $terms_[$x] = "'{$terms[$x]}'";
- }
- $sql_str = " JOIN `$table_prefix$link_table` AS `tr$t_count` ON `tr$t_count`.`object_id` = `p`.`id` ";
- $sql_str .= "JOIN `$table_prefix$taxonomy_table` AS `tt$t_count` ON `tt$t_count`.`term_id` = `tr$t_count`.`term_taxonomy_id` ";
- $sql_str .= "JOIN `$table_prefix$terms_table` AS `t$t_count` ON `t$t_count`.`id` = `tt$t_count`.`term_id` ";
- $sql_str .= "AND (`tt$t_count`.`taxonomy` = '$taxonomy' AND `t$t_count`.`name` IN (" . implode(',', $terms_) . ")) ";
- $sql[] = $sql_str;
- $t_count++;
- }
- }
- }
- return implode(" ", $sql);
- }
- function create_sql_search($table, $between_cells_logic, $cells_data, $words) {
- $match_expr = [];
- $cells_expr = [];
- $words = strlen($words) ? explode(' ', $words) : [];
- if (count($words)) {
- for ($i = 0; $i < count($cells_data); $i++) {
- $cell_logic = $cells_data[$i][0];
- $cells = explode(',', $cells_data[$i][1]);
- for ($j = 0; $j < count($cells); $j++) {
- if (count($words)) {
- for ($x = 0; $x < count($words); $x++) {
- $match_expr[$i][] = "(MATCH `$table`.`{$cells[$j]}` AGAINST ('\"{$words[$x]}\"'))";
- }
- }
- $cells_expr[] = '(' . implode(" $cell_logic ", $match_expr[$i]) . ')';
- }
- }
- return '(' . implode(" $between_cells_logic ", $cells_expr) . ')';
- } else {
- return '';
- }
- }
- function create_sql_query($q, $words_in, $words_logic, $category, $tags, $tags_logic, $limit) {
- $taxonomy = create_sql_taxonomy('ex_', 'term_relationships', 'term_taxonomy', 'terms',
- array(
- [$tags_logic, 'post_tag', $tags],
- ['AND', 'category', $category]
- )
- );
- $where = [];
- $where = create_sql_search('p', 'OR', array(['AND', 'post_title'], ['AND', 'post_content']), $q);
- if (strlen($where)) $where = "WHERE $where";
- $query = "SELECT SQL_CALC_FOUND_ROWS `p`.`id` FROM `ex_posts` AS `p` $taxonomy$where GROUP BY `p`.`id` LIMIT $limit";
- return $query;
- }
- echo create_sql_query('Ключевые слова для поиска', 'all', 'AND', 'programming', 'php,mysql,delphi', 'AND', '0, 10');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment