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.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分