简体   繁体   English

使用codeigniter从数据库中获取数据

[英]fetch data from the database using codeigniter

i have uploaded a image and related content for it in the database, now im trying to fetch it from their and display it on my webpage, but im getting some errors while fetching it , so please can any one help me where im going wrong? 我已经在数据库中上传了图像及其相关内容,现在我正尝试从他们的图像中获取并显示在我的网页上,但是在获取图像时出现了一些错误,所以请提供任何帮助我出现问题的地方吗? i don know the concept correctly but just how much i know i have implemented it please help me 我不正确地知道这个概念,但是我知道我已经实现了多少,请帮助我

this is my Home.php(controller) 这是我的Home.php(controller)

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

        ?>

Contact_model(model) Contact_model(型号)

        <?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(view) 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>

Hope this will help you : 希望这个能对您有所帮助 :

Your controller dispdata_latest_news should be like this : 您的控制器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);
}

Your model displayrecords_latest_news should be like this : 您的模型displayrecords_latest_news应该像这样:

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

} }

Your view should be like this : 您的视图应如下所示:

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

I have check your all files and codes and i think you have wrong in your view file. 我已经检查了您所有的文件和代码,并且我认为您的查看文件有误。 You have use $r->content. 您已经使用$ r-> content。 I think where you have get the data in model file the column name is different ie first_content. 我认为您在模型文件中获得数据的列名称不同,即first_content。 you have to use like this. 您必须像这样使用。

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

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

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