Reputation: 2647
I am confused how to create loop inside MySQL query.
I have this query:
<?php
$arr = explode(',', $features);
$query_spicy = "
SELECT wposts.*
FROM $wpdb->posts wposts
INNER
JOIN ( SELECT post_id
FROM $wpdb->postmeta wpostmeta
WHERE (';
( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test1%' )
OR
( wpostmeta.meta_key = 'features'AND wpostmeta.meta_value LIKE '%test2%' )
OR
( wpostmeta.meta_key = 'features'AND wpostmeta.meta_value LIKE '%test3%' )
OR
( wpostmeta.meta_key = 'features'AND wpostmeta.meta_value LIKE '%test4%' )
OR
( wpostmeta.meta_key = 'features'AND wpostmeta.meta_value LIKE '%test5%' )
')
GROUP BY post_id
) AS t ON t.post_id = wposts.ID WHERE wposts.post_status = 'publish' AND wposts.post_type = 'book' ORDER BY wposts.post_date DESC";
?>
And I want generate bellow part dynamic with for loop:
( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test1%' ) OR
( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test2%' ) OR
( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test3%' ) OR
( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test4%' ) OR
( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test5%' )
Note: test1, test2, test3, test4 and test5, I am getting from this $arr
.
I hope you understand my question and I will thankful if you help me.
Thanks :)
Upvotes: 1
Views: 1193
Reputation: 8020
<?php
$arr = explode( ',', $features );
$query_spicy = "
SELECT wposts.*
FROM {$wpdb->posts wposts}
INNER
JOIN ( SELECT post_id
FROM {$wpdb->postmeta wpostmeta}
WHERE (";
$tmp = array();
foreach( $arr as $value )
{
$tmp[] = "( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%{$value}%' )";
}
$query_spicy .= implode( ' OR ', $tmp );
$query_spicy .= ")
GROUP BY post_id
) AS t ON t.post_id = wposts.ID WHERE wposts.post_status = 'publish' AND wposts.post_type = 'book' ORDER BY wposts.post_date DESC";
?>
Upvotes: 0
Reputation: 1002
FWIIW:
Just set an array to contain the data you need to insert then use foreach to loop through it.
$sql="SELECT wposts.*
FROM $wpdb->posts wposts
INNER
JOIN ( SELECT post_id
FROM $wpdb->postmeta wpostmeta
WHERE ('; ";
foreach($arrayoftestdata as $test)
{
$sql.="( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%$test%' ) OR ";
}
$sql.="')
GROUP BY post_id
) AS t ON t.post_id = wposts.ID WHERE wposts.post_status = 'publish' AND wposts.post_type = 'book' ORDER BY wposts.post_date DESC";
Upvotes: -1
Reputation: 2891
Try this
wpostmeta.meta_key = ".$features." AND wpostmeta.meta_value LIKE '%test1%'
Upvotes: 0
Reputation: 82
try something like
$arr = explode(',', $features);
$conditions = array();
foreach($arr as $feature){
$condition[] = "( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '{$feature}' )";
}
$query = "SELECT ...". join("OR",$conditions);
But you can simply do something like wpostmeta.meta_key = 'feature' and wpostmet IN($features)
I think second solution is better and faster.
Upvotes: 2