在CodeIgniter中查询并显示结果

时间:2016-01-09 16:18:29

标签: php html codeigniter

enter image description here 我有以下代码来查询和显示数据库的结果。

数据库

id title desc 1建立 2美丽使用

模型文件夹中的代码

float3 offset{ 0.0f, 0.0f, 0.0f };
v.push_back(offset + float3{ -10, -10, -10 });
v.push_back(offset + float3{ +10, -10, -10 });
v.push_back(offset + float3{ +10, -10, +10 });
v.push_back(offset + float3{ -10, -10, +10 });

v.push_back(offset + float3{ -10, +10, -10 });
v.push_back(offset + float3{ +10, +10, -10 });
v.push_back(offset + float3{ +10, +10, +10 });
v.push_back(offset + float3{ -10, +10, +10 });

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

<?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>

我是否应该知道如何编辑代码以解决错误?

尝试01 控制器

A PHP ERROR WAS ENCOUNTERED

SEVERITY: NOTICE
MESSAGE: UNDEFINED VARIABLE: WEBINFO

模型

<?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;
        }
}
?>

2 个答案:

答案 0 :(得分:0)

试试这个

在模型中

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. 然后是其他所有功能
  4. 编辑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');*/
    }
    

答案 1 :(得分:0)

控制器:

    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>