【问题标题】:fetch data from the database using codeigniter使用 codeigniter 从数据库中获取数据
【发布时间】:2018-07-04 10:14:36
【问题描述】:

我已经在数据库中上传了一张图片和相关内容,现在我试图从他们那里获取它并显示在我的网页上,但是我在获取它时遇到了一些错误 ,所以请任何人都可以帮助我我哪里出错了?我不正确地了解这个概念,但我知道我已经实现了多少,请帮助我

这是我的 Home.php(控制器)

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

    class Home extends CI_Controller {  

        public function __construct() 
        {
            parent::__construct();

            //load database libray manually
            $this->load->database();

            //load Model
            $this->load->model('Contact_model');

            // load form and url helpers
            $this->load->helper(array('form', 'url'));

            // load form_validation library
            $this->load->library('form_validation');
        }

        public function Latest_news()
        {  
            $this->form_validation->set_rules('first_content', 'First content', 'required');
            $this->form_validation->set_rules('second_content', 'Second content', 'required');

            if ($this->form_validation->run()==TRUE)
            {
                $config['upload_path']   = FCPATH.'uploads/'; 
                $config['allowed_types'] = 'gif|jpg|png'; 
                $this->load->library('upload', $config);

                if ( $this->upload->do_upload('filename') )
                {
                    //print_r($this->upload->data());die; 
                    $data['first_content'] = $this->input->post('first_content');
                    $data['second_content'] = $this->input->post('second_content');
                    $data['filename'] = $this->upload->data('file_name');  

                    //Transfering data to Model
                    $this->Contact_model->latest_news($data);
                    //Redirecting to success page
                    redirect(site_url('Home/Latest_news'));     
                }
                else
                {
                    $error = array('error' => $this->upload->display_errors());
                    print_r($error);die;
                }
            }
            else
            {
                  $this->load->view('Latest_news'); 

            }
        } 


        public function dispdata_latest_news()
        {
        $result['data']=$this->Contact_model->displayrecords_latest_news();
        $this->load->view('display_records',$result);
        }

        ?>

联系模型(模型)

        <?php
            class Contact_model extends CI_Model 
            {

                function latest_news($data)
                {
                    //saving records
                    $this->db->insert('latest_news', $data); 
                }

                //Display
                function displayrecords_latest_news()
                {
                    //Displaying Records
                    $query=$this->db->query("select first_content from latest_news");
                    return $query->result();
                }

            }

        ?>

index.php(视图)

    <div class="lates">
        <div class="container">
          <div class="text-center">
            <h2>Latest News</h2>
          </div>
          <div class="col-md-4 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="300ms">
            <img src="<?php echo base_url('images/4.jpg');?>" class="img-responsive" />

           <table>
            <tr>
            <th>Content</th>

            </tr>
            <?php foreach($query  as $r): ?>
            <tr><?php echo $r->content; ?>

                </tr>
            <?php endforeach; ?>
            </table>

    </div>

【问题讨论】:

  • 你遇到了什么错误?

标签: php codeigniter codeigniter-2 codeigniter-query-builder


【解决方案1】:

希望对您有所帮助:

你的控制器dispdata_latest_news应该是这样的:

public function dispdata_latest_news()
{
  $rows = $this->Contact_model->displayrecords_latest_news();
  $result = array('data' => $rows);

  /* OR use it like this 
    $result = ['data' => $rows];
  */

  $this->load->view('display_records',$result);
}

你的模型displayrecords_latest_news应该是这样的:

function displayrecords_latest_news()
{
  $query = $this->db->get("latest_news");
  if ($query->num_rows() > 0)
  {
      return $query->result();
  }

}

你的视图应该是这样的:

<?php 
   if (! empty($data))
   {
     foreach($data  as $r){ ?>
        <tr><?php echo $r->first_content; ?></tr>
<?php }
   }
   else { ?>
        <tr>no record found</tr>
<?php } ?>

【讨论】:

  • 遇到 PHP 错误严重性:通知消息:未定义变量:数据文件名:views/index.php 行号:107 回溯:文件:C:\xampp\htdocs\CodeIgniter_try\application\views\ index.php 行:107 函数:_error_handler 文件:C:\xampp\htdocs\CodeIgniter_try\application\controllers\Home.php 行:27 函数:查看 文件:C:\xampp\htdocs\CodeIgniter_try\index.php 行:315函数:require_once
  • 它的代码非常简单,请交叉检查控制器中的代码,它应该是$result['data']=$this-&gt;Contact_model-&gt;displayrecords_latest_news();
  • 刚刚更新了我的答案副本并使用我的代码并检查你得到了什么,看看我更新的答案
  • 先生,我现在没有收到任何错误,但是我的数据库中的内容没有显示在我的网页上
  • 而不是 $query-&gt;result(); 在您的模式中使用 $query-&gt;row(); 这将只获取单行
【解决方案2】:

我检查了您的所有文件和代码,我认为您的视图文件有误。 你已经使用了 $r->content。我认为您在模型文件中获取数据的位置是不同的,即 first_content。 你必须像这样使用。

<?php     foreach($query  as $r): ?>
            <tr>    <?php echo $r->first_content; ?>
            </tr>
<?php endforeach; ?>

【讨论】:

  • content; ?> 找不到记录
  • 我应该替换整个部分吗
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2020-04-22
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
相关资源
最近更新 更多