我正在尝试查询格式正确。
我需要所有结果在前面加上“$
”,并且在结尾处添加“/mo.*
”时要小于300。它没有做我想做的事。有什么建议吗?提前谢谢!
public function get_under300_specials() {
$this->db->select(' vehicle_specials.type, vehicle_specials.stock_number, inventory.year, inventory.make, inventory.model, inventory.trim, vehicle_specials.price, vehicle_specials.msrp, vehicle_specials.was, vehicle_specials.description, inventory.photos')->from('vehicle_specials')->join('inventory', 'inventory.stock_number = vehicle_specials.stock_number');
$this->db->where("vehicle_specials.price < 300");
$this->db->where('status','active');
//return $this->db->get();
$results = $this;
foreach ($results as $key => $result) { // Format number
$results[$key]['price'] = '$' . number_format($result['price']) . (($result['price'] < 300) ? '/mo.*' : '');
}
return $results->db->get();
}
答案 0 :(得分:0)
在不知道您正在使用什么框架的情况下,我认为问题是您在尝试格式化数据之前没有从数据库获取数据。
public function get_under300_specials() {
$this->db->select(' vehicle_specials.type, vehicle_specials.stock_number, inventory.year, inventory.make, inventory.model, inventory.trim, vehicle_specials.price, vehicle_specials.msrp, vehicle_specials.was, vehicle_specials.description, inventory.photos')->from('vehicle_specials')->join('inventory', 'inventory.stock_number = vehicle_specials.stock_number');
$this->db->where("vehicle_specials.price < 300");
$this->db->where('status','active');
$results = $this->db->get(); // this might require a table to get data from
// assuming get()-method returns array, not object
// futher more, if there is 'price' field already in object, it would be good practice to not to overwrite it if not necessary. Thus, using price_str
foreach ($results as $key => &$result) { // Format number
$result['price_str'] = '$' . number_format($result['price']);
if( intval($result['price']) < 300 )
$result['price_str'] .= "/mo.*";
}
return $results;
}