2016-04-23 102 views
0

我有这样的代码,在WordPress的编码:比较数据库的价格和选择价格最低

<?php 
     global $product; 
     global $wpdb; 
     $locations = get_the_terms($product->ID, 'pa_location'); 
     foreach ($locations as $locationz) { 
     $location = $locationz->name; 
     } 
     $sql = "SELECT * FROM rates WHERE location = '" . $location . "';"; 
     $query = $wpdb->get_results($sql); 
     foreach ($query as $price) { 
      $regularprice = $price->fareprice; 
     } 
?> 

现在,我的问题是选择使用,其中有4个相同位置的位置列中的最低价格。

我知道我可以使用min();但是如何将$regularprices存储在数组上并使用min();来选择最低价格?

+0

没关系,我得到了它已经 “' – Romee648

回答

2

如果你想存储$regularprices阵列上,你可以使用$regularprices[]然后用min()功能,像这样:

foreach ($query as $price) 
{ 
    $regularprice[] = $price->fareprice; 
} 
$lowestPrice = min($regularprice); 

我也注意到,您的变量$location只有1值之后foreach功能,如果你想通过产品id从所有地点得到价格,你应该改变你的代码:

foreach ($locations as $locationz) 
{ 
    $location[] = $locationz->name; 
} 
$sql = "SELECT * FROM rates WHERE location IN '" . $location . "';"; 

祝你好运!

+0

谢谢,但我已经解决了它,你发布前一个小时,但无论如何,这是正确的答案:) – Romee648