0

I am trying to build a project in php codeigniter. But I'm stuck to fetch data from database.

I'm using mysql, codeigniter-3.1.10 & php-7.0.33.

Database config file:

    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '221b',
    'database' => 'car',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

My model is

if (defined('BASEPATH') OR exit ('No direct script access allowed'));
    class Homepage_model extends CI_Model{
        public $result;
        public $rs;

        public function __construct(){
            parent::__construct();
            $this->load->database();

        }

        public function get_data($slug = FALSE){
            if ($slug === FALSE) {
                $query = $this->db->get('cars');
                return $query->result_array();
            }

            $query = $this->db->get_where('cars', array('status' => "Available"));
            return $query->row_array();
        }
    }

In model file I want to fetch all data from the table cars when the value of database field "status" is 'Available'. Controller file:

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

class Homepage extends CI_Controller{
    public function __construct(){
        parent::__construct();
    }
    public function index(){
        $this->load->model('Homepage_model');
        $rs = $this->Homepage_model->get_data();
        $this->load->view('homepage_view', $rs);

    }
}

and my view portion where I'm going to show the data is:

                foreach($rs as $rws){
                    //echo $rws['car_id'];

            ?>
                <li>
                    <?php echo $rws['car_id'];?>
                    <a href="book_car.php?id=<?php echo $rws['car_id'] ?>">
                        <img class="thumb" src="cars/<?php echo $rws['image'];?>" width="300" height="200">
                    </a>
                    <span class="price"><?php echo 'Kshs.'.$rws['hire_cost'];?></span>
                    <div class="property_details">
                        <h1>
                            <a href="book_car.php?id=<?php echo $rws['car_id'] ?>"><?php echo 'Car Make>'.$rws['car_type'];?></a>
                        </h1>
                        <h2>Car Name/Model: <span class="property_size"><?php echo $rws['car_name'];?></span></h2>
                    </div>
                </li>
            <?php
                }
            ?>

I've done this. But when I browse the webpage none of this data is show.Please help to solve this.

3
  • in get_data you are not passing slug param, so second result will return and that result is an row_array` so you cant use it in foreach so return result_array() instead of row_array() Commented Jun 10, 2019 at 4:30
  • I've changed the row_array() as result_array(), but nothing happened. Commented Jun 10, 2019 at 4:39
  • print the response r u getting data or not from DB. Commented Jun 10, 2019 at 4:42

4 Answers 4

1

In Your Controller Function pass $data Variable like $this->load->view('homepage_view', $data);

public function index(){
    $this->load->model('Homepage_model');
    $result = $this->Homepage_model->get_data();
    $data['rs'] =  $result; 
    $this->load->view('homepage_view', $data);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Now the $rs variable will be available on view file
0

Here is changed in query

$query = $this->db->get_where('cars', array('status' => "Available"))->result_array();
return $query;

Comments

0

Just change $row_array() with $result_array()

return $this->db->get_where('cars', array('status' => "Available"))->result_array();

row_array() is used to fetch only one while $result_array() all records in the form of an associative array

Read more about row array and result array

Comments

0

In Model

public function get_data($slug = FALSE){
    if ($slug === FALSE) {
        $query = $this->db->get('cars');
        return $query->result_array();
    }

    $query = $this->db->get_where('cars', array('status' => "Available"));
    $result = $query->result_array(); # change
    return $result; # change
}

In Controller

public function index(){
    $this->load->model('Homepage_model');
    $result = $this->Homepage_model->get_data();
    $data['rs'] =  $result; # or  $data['rs']  = $this->Homepage_model->get_data(); assign directly 
    $this->load->view('homepage_view', $rs);
}

And In View foreach($rs as $rws){ will works

Make sure the table is not empty.

4 Comments

in controller if I write this $this->load->view('homepage_view', $rs); this gives me an error "Undefined variable: rs" and if I write this $this->load->view('homepage_view', $data['rs']); this is shown nothing.
@Sharif use as i posted $result = $this->Homepage_model->get_data(); $data['rs'] = $result;
I did as you said. But this happens "Message: Undefined variable: rs"
Abdulla, please try to refrain from using quote/citation formatting, unless the material you are presenting is a quote. Thanks!

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.