Pandey
Pandey

Reputation: 11

How can i call the dynamic page contents in codeigniter

I am making a website using Codeigniter

My models are as

I have one controller to handle all the pages with calling different methods inside Controller Class.

The home page model is working fine;but when i call the about page the inner contents inside about page is giving error As: Message: Undefined variable: a Whereas i am simply passing an array to the view of about page I have used print_r($a) on the view but the error is same


Controller Code:

class Pages extends CI_Controller{

  public function __construct()
    {
            parent::__construct();
            $this->load->model('home_model');
            $this->load->model('about_model');
            $this->load->helper('url_helper');
    }
    public function index(){
        $page = 'home';
        $test_cms['query']=$this->home_model->getcms();
        $test_cms['title'] = 'Home Page';
        $this->load->view('templates/header', $page);
        $this->load->view('pages/'.$page, $test_cms);
        $this->load->view('templates/footer',$page);

    }
    public function home(){
    $page = 'home';
        $test_cms['query']=$this->home_model->getcms();
        $test_cms['title'] = 'Home Page';
        $this->load->view('templates/header', $page);
        $this->load->view('pages/'.$page, $test_cms);
        $this->load->view('templates/footer',$page);
   }
   public function about(){
    $page = 'about';

        $a['dada']=$this->about_model->testabout();
        $data['title']='other page';
        $this->load->view('templates/header', $page);
        $this->load->view('pages/'.$page, $a);
        $this->load->view('templates/footer',$page);
   }
 }

Home Page Model

class Home_model extends CI_Model{
public function __construct()
{
 $this->load->database(); 
}
public function getcms() {
    $q = $this->db->query('select * from cms');
    if($q->num_rows() > 0){
        foreach ($q->result_array() as $row)
        {
            $data[] = $row;
        }
    return $data;
    }
}

}


About Page Model

class About_model extends CI_Model{
public function testabout() {
    $about['content']="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    return $about;
  }   

}

Every time when i call the about page it is loading header and footer contents correctly but inner contents of about page saying the variable is undefined. whereas i am simply calling the array inside it.

I am beginner.Can you please suggest me where i am going wrong.

Upvotes: 0

Views: 1901

Answers (2)

devpro
devpro

Reputation: 16117

As per your comments you are using

print_r($a);

In your aboutus view you need to use

print_r($dada);

Becuase you define $dada inside the $a array. And its define as $dada and $a is your array.

Upvotes: 1

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

Always pass data to view with $data

$data['query'] = $this->home_model->getcms(); # change  $test_cms['query'] to $data['query']
$data['title'] = 'Home Page';

So accordingly change all

In home_model.php

class Home_model extends CI_Model{
    public function __construct()
    {
     $this->load->database(); 
    }

    public function getcms() 
    {
        $query = $this->db->query('select * from cms');
        $result = $query->result_array();
        $count = count($result);

        if (!empty($count)) {
            return $result;
        }
        else{
            return false;
        }
    }
}

In Controller

public function home()
{
$page = 'home';
    $result = $this->home_model->getcms();
    if($result == false)
    {
        $data['query'] = "No data found"
    }
    else{
        $data['query'] = $result;
    }
    $data['title'] = 'Home Page';
    $this->load->view('templates/header', $page);
    $this->load->view('pages/'.$page, $test_cms);
    $this->load->view('templates/footer',$page);
}

In view

print_r($query);

Upvotes: 2

Related Questions