Welcome Guest, Not a member yet? Register   Sign In
Step one ... Step two ... Step three ... Oops
#1

[eluser]a.g.r.c[/eluser]
Hey guys,

Recently I started to take the walk on the learning curve of CI. Having read few the introduction docs last night and watching a few video tutorials it was all so clear.

It still is to be honest, I'm probs just doing this wrong.

So the installation went fine, I got some scaffolding set up and managed to translate a template over to CI in a modular way using MVC.

Using $image_properties and img() for embedded images and re-working the html helper to find my assetts folder.

All was well.

Set up a few links and was passing the top nav to sub nav links no problems (easy stuff)

[controller, function] or blog and post

So then it was time to get a result from a DB.

A 3 fielded table with id, message and date.

So I then went on to make a model seting up a query

$data['query'] = $this->db-get('twitterClone');

Then loading the view with the data bound to it ready for selection.

Forgetting to mention that this needs to be a global function so autoload would be the right answer i think.

Problem was every time I passed over the var in header.php and looped over to get the result, it would always return an error

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$My Title

Filename: components/header.php

Line Number: 225

Any ideas guys, where has it all gone wrong? :'(

a.g.r.c
#2

[eluser]Tom Glover[/eluser]
Can you post your controller function code and your model code.
#3

[eluser]Adam Griffiths[/eluser]
It looks like your variable has a space. Variable names can't contain spaces.
#4

[eluser]Tom Glover[/eluser]
I'm not that observant, but now you mention it, yes your var does have a space, which it cant, replcae it with a _ or - or use camel case and that should solve your problem
#5

[eluser]a.g.r.c[/eluser]
Hey guys thanks for your help.

Model
Code:
class JournalModel extends Model
{
    
    function journalModel()
    {
        parent::Model();
    }
    
    function getTwitter()
    {
        $query = $this->db->get('twitter');
        return $query->result();        
    }

}

Controller
Code:
class Journal extends Controller
{
    function index()
    {    
        $this->load->view('components/header' $query);
        $this->load->view('components/bodyJournalHome');
        $this->load->view('components/recent');
        $this->load->view('components/footer');
    }
    
    function post()
    {
        $this->load->view('components/header');
        $this->load->view('components/bodyJournalPost');
        $this->load->view('components/recent');
        $this->load->view('components/footer');
    }
    
}

Retrieving the data in the header view.
Code:
<?php foreach($query as $row):?>
<?php echo $row->$message;?>
<?php endforeach;?>

More than likely this is messed up in a bad way, at the start it runs the query (model) which is then passed to the controller, the controller is serving up the view and passes the query also and then in the view the data is echoed.

Help again appreciated.

Thanks

a.g.r.c
#6

[eluser]Tom Glover[/eluser]
Controller need to be like so:

Code:
class Journal extends Controller
{
    function index()
    {    
        $this->load->model('modelName',,TRUE) //unless the model is auto loaded.
        $query = $this->modelName->getTwitter();

        $this->load->view('components/header', $query);
        $this->load->view('components/bodyJournalHome');
        $this->load->view('components/recent');
        $this->load->view('components/footer');
    }
    
    function post()
    {
        $this->load->view('components/header');
        $this->load->view('components/bodyJournalPost');
        $this->load->view('components/recent');
        $this->load->view('components/footer');
    }
    
}
#7

[eluser]a.g.r.c[/eluser]
Hey Tom,

Error
Code:
Fatal error: Call to undefined method Journal::_assign_libraries() in C:\wamp\www\Work\AlistairRossini\v3\system\libraries\Loader.php on line 185

Model
Code:
class JournalModel extends Model
{
    
    function journalModel()
    {
        parent::Model();
    }
    
    function getTwitter()
    {
        $query = $this->db->get('twitter');
        return $query->result();        
    }

}

Controller
Code:
class Journal extends Controller
{
    function index()
    {    
        $this->load->model('journal', TRUE); //unless the model is auto loaded.
        $query = $this->modelName->getTwitter();

        $this->load->view('components/header', $query);
        $this->load->view('components/bodyJournalHome');
        $this->load->view('components/recent');
        $this->load->view('components/footer');
    }
    
    function post()
    {
        $this->load->view('components/header');
        $this->load->view('components/bodyJournalPost');
        $this->load->view('components/recent');
        $this->load->view('components/footer');
    }
    
}

View
Code:
<?php foreach($query as $row):?>
<?php echo $row->$message;?>
<?php endforeach;?>

Not good mate, hope you can help.

Cheers

a.g.r.c
#8

[eluser]daniel.affonso[/eluser]
I think there is a problem in constructor of your model:

Code:
class JournalModel extends Model
{
    function journalModel()
    {
        parent::Model();
    }

The constructor must have the same name of the class.
(or __construct() in PHP5 way)

Let's see if this will help...
#9

[eluser]Tom Glover[/eluser]
Nope it the model you loaded:

You Loaded: $this->load->model('journal', TRUE);
$query = $this->modelName->getTwitter();

It should be in your case: $this->load->model('JournalModel',, TRUE); // When Loading a model the name stated needs to be the same as the class name in the model file. The manual explains this in more detail.
$query = $this->JournalModel->getTwitter();
#10

[eluser]daniel.affonso[/eluser]
right to the point! Smile




Theme © iAndrew 2016 - Forum software by © MyBB