0

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!!!

6
  • 1
    Tried to change in DB query to populate result according to year instead of if condition. Commented May 19, 2016 at 12:23
  • can you please explain with example? Commented May 19, 2016 at 13:40
  • can you post get_many_by function ? Commented May 19, 2016 at 13:43
  • i am using this https://github.com/jamierumbelow/codeigniter-base-model/blob/master/core/MY_Model.php Commented May 19, 2016 at 13:44
  • can you try to add a node in your array('ID' => $timeframe_id,'from >=' => '2015-01-01'). this will return the result according to year i think. Commented May 19, 2016 at 13:57

2 Answers 2

2

You can use the "Query Builder Class" to build the query and retrieve the data in the form that you want, something like:

$this->db->where(array('Objekt' => $property_id, 'from >='=>'2015-01-01'))->get('prices');
Sign up to request clarification or add additional context in comments.

2 Comments

it does not give me anything. Please check my updated question in which i have timeframe_get method.
it does not return any rows. And there are rows inside of the table 2015 and over
0

I have finally managed to solve it like this:

$sql = "SELECT z.ID as id, z.von as _from, z.bis as _to, p.ID as price_id, p.Objekt as property_id, p.Preis as price FROM timeframe` z INNER JOIN prices p ON z.ID = p.Zeitraum INNER JOIN properties o ON p.Objekt = o.ID WHERE p.Objekt = ".$property_id." AND YEAR(z.von) >= YEAR('2015-01-01');";

But it would have been nicer to actually can use the built in method that MY_Model uses.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.