anta40
anta40

Reputation: 6743

How to display a table using CodeIgniter?

Just stared learning CodeIgniter (using v3.1.7). I have this table called training. enter image description here

I followed the steps described in this tutorial:

First, create a model called training_model.php (put it in /application/models)

<?php

    class training_model extends CI_Model {

        function get_all_trainings(){

            $q = $this->db->get('training');

            if($q->num_rows() > 0){

            foreach ($q->result() as $row){
                $data[] = $row;
            }
            return $data;
        }
    }

?>

Then create it's controller (training.php), put in /application/controllers.

<?php
if(!defined("BASEPATH")) exit("No direct script access allowed");

class training extends CI_Controller
{
  public function index()
  {
    $this->load->model("training_model"); 
    $data["trainings"]=$this->users_model->get_all_trainings(); 

    $this->load->view("users_view", $data);
  }
}

Last one is the view (training_view.php), put it into /application/views

<?php

if (!empty($trainings)){
    foreach ($trainings as $t){
        echo $t->eventName .' '. $t->eventDescription.' '.$t->eventDate.' '.$t->eventVenue;
    }
}

?>

How to access the view from the browser, so I can see the database's content?

Upvotes: 0

Views: 726

Answers (1)

DFriend
DFriend

Reputation: 8964

If your site was example.com then the url

http://example.com/training

should work.

You need to implement an .htaccess file for the above to work. Without .htaccess you would need this url

http://example.com/index.php/training

Read about removing the index.php file from the URL HERE.

Upvotes: 2

Related Questions