【问题标题】:codeigniter won't show anything from my producten tablecodeigniter 不会显示我的 producten 表中的任何内容
【发布时间】:2014-06-20 08:01:41
【问题描述】:

谁能帮我看看。

型号:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_product extends CI_Model 
{
    public function getALL() 
    {
        $results = $this->db->get('producten');

        return $results->result_array();
    }
}

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller 
{
    public function index()
    {
        $this->load->model('Model_product');

        $data['products'] = $this->Model_product->getALL();
        $this->load->view('header');
        $this->load->view('welcome_message', $data);
        $this->load->view('footer');
    }
}

查看:

<table class="table">
<thead>
    <th>Naam</th>
    <th>Beschrijving</th>
    <th>Prijs</th>
    <th>Vooraad</th>
    <th>Categorie</th>
</thead>
<tbody>
    <?php foreach ($products as $product) { ?>
        <tr>
            <td><?php echo $product['naam']; ?></td>
            <td><?php echo $product['beschrijving']; ?></td>
            <td><?php echo $product['prijs']; ?></td>
            <td><?php echo $product['producten_op_voorraad']; ?></td>
            <td><?php echo $product['categorie_naam']; ?></td>
        </tr>
    <?php } ?>
</tbody>
</table>

加载数据库的正确表“producten”中有 1 行。我只是不知道该怎么办。谁能帮我解决这个问题?

【问题讨论】:

    标签: php database codeigniter echo


    【解决方案1】:

    这样试试

    型号

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Model_product extends CI_Model 
    {
    
        function __construct()
        {
             // Initialization of class
            parent::__construct();
        }
        public function getALL() 
        {
            $results = $this->db->get('producten');
    
            return $results->result_array();
        }
    }
    

    控制器

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Welcome extends CI_Controller 
    {
    
            function __construct()
            {
                 // Initialization of class
                parent::__construct();
            $this->load->model('model_product');
            }
        public function index()
        {
    
    
            $data['products'] = $this->model_product->getALL();
            $this->load->view('header');
            $this->load->view('welcome_message', $data);
            $this->load->view('footer');
        }
    }
    

    在 autoload.php 中

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

    【讨论】:

      【解决方案2】:

      Afaik 你必须调用模型 product_model.php,在控制器“product_model”中调用它并调用类“Product_model”(大写 P)。

      此外,CI 不会自动连接到数据库,因此在加载时您必须使用第三个参数指定它。

      所以:

      class Product_model extends CI_Model
      

      还有:

      $this->load->model('product_model', '', TRUE);
      

      ...

      $data['products'] = $this->product_model->getALL();
      

      【讨论】:

        【解决方案3】:

        请至少找出问题所在。你发了 M+C+V 说明你没有做任何努力。提出一个更具体的问题。

        检查是否执行了正确的文件和行(例如:echo),是否从数据库加载数据(var_dump/print_r/echo)等(主要是echo...真的。调试101。)。 ..

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多