PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 29, 2022

[FIXED] Need help to fetch data from database in php codeigntier

 January 29, 2022     codeigniter, php     No comments   

Issue

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.


Solution

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);
}


Answered By - M Ikram Zafar
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing