Welcome Guest, Not a member yet? Register   Sign In
Creating a Navigation Site with a Database in CI
#1

[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 Tongue

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>&lt;?=$heading?&gt;</p>


<a href = "http://localhost/ci1/student/addstudent">Add Student</a>

&lt;?//if using url helper//=anchor('student/addstudent','Add Student');?&gt;

<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>
&lt;?php foreach($res as $row): ?&gt;
    <tr>
    <td class="studentID">&lt;?=$row->studentID?&gt;</td>
    <td>&lt;?=$row->courseID?&gt;</td>
    <td >&lt;?=$row->first_name?&gt;</td>
    <td >&lt;?=$row->last_name?&gt;</td>
  
    <td>&lt;?=anchor('student/editstudent/'.$row->studentID,'Edit');?&gt;</td>
    <td>&lt;?=anchor('student/deletestudent/'.$row->studentID,'Delete');?&gt;</td>
    
    </tr>
&lt;?php endforeach; ?&gt;
</table>
</div>
&lt;/body&gt;
&lt;/html&gt;

studentaddview.php

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="students.css" /&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>&lt;?=$heading?&gt;</h1>

    
<h3>Enter new details below</h3>    


&lt;form action="http://localhost/ci1/index.php/emps/insertstudent" method="post"&gt;
&lt;?//or using form helper //=form_open('blog/insertentry');?&gt;

<p>Employee ID:<br>
&lt;input type='text' name='studentID'&gt;&lt;/p>
<p>Course ID:<br>
&lt;input type='text' name='courseID'&gt;&lt;/p>
<p>First Name:<br>
&lt;input type='text' name='first_name'&gt;&lt;/p>
<p>Last Name:<br>
&lt;input type='text' name='last_name'&gt;&lt;/p>

<p>&lt;input type='submit' value='Submit'&gt;&lt;/p>

&lt;/form&gt;

DO I NEED SOMETHING LIKE THIS?

Code:
&lt;?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);  
   }
}
?&gt;
#2

[eluser]TheFuzzy0ne[/eluser]
Generally, a controller should represent a "module" of your site. In this case it's "students". Generally, you'd have methods such as add, delete, show and update.

You can include views from within views by simply calling $this->load->view('header') or something along those lines.
#3

[eluser]crispinatari[/eluser]
I still dont have a clear understanding of how it all fits together as a whole navigational website and the correct coding, i've tried for days now...

its all a bit too much, i give up =(
#4

[eluser]TheFuzzy0ne[/eluser]
Is your navigation static or dynamic?
#5

[eluser]crispinatari[/eluser]
Its a dynamic site only so i can make changes when need be, its just working out the views and how they relate to each other that is a little tricky at this stage and what is needed in the controller.
#6

[eluser]TheFuzzy0ne[/eluser]
OK, let's see if I can clear this up for you at all.

In most cases, each controller method has it's own view. You set the variables in the controller method for that view.

Within those views, you can include other views, for example:
Code:
&lt;?php $this->load->view('site_nav'); ?&gt;
<h2>Some title</h2>
...

Assuming your site navigation expects some variables, you can either set those variable from within your controller method, or export the functionality to a model or helper.

With a model, you'll most likely make a call to the model method from within each controller method:

Code:
$data = $this->my_model->get_global_vars(); # I'm assuming that this method returns an array containing all of the global variables that your site navigation uses.
...
# And set some of the other data specific to the current controller method.
$data['site_title'] = $this->config->item('site_title');
$data['user_name'] = $this->session->userdata('username');

With a helper, you can have similar functionality, where the helper calls upon the model, and sets the global variables for your view:
Code:
function load_global_vars()
{
    $CI =& get_instance();
    $CI->load->vars($CI->my_model->get_global_vars());
}
So with the above code, the variables for site nav, and anything else that's needed on each request are automatically loaded and ready for use by your view and any sub-views. You'd just need to add any variables specific to your controller method from within the actual controller method.

Does this make anything any clearer?




Theme © iAndrew 2016 - Forum software by © MyBB