dongmei9203 2018-03-20 18:04
浏览 18
已采纳

如何使用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?

  • 写回答

1条回答 默认 最新

  • duanjia8215 2018-03-20 19:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?