doushang1890 2018-08-10 07:26
浏览 18

Codeigniter页面数据不显示

I trying to get a very simple query results set to display on a page using Codeignighter.

For my controller i have this:

class Homepage extends CI_Controller {

function __construct() {
    parent::__construct();
    $this->load->helper('string');
    $this->load->helper('text');
    $this->load->library('form_validation');
    $this->load->helper('url');
    $this->load->model('Job_sectors_model');
}


public function index(){
    $page_data['sectors'] = $this->Job_sectors_model->get_sectors();
    $this->load->view('header');
    $this->load->view('homepage', $page_data);
    $this->load->view('footer');
}
}

and for my model file i have this:

    class Job_sectors_model extends CI_Model {

      function get_sectors() {
        return $this->db->get('sectors');
      }

    }   

On my view file i have a simple select:

                  <select class="form-control" name="job_sectors">
                     <option>Choose job sector...</option>
                     <?php foreach ($sectors->result() as $row) : ?>
                      <option value="<?php echo $row->nc_id ; ?>"><?php echo $row->tc_name ; ?></option>
                     <?php endforeach ; ?>
                 </select>

I have this setting in my autoload.php file:

$autoload['libraries'] = array('database', 'session');

The page loads and the select has the correct number of items from the db table. What I am getting is an error in each of the seelcts option when you open the select. I see thiswhen I inspect it:

A PHP Error was encountered

Severity: Notice
Message:  Undefined property: stdClass::$nc_id
Filename: views/homepage.php
Line Number: 68

So the nc_id is a table column name, its just not returning the value. How do i define the property to stop this error from happening?

Many thanks M


Ok i now have it working but it doesnt feel right:

so my model is this:

    class Job_sectors_model extends CI_Model {

    function get_sectors() {

        $query = $this->db->get('sectors');
        return $query->result_array();

    }        

    }   

my view is this:

<select class="form-control" name="job_sector">
                <?php foreach ($sectors as $sector): ?>
                  <option value="<?php echo $sector['NC_ID']; ?>"><?php echo $sector['TC_NAME']; ?></option>
                <?php endforeach; ?>
              </select>

and my controller is this:

class Homepage extends CI_Controller {

function __construct() {
    parent::__construct();
    $this->load->helper('string');
    $this->load->helper('text');
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->library('form_validation');
    $this->load->model('Job_sectors_model');
}


public function index(){
    $this->load->view('header');
    $page_data['sectors'] = $this->Job_sectors_model->get_sectors();
    $this->load->view('homepage', $page_data);
    $this->load->view('footer');    
}

When I say ity doesnt feel right is this the right way to code in codeigniter, feels like i'm hacking it together to make it work?

Thanks

  • 写回答

3条回答 默认 最新

  • dongshangan2074 2018-08-10 07:43
    关注

    2 possibilities. 1 the column name actually doesn't exist, 2 you don't have any rows. This prevents the later from causing issues.

    class Job_sectors_model extends CI_Model {
    
          function get_sectors() {
            $q = $this->db->get('sectors');
            if ($q->num_rows > 0) {
                return $q->result();
            }
            return false;
          }
    
        }   
    

    View:

            <?php if ($sectors): ?>
              <select class="form-control" name="job_sectors">
                     <option>Choose job sector...</option>
                     <?php foreach ($sectors as $row) : ?>
                      <option value="<?php echo $row->nc_id ; ?>"><?php echo $row->tc_name ; ?></option>
                     <?php endforeach ; ?>
                 </select>
              <?php else: ?>
                 No sectors
              <?php endif; ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)