2015-11-12 21 views
-1

我想要查詢格式正確。如何正確使用number_format?

我需要所有結果在前面都有'$',任何小於300的結果都會在末尾加上'/mo.*'。它沒有做我想做的事。有任何建議嗎?提前致謝!

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(); 
} 
+1

什麼,祈禱告訴你,你會喜歡它做的。或者我們都會猜測? –

+0

預期格式是什麼? –

+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; 
}