【问题标题】:fetch database records in codeigniter在codeigniter中获取数据库记录
【发布时间】:2018-02-10 11:16:10
【问题描述】:

我正在通过这个链接学习CodeIgniter。我在从数据库中获取数据时有点困惑。

<h2><?php echo $title; ?></h2>

<?php foreach ($news as $news_item): ?>
    <h3><?php echo $news_item['title']; ?></h3>
    <div class="main">
            <?php echo $news_item['text']; ?>
    </div>
    <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>

在视图中显示数据时,他们在 foreach 循环中使用了变量 $news。然而,这个变量永远不会在任何地方定义

【问题讨论】:

  • $news 应该是一个关联数组,是的,你是对的,他们应该在该教程中定义...
  • 是的,我知道,但问题是它来自哪里
  • 如果你正确地学习了codeigniter,你肯定会知道这个变量与控制器相关联。它来自控制器
  • 从模型中获取数据时需要保存在变量或数组中。在您的情况下,变量/数组是 $news.

标签: php database codeigniter


【解决方案1】:

复制自here

$query = $this->db->get('mytable');

// Produces: SELECT * FROM mytable
The second and third parameters enable you to set a limit and offset clause:

$query = $this->db->get('mytable', 10, 20);

// Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
You'll notice that the above function is assigned to a variable named $query, which can be used to show the results:

$query = $this->db->get('mytable');

foreach ($query->result_array() as $row)
{
    echo $row['title'];
}

【讨论】:

    【解决方案2】:

    像这样尝试希望这会给你想要的东西,只需制作一个控制器并在构造函数中调用关联的模型并执行你想要的函数它将返回数组,这样你就可以轻松获取并在你的视图中使用它. 控制器代码

    class TestController extends Controller{
        function __construct() {
                                parent::__construct();
                                $this->load->helper('url');
                                $this->load->helper(array('form', 'url'));
                                $this->load->library('form_validation');                        
                                $this->load->model('User');             
                }
        public function index(){
        $data['all_user']=$this->User->getAllUser();
        $this->load->view('index',$data);
        }
        }
    

    这是我的模型代码

    class User extends CI_Model{
     public function __construct()
            {
                parent::__construct();
                $this->load->database();
            }
    
            public function getAllUser()
            {
                        $this->db->select('*');
                        $this->db->from('users');
                        return $this->db->get()->result();
            }
    }
    

    只需按照步骤操作 做一个控制器 在控制器内部创建一个函数 调用相关的模型函数,你一定会得到结果。

    【讨论】:

      【解决方案3】:

      你看过这个Controller 代码吗? (在您选择的代码上方,在教程中)

      public function index()
      {
              $data['news'] = $this->news_model->get_news();
              $data['title'] = 'News archive';
      
              $this->load->view('templates/header', $data);
              $this->load->view('news/index', $data);
              $this->load->view('templates/footer');
      }
      

      $news 作为$data 元素从Controller 传递到View

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        • 2020-10-26
        • 2011-05-29
        • 2014-06-02
        • 1970-01-01
        • 1970-01-01
        • 2018-06-08
        相关资源
        最近更新 更多