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>
Pradeep
  • 9,667
  • 13
  • 27
  • 34
keerthi patil
  • 123
  • 2
  • 13

2 Answers2

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 } ?>
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • 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 – keerthi patil Jul 04 '18 at 10:42
  • its pretty simple code pls cross check your code in controller it should be `$result['data']=$this->Contact_model->displayrecords_latest_news();` – Pradeep Jul 04 '18 at 10:52
  • Just updated my answer copy and use my code and check what u got, see my updated answer – Pradeep Jul 04 '18 at 11:01
  • 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 – keerthi patil Jul 04 '18 at 11:13
  • Uncaught ReferenceError: google is not defined at HTMLDocument. (functions.js:67) at j (jquery-2.1.1.min.js:2) at Object.fireWith [as resolveWith] (jquery-2.1.1.min.js:2) at Function.ready (jquery-2.1.1.min.js:2) at HTMLDocument.I (jquery-2.1.1.min.js:2) – keerthi patil Jul 04 '18 at 11:13
  • its a js error not related to your php code and content u getting not displayed on your web page is just beacause ur `$data` is empty – Pradeep Jul 04 '18 at 11:22
  • use `print_r($result);die;` in your controller before the view loading to check what is coming – Pradeep Jul 04 '18 at 11:31
  • sir i just pasted the code print_r($result);die; above $this->load->view('display_records',$result); – keerthi patil Jul 04 '18 at 12:13
  • updated `dispdata_latest_news` method see my updated answer – Pradeep Jul 04 '18 at 13:14
  • make sure column name should be correct what u r accessing in your view – Pradeep Jul 04 '18 at 14:04
  • thank you sir what u said i tried and it helped me. i would say its not completely clear answer but around 90% it was correct thank you sir for helping – keerthi patil Jul 05 '18 at 04:27
  • https://stackoverflow.com/questions/18951168/invalid-argument-supplied-for-foreach-in-codeigniter – keerthi patil Jul 05 '18 at 04:28
  • '; echo '

    '.$row->first_content.'

    '; echo '

    '.$row->second_content.'

    '; echo ''; } } ?>
    – keerthi patil Jul 05 '18 at 05:19
  • sir i used this in my view, i have one simple doubt i want to fetch only single row so wht i should for that because, if i use this im getting all the row which is their in the database – keerthi patil Jul 05 '18 at 05:19
  • 1
    instead of `$query->result();` in your modal use `$query->row();` this will fetch only single row – Pradeep Jul 05 '18 at 05:21
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; ?>
Sandeep K.
  • 759
  • 6
  • 18