[eluser]crispinatari[/eluser]
Hi everyone!! :-)
While i'm enjoying using codeignitor so far i've been having trouble understanding the relationships between the numbers of controllers i need to create for each page in my website. Is it one controller to maintain all of the pages required in index or would there be controllers made for each seperate page eg.(site_controller, mainpage_controller, schools_controller, courses_controller, students_controller, results_controller) with seperate index functions for each page. I've created a database in SQL but now i'm creating a site to view the pages.
I'm creating a site_page that will feature a left_menu, a header a footer with a home(main) page in the center that navigates to those four different table views(pages)within the main page but i don't want the header, footer, or left menu to dissapear from view, the table (page) views are school, course, student and result, and i've called them studentlistview etc ...
My struggle has been creating a navigational website using the controller, i'm not 100% sure how to lay it out properly as i'm quite new to the MVC concept and i haven't found one tutorial that really covers this specific area in great detail.
So how do you make every page feature the header, footer, left_menu while making ONLY the main content navigate from page to page. its certainly a brain masher
So the student controller page is something like this:
student_controller.php
Code:
<?php
class Student extends Controller {
function student()
{
parent::Controller();
//$this->load->scaffolding('student');
$this->load->model('studentmodel');
$this->load->helper('url');
$this->load->helper('form');
}
function index()
{
//default method, gets data from model
//and lists all students using default view
$data['title'] = "Student application";
$data['heading'] = "Student List";
$data['res']=$this->studentmodel->get_all_students();
$this->load->view('studentlistview', $data);
}
function addstudent()
{
//form for adding an entry';
$data['title'] = "Add Student";
$data['heading'] = "Add Student";
$this->load->view('studentaddview',$data);
}
function insertstudent()
{
//calls the model function for actually inserting the new record
//get the data array from the post array sent by studentaddview.php
$data=$_POST;
//print_r($data);
//if using helper
//$data=$this->input->post();
//send the data to the insertstudent function in the model
$this->studentmodel->insertstudent($data);
//after insert, redirect back to main page
//header
//using url helper is easier, ie
redirect('views/index');
}
function deletstudent()
{
//called by studentlistview url, with segment 3 = id
//which is still used by studentmodel->delete_entry
//id is not posted, just part of url in the link code
$this->studentmodel->deletestudent();
//using the url helper
redirect('views/index');
}
}
?>
studentlistview.php
Code:
<html>
<head>
<title>Display All Students</title>
<link rel="stylesheet" type="text/css" href="students.css" />
</head>
<body>
<p><?=$heading?></p>
<a href = "http://localhost/ci1/student/addstudent">Add Student</a>
<?//if using url helper//=anchor('student/addstudent','Add Student');?>
<table class="student" width="57%" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<th>Student ID</th>
<th>Course ID</th>
<th> First Name</th>
<th> Last Name</th>
</tr>
<?php foreach($res as $row): ?>
<tr>
<td class="studentID"><?=$row->studentID?></td>
<td><?=$row->courseID?></td>
<td ><?=$row->first_name?></td>
<td ><?=$row->last_name?></td>
<td><?=anchor('student/editstudent/'.$row->studentID,'Edit');?></td>
<td><?=anchor('student/deletestudent/'.$row->studentID,'Delete');?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</body>
</html>
studentaddview.php
Code:
<html>
<head>
<title><?=$title?></title>
<link rel="stylesheet" type="text/css" href="students.css" />
</head>
<body>
<h1><?=$heading?></h1>
<h3>Enter new details below</h3>
<form action="http://localhost/ci1/index.php/emps/insertstudent" method="post">
<?//or using form helper //=form_open('blog/insertentry');?>
<p>Employee ID:<br>
<input type='text' name='studentID'></p>
<p>Course ID:<br>
<input type='text' name='courseID'></p>
<p>First Name:<br>
<input type='text' name='first_name'></p>
<p>Last Name:<br>
<input type='text' name='last_name'></p>
<p><input type='submit' value='Submit'></p>
</form>
DO I NEED SOMETHING LIKE THIS?
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class main_controller extends Controller {
function main_controller() {
parent::Controller();
$this->load->helper('file');
$this->load->helper('form');
}
function index() {
/// createe Dynamic Content //
$data ['title'] = "Student Website";
$data ['footer'] = $this->load->view('views/footer', $data, TRUE);
/// end content //
/// Load Partials //
$data ['header'] = $this->load->view('views/header', $data, TRUE);
$data ['menu'] = $this->load->view('views/menu', $data, TRUE);
/// END //////
$this->load->view('views/index', $data);
}
}
?>