[eluser]JamesTaylor[/eluser]
So i am trying to get my 1st model up and running with CodeIgniter...
I have built a template page that contains the header and footer elements that will be consistant throughout the site. I then have a controller which is passing various information to the page via and array called $data - these are elements such as the page title, h1 tag, h2 tag etc. This is all working fine.
Here is my controller:
Code:
class Home extends Controller {
//Home Page
function index(){
$data['main_content'] = 'Home';
$data['title'] = 'Welcome to the Golf Club';
$data['h1'] = 'Golf Club';
$data['h2'] = 'Welcome to the Golf Club';
$data['BodyClass'] = 'Home';
$data['FlashFile'] = 'crossfade_xml';
$data['Image1'] = 'Image1.jpg';
$data['Alt1'] = 'Pict1.jpg';
$data['Image2'] = 'Image1.jpg';
$data['Alt2'] = 'Pict2.jpg';
$data['Image3'] = 'Image1.jpg';
$data['Alt3'] = 'Pict3.jpg';
$data['Image4'] = 'Image1.jpg';
$data['Alt4'] = 'Pict4.jpg';
$this->load->view('template', $data, $NextComp);
}
}
Towards the footer of the page i am attempting to have a section of dynamic content that will show the next competition, the next social event and latest news by referencing a database - (the site is for a golf club).
i've set a model to select the next competition as follows:
Code:
class NextComp extends Model {
function index()
{
$datetoday = time();
$query = $this->db->query('
SELECT CompName
, CompFormat
, CompDate
FROM clubcompetitions
WHERE CompDate + 86399 >= $datetoday
ORDER BY CompDate ASC
LIMIT 0, 1');
return $query();
}
}
I have already built this once in straight php and the code works, although it may not be the best method of doing it as am still learning! I am attempting to transport the existing code into CodeIgniter to learn the way it works.
Anyway, I now need to setup the controller to to call the method and pass the resulting information to the view which is where i am struggling to get my head around it.
I am currently trying to add this to my controller:
Code:
//Select Next Comp from DB for footer
$this->load->model('NextComp');
$NextComp = array();
if($query = $this->NextComp->index())
{
$NextComp['results'] = $query;
}
//Loads view & passes $data and $NextComp
$this->load->view('template', $data, $NextComp);
}
and then display it in the view with:
Code:
<?php echo $results['CompName']; ?>
but i get an error saying that 'results' is an undefined variable?
As i say i am just starting out with CodeIgniter and have got this far from following various tutorial and some trial and error so i may be miles away from a working solution or almost there, i don't know as i've been going round in circles for the last few hours!
can anyone give me a point in the right direction please, it'd be much appreciated!
Thanks
James