Hello i am building a REST API using Codeigniter. The thing is that i want to get the prices for a specific property. I get the prices correctly but i want to get the prices for the current year only. The table has prices from 2003-2017 so i only want to display prices greater or equal than the current year.
So i have something like this:
{
"status": "success",
"status_code": "200",
"message": "200 OK",
"response": [
{
"property_id": "3",
"price_id": "66",
"price": "350",
"currency": "EUR",
"timeframe_id": "1"
},
{
"property_id": "3",
"price_id": "70",
"price": "300",
"currency": "EUR",
"timeframe_id": "6"
}
and at the very bottom:
{
"property_id": "3",
"price_id": "167547",
"price": "500",
"currency": "EUR",
"timeframe_id": "1186",
"periods": [
{
"from": "2015-12-12",
"to": "2015-12-19",
"day": "Sa"
}
]
},
{
"property_id": "3",
"price_id": "167548",
"price": "550",
"currency": "EUR",
"timeframe_id": "1187",
"periods": [
{
"from": "2015-12-19",
"to": "2015-12-26",
"day": "Sa"
}
]
}
]
}
What i want to do is only display the prices that they have periods. So i used unset but the results come in a weird way like this:
{
"status": "success",
"status_code": "200",
"message": "200 OK",
"response": {
"582": {
"property_id": "3",
"price_id": "167498",
"price": "300",
"currency": "EUR",
"timeframe_id": "1137",
"periods": [
{
"from": "2015-01-03",
"to": "2015-01-10",
"day": "Sa"
}
]
},
"583": {
"property_id": "3",
"price_id": "167499",
"price": "300",
"currency": "EUR",
"timeframe_id": "1138",
"periods": [
{
"from": "2015-01-10",
"to": "2015-01-17",
"day": "Sa"
}
]
}
How can i remove this 582:{ in front of every object? My code is:
$prices = $this->Model_prices->get_many_by(array('Objekt' => $property_id));
foreach ($prices as $key => $value) {
$data = $this->timeframe_get($value['timeframe_id']);
foreach ($data as $k => $v) {
$from = $v['from'];
if ( date("Y", strtotime(".$from.")) >= "2015" ) {
$prices[$key]['periods'] = $data;
}else{
unset($prices[$key]);
}
}
}
$this->response(array('status' => 'success', 'status_code' => '200', 'message' => '200 OK', 'response' => $prices));
The timeframe_get method:
public function timeframe_get($timeframe_id){
$this->load->model('Model_timeframe');
$this->load->database();
// $sql = "SELECT ID as id, von as _from, bis as _to FROM zeitraeumevk WHERE ID = $timeframe_id AND YEAR(von) >= YEAR('2015-01-01')";
// $query = $this->db->query($sql);
$timeframes = $this->Model_timeframe->get_many_by(array('ID' => $timeframe_id));
if ($timeframes) {
return $timeframes;
} else {
return "There is no timeframe specified for this property";
}
}
Any ideas? Thank you in advance!!!