0

Help me solve my problem... I'm using Codeigniter Framework for a couple of days now and trying to do some exercise on how to fetch my database table data

here is my code:

Controller

public function view($page = 'login'){

    if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
        show_404();
    }


    $data['title'] = ucfirst($page);
    $this->load->view('templates/header');
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/footer');

}

//this is my main controller.
}

Model

 Class Query_model extends CI_Controller{

 //what to put here???

 }

Views

<table class="table table-striped">
<thead>
 <tr>
  <th>Invoice Number</th>
  <th>Name</th>
  <th>Amount Due</th>
  <th>Date</th>
  <th>Due</th>
  <th>Status</th>
  <th>Actions</th>
 </tr>
</thead>
<tbody>
<?php 
 //here i wanna display those data..
?>
</tbody>

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Vier
  • 23
  • 7
  • Please make use of Codeigniter Documentation. There are all these answers. Have a look at it : https://www.codeigniter.com/user_guide – Robin Rai Jan 25 '18 at 05:47

1 Answers1

0

In Controller

public function view($page = 'login'){

    if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
        show_404();
    }
    $this->load->model('Query_model'); # load model

    $data['invoices'] = $this->Query_model->getData(); # get data

    $data['title'] = ucfirst($page);
    $this->load->view('templates/header');
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/footer');

}

In Model

Class Query_model extends CI_Model{
    public function getData(){
      $query = $this->db->get('mytable');
      $result = $query->result_array();
      return $result;
   }
}

In View

<tbody>
<?php 
    foreach ($invoices as $key => $invoice) {
        ?>
        <tr>
            <td><?php echo $invoice[''] ?></td> // InvoiceNumber
            <td><?php echo $invoice[''] ?></td> // Name
            <td><?php echo $invoice[''] ?></td> // Amount Due
            <td><?php echo $invoice[''] ?></td> // Date
            <td><?php echo $invoice[''] ?></td> // Due
            <td><?php echo $invoice[''] ?></td> // Status
            <td><?php echo $invoice[''] ?></td> // Actions
        </tr>

        <?php
    }
?>
</tbody>

Read these too

  1. How to fetch title of an item from a database and send it to the header template in CodeIgniter
  2. selecting data
Virb
  • 1,639
  • 1
  • 16
  • 25
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85