0

I am fetching the data from the database.But I am getting error.Here I need to display product name and image in the page.But I am getting the error undefined variable 'name' in the page. Please check my code give your valuable suggestions,Thank you.

My controller

<?php

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

    class productdisplay_ctrl extends CI_Controller {

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

            $this->load->helper('url');
            $this->load->model("productdisplay_model");
        }

        public function productfetch(){

            $this->load->model("productdisplay_model");
            $result = $this->productdisplay_model->fetchData();
            $data['name'] = $result->sub3_category_name;

            $this->load->view('home', $data);
        }

    }

?>

my model

<?php

    class productdisplay_model extends CI_Model {

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

        public function fetchData() {

            $this->db->where($where);
            $this->db->order_by('rand()');
            $this->db->select('*');
            $this->db->from('sub3_category');
            $query = $this->db->get();

            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                    $data[] = $row;
                }

                return $data;
            }
            return false;
        }

    }

?>

my view

<div class="container-main">

  <div class="col-xs-12 col-sm-4 col-md-3">
    <div class="prod-container">
      <a href="<?php echo base_url(); ?>index.php/welcome/productlist">
        <div class="prod_img">
          <img src="<?php echo base_url();?>images/img-electronics.jpg" width="100%"/>
        </div>
        <div class="prod_desc">
          <div class="prod-round-icon"></div>
          <h4 class="prod_title"><?php echo $name; ?></h4>
          <p class="prod_text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
        </div>


        <div class="view-more"> view more</div>
      </a>  
    </div>
  </div>
1
  • What is the contents of the $result variable after $result = $this->productdisplay_model->fetchData();? Commented Jan 25, 2017 at 10:47

3 Answers 3

4

Controller

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

            class productdisplay_ctrl extends CI_Controller {

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

                    $this->load->helper('url');
                    $this->load->model("productdisplay_model");
                }

                public function productfetch(){

                    $this->load->model("productdisplay_model");
                    $result = $this->productdisplay_model->fetchData();
                    $data['name'] = $result->sub3_category_name;

                    $this->load->view('home', $data);
                }

            }

        ?>

Model:

    <?php

        class productdisplay_model extends CI_Model {

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

            public function fetchData() {

              $this->db->select('*');
                $this->db->where($where);
                $this->db->order_by('rand()');
                $this->db->from('sub3_category');
                $query = $this->db->get();

                if ($query->num_rows() > 0) {

                    return  $query->row();
                }
                return false;
            }

        }

    ?>

view:

<div class="container-main">

  <div class="col-xs-12 col-sm-4 col-md-3">
    <div class="prod-container">
      <a href="<?php echo base_url(); ?>index.php/welcome/productlist">
        <div class="prod_img">
          <img src="<?php echo base_url();?>images/img-electronics.jpg" width="100%"/>
        </div>
        <div class="prod_desc">
          <div class="prod-round-icon"></div>
          <h4 class="prod_title"><?php echo $name; ?></h4>
          <p class="prod_text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
        </div>


        <div class="view-more"> view more</div>
      </a>  
    </div>
  </div>
Sign up to request clarification or add additional context in comments.

Comments

0

Change in model.like this.use row() which returns result in object format having matched row.so that you can access columns using arrow operator.

And what about your $where variable.set it.

public function fetchData() {

            $this->db->select('*');
            $this->db->where($where);
            $this->db->order_by('rand()');
            $this->db->from('sub3_category');
            $query = $this->db->get();

            if ($query->num_rows() > 0) {

                return  $query->row();
            }
            return false;
        }

Comments

0

inside the foreach loop, remember $row var is a object

if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row->field;
            }

Comments

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.