извлекать данные из базы данных, используя codeigniter - PullRequest
0 голосов
/ 04 июля 2018

Я загрузил изображение и связанный с ним контент в базу данных, теперь я пытаюсь извлечь его из их и отобразить на моей веб-странице, но при получении я получаю некоторые ошибки Так что, пожалуйста, кто-нибудь может мне помочь, где я иду не так? Я не знаю концепцию правильно, но насколько я знаю, что я реализовал это, пожалуйста, помогите мне

это мой 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);
        }

        ?>

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 (вид)

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

Ответы [ 2 ]

0 голосов
/ 04 июля 2018

Я проверил все ваши файлы и коды, и я думаю, что вы ошиблись в вашем файле просмотра. Вы используете $ r-> content. Я думаю, где у вас есть данные в файле модели, имя столбца отличается, то есть first_content. Вы должны использовать как это.

<?php     foreach($query  as $r): ?>
            <tr>    <?php echo $r->first_content; ?>
            </tr>
<?php endforeach; ?>
0 голосов
/ 04 июля 2018

Надеюсь, это поможет вам:

Ваш контроллер 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 } ?>
...