【问题标题】:Query and display result in CodeIgniter在 CodeIgniter 中查询并显示结果
【发布时间】:2016-01-09 16:18:29
【问题描述】:

我有以下代码查询并显示来自数据库的结果。

数据库

id 标题描述 1 建立 2 美好的使用

模型文件夹中的一段代码

<?php
class Portfolio_model extends CI_Model {

        public function __construct()
        {
                $this->load->database();
        }

        public function get_webinfo()
        {
                $query = $this->db->select('title')->from('webinfo')->where('id', 1)->get();
                return $query->row_array();
        }
}
?>

控制器文件夹中的一段代码

<?php
    class Portfolio extends CI_Controller {

        public function view($portfolio = 'home')
        {
                if ( ! file_exists(APPPATH.'/views/portfolio/'.$portfolio.'.php'))
            {
                    // Whoops, we don't have a page for that!
                    show_404();
            }

            $data['title'] = ucfirst($portfolio); // Capitalize the first letter

            $this->load->view('templates/header', $data);
            $this->load->view('portfolio/'.$portfolio, $data);
            $this->load->view('templates/footer', $data);
        }

        public function __construct()
        {
                parent::__construct();
                $this->load->model('portfolio_model');
                $this->load->helper('url_helper');
        }

        public function index()
        {
                $data['webinfo'] = $this->portfolio_model->get_webinfo();

                 $this->load->view('templates/header', $data);
                $this->load->view('portfolio/index', $data);
                $this->load->view('templates/footer');
        }
    }
?>

我要显示数据的页面

<h2 class="intro-text text-center">
                        <strong><?php echo $webinfo['title']; ?></strong>
                    </h2>

但是,当我运行显示页面时出现以下错误

A PHP ERROR WAS ENCOUNTERED

SEVERITY: NOTICE
MESSAGE: UNDEFINED VARIABLE: WEBINFO

我可以知道我应该如何编辑代码来解决错误吗?

试试 01 控制器

<?php
    class Portfolio extends CI_Controller {
        public function view($portfolio = 'home')
        {
                if ( ! file_exists(APPPATH.'/views/portfolio/'.$portfolio.'.php'))
            {
                    // Whoops, we don't have a page for that!
                    show_404();
            }

            $data['title'] = ucfirst($portfolio); // Capitalize the first letter

            $this->load->view('templates/header', $data);
            $this->load->view('portfolio/'.$portfolio, $data);
            $this->load->view('templates/footer', $data);
        }

        public function __construct()
        {
                parent::__construct();
                $this->load->model('portfolio_model');
                $this->load->helper('url_helper');
        }

        public function index()
        {
                $data['webinfo'] = $this->portfolio_model->get_webinfo();

                print_r($data['webinfo']);
                /* $this->load->view('templates/header', $data);
                $this->load->view('portfolio/index', $data);
                $this->load->view('templates/footer');*/
        }
    }
?>

型号

<?php
class Portfolio_model extends CI_Model {

        public function __construct()
        {
                $this->load->database();
        }

        public function get_webinfo()
        {
                $query = $this->db->query("SELECT title FROM webinfo WHERE id = 1");
                $result = $query->result_array();
                return $result;
        }
}
?>

查看

<h2 class="intro-text text-center">
                        <strong><?php echo (!empty($webinfo[0]['title'])) ? $webinfo[0]['title'] : 'Empty Title' ;; ?></strong>
                    </h2>

【问题讨论】:

  • 你能在你的索引方法中为我var_dump($data['web_info']);吗?并在此处发布结果。

标签: php html codeigniter


【解决方案1】:

试试这个

在模型中

public function get_webinfo()
{
    $query = $this->db->query("SELECT title FROM webinfo WHERE id = 1");
    $result = $query->result_array();
    return $result;
}

在视图中

<h2 class="intro-text text-center">
    <strong><?php echo (!empty($webinfo['title'])) ? $webinfo['title'] : 'Empty Title' ;; ?></strong>
</h2>

注意:如果我是对的,有时这个$webinfo['title'] 会出错,并且它与$webinfo[0]['title'] 一起工作正常


还有写控制器的方法。

  1. __construct
  2. index()
  3. 然后是其他所有功能

编辑 01

public function index()
{
    $data['webinfo'] = $this->portfolio_model->get_webinfo();

    print_r($data['webinfo']);
    /* $this->load->view('templates/header', $data);
    $this->load->view('portfolio/index', $data);
    $this->load->view('templates/footer');*/
}

【讨论】:

    【解决方案2】:

    控制器:

        public function index()
            {
                    $data['result'] = $this->portfolio_model->get_webinfo();
    
                     //$this->load->view('templates/header', $data);
                    //$this->load->view('portfolio/index', $data);
                   // $this->load->view('templates/footer');
    
                    print_r($data);
            }
    

    型号

        public function get_webinfo()
    {
       $this->db->select('*');
             $this->db->from('pwebinfo');
             $this->db->where('id',1);
    
    $query = $this->db->get();
    
    if($query->num_rows() ==''){
                    return 'failure';       
            }else{
                    $results = $query->result();        
                    $result = $results[0];
                    return $result; 
            }
    }
    

    查看

     <h2 class="intro-text text-center">
    
     <strong> <?php echo $result[0]->title; ?> ?></strong>
     </h2>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多