Php 如何正确使用数字格式?

Php 如何正确使用数字格式?,php,Php,我正在尝试使查询的格式正确 我需要所有结果的前面都有“$”,如果少于300,则在末尾添加“/mo.*”。这不是我想要的。有什么建议吗?提前谢谢 public function get_under300_specials() { $this->db->select(' vehicle_specials.type, vehicle_specials.stock_number, inventory.year, inventory.make, inventory.model, inv

我正在尝试使查询的格式正确

我需要所有结果的前面都有“
$
”,如果少于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();
}
300_特价()下的公共函数get_{
$this->db->select('vehicle\u specials.type,vehicle\u specials.stock\u number,inventory.year,inventory.make,inventory.model,inventory.trim,vehicle\u specials.price,vehicle\u specials.msrp,vehicle\u specials.was,vehicle\u specials.description,inventory.photos')->来自('vehicle\u specials')->加入('inventory'、'inventory.stock_number=车辆_specials.stock_number');
$this->db->where(“vehicle_specials.price<300”);
$this->db->where('status','active');
//返回$this->db->get();
$results=$this;
foreach($results as$key=>$result){//格式编号
$results[$key]['price']='$'.number_格式($result['price'])。($result['price']<300)?'/mo.*':'';
}
返回$results->db->get();
}

在不知道您使用的是什么框架的情况下,我假设问题在于您在尝试格式化数据库之前没有从数据库中获取数据

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;
}
300_特价()下的公共函数get_{
$this->db->select('vehicle\u specials.type,vehicle\u specials.stock\u number,inventory.year,inventory.make,inventory.model,inventory.trim,vehicle\u specials.price,vehicle\u specials.msrp,vehicle\u specials.was,vehicle\u specials.description,inventory.photos')->来自('vehicle\u specials')->加入('inventory'、'inventory.stock_number=车辆_specials.stock_number');
$this->db->where(“vehicle_specials.price<300”);
$this->db->where('status','active');
$results=$this->db->get();//这可能需要一个表才能从中获取数据
//假设get()-方法返回数组,而不是对象
//此外,如果对象中已经存在“price”字段,则最好不要在不必要时覆盖它
foreach($results as$key=>&$result){//格式编号
$result['price_str']='$'.number_格式($result['price']);
if(intval($result['price'])<300)
$result['price_str'].=“/mo.*”;
}
返回$results;
}

请告诉我,您希望它做什么。或者我们都猜猜看吗?预期的格式是什么?这里似乎很好。您检查了哪些值?我尝试了几个。所有结果都是数字的,没有$Or/mo.*。我想返回$results->db->get();可能是问题的原因。get()方法是什么?