Frage

this is how I output my query results, but i'm wondering if it's the correct way.. As i'm doing some php logic in my view, i prefer to have it in my controller, isn't it?

Someone who can tell me if this is the way how it should be done, or how i can improve my code?

contoller:

public function reservations()
{
    $this->load->model('admin_model');
    $data = $this->admin_model->get_reservations();

    $this->load->view('view_reservations', $data);
}

Model:

function get_reservations()
{
    $this->db->select('*');
    $this->db->from('t_reservation');
    $data['query'] = $this->db->get();  

    return $data;
}

View:

<div class="row">
    <?php 
        foreach ($query->result() as $row):
          for($i=0; $i<$row->days; $i++)
          {
            $date = date("d-m-Y", strtotime(date("Y-m-d", strtotime($row->date)) . " +".$i." day"));
            $reservations[$i] = $date;
          } 
          echo $row->name;
          echo $row->price;
          echo $reservations[0]. '-'.$reservations[$row->days-1];
        endforeach; 
    ?>
</div><!-- /.row -->

So, is there a way to have that php logic out of my view, put is somewhere in my controller and have a cleaner view?

War es hilfreich?

Lösung

Your view should have as little PHP as possible.

Your controller should have $data declarations and your model should return data from the database.

Your for loop seems a bit unnecessary. You really just want a 'start date' and 'end date'. You could put this in the model, or in your controller, depending whether you want these returned every time you call the get_reservations.

Controller

public function reservations()
{
    $this->load->model('admin_model');
    $data['reservations'] = $this->admin_model->get_reservations();
    $this->load->view('view_reservations', $data);
}

Model

function get_reservations()
{
    $reservations = $this->db->get('t_reservation')->result();

    foreach ($reservations as $reservation)
    {
        $reservation->start_date = date("d-m-Y", strtotime($reservation->date));
        $reservation->end_date = date("d-m-Y", strtotime(date("Y-m-d", strtotime($reservation->date)) . " +".$reservation->days." day"));
    }

    return $reservations;
}

View I've edited your view as I'm guessing you want each result in a separate row?

<? foreach ($reservations as $reservations): ?>
    <div class="row">
        <?= $reservation->name ?>
        <?= $reservation->price ?>
        <?= $reservation->start_date ?> - <?= $reservation->end_date ?>
    </div><!-- /.row -->
<? endforeach ?>

Depending on how you want to display your data, you could have the foreach loop within the controller, and load multiple views:

Controller

public function reservations()
{
    $this->load->model('admin_model');
    $reservations = $this->admin_model->get_reservations();

    foreach ($reservations as $reservation)
    {
        $data['reservation'] = $reservation;
        $this->load->view('view_reservation', $data);    
    }
}

View

<div class="row">
    <?= $reservation->name ?>
    <?= $reservation->price ?>
    <?= $reservation->start_date ?> - <?= $reservation->end_date ?>
</div><!-- /.row -->
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top