Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function create_sql_taxonomy($taxonomies) {
- $t_count = 1;
- $sql = [];
- $sql_str = '';
- $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:
- $terms = $t;
- break;
- }
- }
- if ($logic == 'AND') {
- for ($x = 0; $x < count($terms); $x++) {
- $term = $terms[$x];
- $sql_str = "JOIN ex_term_relationships AS tr$t_count ON tr$t_count.object_id = p.id\r\n";
- $sql_str .= "JOIN ex_term_taxonomy AS tt$t_count ON tt$t_count.term_id = tr$t_count.term_taxonomy_id\r\n";
- $sql_str .= "JOIN ex_terms AS t$t_count ON t$t_count.id = tt$t_count.term_id\r\n";
- $sql_str .= "AND (tt$t_count.taxonomy = '$taxonomy' AND t$t_count.name = '$term')\r\n";
- $sql[] = $sql_str;
- $t_count++;
- }
- }
- if ($logic == 'OR') {
- for ($x = 0; $x < count($terms); $x++) {
- $terms_[$x] = "'{$terms[$x]}'";
- }
- $sql_str = "JOIN ex_term_relationships AS tr$t_count ON tr$t_count.object_id = p.id\r\n";
- $sql_str .= "JOIN ex_term_taxonomy AS tt$t_count ON tt$t_count.term_id = tr$t_count.term_taxonomy_id\r\n";
- $sql_str .= "JOIN ex_terms AS t$t_count ON t$t_count.id = tt$t_count.term_id\r\n";
- $sql_str .= "AND (tt$t_count.taxonomy = '$taxonomy' AND t$t_count.name IN (" . implode(',', $terms_) . "))\r\n";
- $sql[] = $sql_str;
- $t_count++;
- }
- }
- return implode("\r\n", $sql);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment