0

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)

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

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

    <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>
1
  • What are the errors you are getting? Commented Jul 4, 2018 at 10:26

2 Answers 2

1

Hope this will help you :

Your controller dispdata_latest_news should be like this :

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 :

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 } ?>
Sign up to request clarification or add additional context in comments.

15 Comments

A PHP Error was encountered Severity: Notice Message: Undefined variable: data Filename: views/index.php Line Number: 107 Backtrace: File: C:\xampp\htdocs\CodeIgniter_try\application\views\index.php Line: 107 Function: _error_handler File: C:\xampp\htdocs\CodeIgniter_try\application\controllers\Home.php Line: 27 Function: view File: C:\xampp\htdocs\CodeIgniter_try\index.php Line: 315 Function: require_once
its pretty simple code pls cross check your code in controller it should be $result['data']=$this->Contact_model->displayrecords_latest_news();
Just updated my answer copy and use my code and check what u got, see my updated answer
Sir I'm not getting any errors now but the content which is their in my database is not getting displayed on my web page
instead of $query->result(); in your modal use $query->row(); this will fetch only single row
|
1

I have check your all files and codes and i think you have wrong in your view file. You have use $r->content. I think where you have get the data in model file the column name is different i.e. first_content. you have to use like this.

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

2 Comments

<?php if (! empty($data)) { foreach($data as $r){ ?> <tr><?php echo $r->content; ?></tr> <?php } } else { ?> <tr>no record found</tr> <?php } ?>
should i replace this whole part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.