我编写了以下代码来计算查询与一组属性之间的距离。但是,每个循环的产品属性返回相同,而实际上我知道它们不是。它可能与wpdb变量有关吗?
我已经尝试在循环顶部将变量重置为0,并且还尝试取消设置。当多个产品的距离值存在差异时,代码将正常工作。
请参阅以下代码:
$the_query = get_posts( 'cat=2&post_type=product&fields=ids' );
if($the_query){
foreach($the_query as $id) {
//reset variables at begining of foreach loop
$distance = 0;
$product_head_standard = 0;
$product_loft = 0;
$product_shape = 0;
$product_trajectory_head_design = 0;
//queries for product attributes
$product_head_standard = $wpdb->query("SELECT meta_value FROM wp_postmeta
WHERE meta_key = 'head_standard' AND post_id =".$id);
$product_shape = $wpdb->query("SELECT meta_value FROM wp_postmeta
WHERE meta_key = 'shape' AND post_id =".$id);
$product_trajectory_head_design = $wpdb->query("SELECT meta_value
FROM wp_postmeta WHERE meta_key = 'trajectory_head_design' AND post_id =".$id);
$product_loft = $wpdb->query("SELECT meta_value FROM wp_postmeta
WHERE meta_key = 'loft' AND post_id =".$id);
echo $product_head_standard."productheadstandard";
echo $product_loft."productloft";
echo $product_shape."shape";
echo $product_trajectory_head_design."producttracjectory";
//calculate total height
$height_variables = $product_trajectory_head_design + $product_loft;
//calculate difference between search query and product attributes
$distance = ABS($form_standard-$product_head_standard)
+ ABS($form_desired_shape-$product_shape) + ABS($form_desired_height-$height_variables);
//output
echo "<tr><p>id".$id." distance".$distance."</p></tr>";
答案 0 :(得分:1)
我认为必须使用get_var选择单个值,如此
$value = $wpdb->get_var("sql goes here");
我相信你得到的数字不是你想要的价值。也许是结果的数量。