[eluser]stuffradio[/eluser]
Hey Jenks.
I had to do some googling to find out what exactly Models did, and then I searched the CI user guide to see how to use them. You're in luck though because I will show you how to use it.
Say you wanted to get everything from a database. You have your controller like this:
Code:
<?php
class Information extends Controller {
function Information()
{
parent::Controller();
}
function index()
{
}
}
That's the core of the controller. So lets write an example model:
Code:
<?php
class Latest extends Model {
function Latest()
{
parent::Model();
}
}
So, I believe this would be the best name for the model because you're basically getting the latest info of different things. Lets make a new function in the Model class for getting News articles.
We're going to get all the latest articles. Put this underneath 'function Latest()'
Code:
function news()
{
$query = $this->db->get('news');
return $query->result();
}
In the Controller we need to load the model now.
Code:
function Information()
{
$this->load->model('latest');
}
We can now pull all results from the model in the index page.
Code:
function index()
{
$display['info'] = $this->latest->news();
$this->load->view('index', $display);
}
We now need a view file to handle the results. Call it index.php and put it in your views folder.
Code:
<?php foreach($info as $news):?>
<h1>Static Headline</h1>
<br />
<?php echo $news->message; ?>
<?php endforeach; ?>
$info is the variable containing the array of info from the database, we named it $news. Message is the field in the news table. That's where I got those things from.
It's very hard for me to see if I got everything right right now, so I will put it all together and look after I post.
information.php (Location: system/application/controllers/)
Code:
<?php
class Information extends Controller {
function Information()
{
parent::Controller();
$this->load->model('latest');
}
function index()
{
$display['info'] = $this->latest->news();
$this->load->view('index', $display);
}
}
latest.php (Location: system/application/models/)
Code:
<?php
class Latest extends Model {
function Latest()
{
parent::Model();
}
function news()
{
$query = $this->db->get('news');
return $query->result();
}
}
index.php (Location: system/application/views/)
Code:
<?php foreach($info as $news):?>
<h1>Static Headline</h1>
<br />
<?php echo $news->message; ?>
<?php endforeach; ?>
I got a bit carried away with this bit of information, but I hope you can get some info from it