ytsejam
ytsejam

Reputation: 3439

Codeigniter Message: Undefined variable: infos

I am a newbie in CI. I used MY_Controller.php as main controller. I could open the ajax as the div#page loader. Now , My problem is although I load /about page, I get the database entries for the services model. How can i get the about table for the about controller ?

..
    function render_page($view) {
        if( ! $this->input->is_ajax_request() )
        {
          $this->load->view('templates/header', $this->data);
        }
        $this->load->view($view, $this->data);

        if( ! $this->input->is_ajax_request() )
        {
         $this->load->view('templates/menu');
         $this->load->view('templates/footer', $this->data);
        }
      }..

My services_model:

class Services_model extends CI_Model {

    function getAll() {
        $q = $this->db->get('services');
        if($q->num_rows() > 0){
        foreach ($q->result() as $row)
        {
            $data[] = $row;

            }
        return $data;
    }
    }
}

My home controller :

public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
    // Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}

When I use them in the home view there is no problem I can see the services_table :

<ul class="blog-medium">
<?php foreach($records as $row): ?>
<li>    
<div class="blog-medium-text">
<h1><a href="./post.html"><?php echo $row->title; ?></a></h1>
<p class="blog-medium-excerpt"><?php  echo $row->content; ?><br />
<a href="./post.html" class="read_more">Devamını Okumak için &rarr;</a></p>
</div>
<?php endforeach ?>

I want to use the same way in about page. About_model:

class About_model extends CI_Model {
    function getAll() {
        $q = $this->db->get('abouts');
        if($q->num_rows() > 0){
        foreach ($q->result() as $row)
        {
            $data[] = $row;

            }
        return $data;
    }
    }
}

About controller :

 public function view($page = 'about')
    {
    $this->load->helper('text');
    $this->data['records']= $this->about_model->getAll();
    if ( ! file_exists('application/views/pages/'.$page.'.php'))
    {
        // Whoops, we don't have a page for that!
    show_404();
    }
    $data['title'] = ucfirst($page); // Capitalize the first letter
    $this->render_page('pages/'.$page,$data);
    }

And this is my view file of about :

<div id="content">
<?php foreach($infos as $row): ?>
<h3 style="text-align: center;"> <?php echo $row->title; ?></h3>
<div class="hr">    </div>
<?php echo $row->content; ?>
<?php endforeach; ?>

I get the error telling me :

Severity: Notice
Message: Undefined variable: infos
Filename: pages/about.php
Line Number: 3

Why cant i get the abouts table?

Upvotes: 0

Views: 2181

Answers (1)

Ryan Gibbons
Ryan Gibbons

Reputation: 3601

You are calling a variable $infos in your foreach, but it is never passed in as a variable to your view.

Read the docs about Adding Dynamic Data to the View

You will either need to set $data['infos'] to something or, and I'm guessing this what you intended, use $records in your foreach

The above answers your specific, but after you provided the repo for your source, there are issues you are struggling with. I highly suggest you read through the entire documentation, staring with the Introduction: Getting Started, continuing to the Tutorials and then General Topics.

The reason you are having problems here, you have your routes.php setup to that everything is routed into the Home controller executing the view method. This method, while it accepts the page you want to see is always returning a fetch of the services model. Your other controllers are not getting executed at all. Based on your controller setup, if you would just remove the custom route, http://theurl/about would route to the About Controller. The default method to be loaded is index so if you change view to index, then it would be displayed by default.

Upvotes: 2

Related Questions