繁体   English   中英

在CodeIgniter中查询和显示结果

[英]Query and display result in CodeIgniter

在此处输入图片说明 我有以下代码查询并显示数据库的结果。

数据库

id title desc 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>

尝试这个

在模型中

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');*/
}

控制器:

    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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM