CodeIgniter Forums

Full Version: Cant load my view
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]lucasvm[/eluser]
Hello

Im new in CodeIgniter and im trying to create simple website, i want to show my index page but im getting this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Index::$load

Filename: controllers/index.php

Line Number: 6


Fatal error: Call to a member function view() on a non-object in site\application\controllers\index.php on line 6

My controller index.php has this:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Index extends CI_Controller {

    function index()
    {
        $this->load->view('index', $data);
    }
}

And in views the html code in index.php

Any solution?

El Forum

[eluser]danmontgomery[/eluser]
You can't name a controller Index.

http://ellislab.com/codeigniter/user-gui...names.html

El Forum

[eluser]lucasvm[/eluser]
Thank you, thats works, but now im trying to show some content from my database but i get undefined variable data on my index, this is my code in funcion index:

Code:
function index()
    {
                $this->load->database();
                $query = $this->db->query('myquery');
                foreach ($query->result() as $row)
                {
                    $data['titulo'] = $row->titulo;
                    $data['contenido'] = $row->contenido;
                    $data['autor'] = $row->autor;
                }

                $this->load->view('header');
                $this->load->view('principal', $data);
                $this->load->view('footer');
    }

but if i do echo $data on my index view i get variable undefined, why?

El Forum

[eluser]InsiteFX[/eluser]
Code:
function index()
{
    $data = array();

    $this->load->database();
    $query = $this->db->query('myquery');
    foreach ($query->result() as $row)
    {
        $data['titulo'] = $row->titulo;
        $data['contenido'] = $row->contenido;
        $data['autor'] = $row->autor;
    }

    $this->load->vars($data);

    $this->load->view('header');
    $this->load->view('principal');
    $this->load->view('footer');
}

InsiteFX

El Forum

[eluser]cahva[/eluser]
[quote author="lucasvm" date="1303428949"]
but if i do echo $data on my index view i get variable undefined, why?[/quote]

When you pass $data to your view, there wont be a variable called $data in your view(unless you have $data['data'] set). From your example you would have $titulo, $contenido and $autor at your disposal.

But there is an error in your controller also. You iterate through the result and set the variables with the same name over and over again. You would only have the last $titulo etc. from the result.

Just pass the whole result to your view(and btw, you should put the db calls to model):
Code:
// In controller
$query = $this->db->query('myquery');
$data['rows'] = $query->result();

$this->load->view('header');
$this->load->view('principal',$data);
$this->load->view('footer');

In view:
Code:
<table>
    <tr>
        <th> Titulo </th>
        <th> Contenido </th>
        <th> Autor </th>
    </tr>
&lt;?php foreach ($rows as $row): ?&gt;
    <tr>
        <td>&lt;?= $row->titulo ?&gt;</td>
        <td>&lt;?= $row->contenido ?&gt;</td>
        <td>&lt;?= $row->autor ?&gt;</td>
    </tr>
&lt;?php endforeach; ?&gt;
</table>