Welcome Guest, Not a member yet? Register   Sign In
Using Menu in CI
#1

[eluser]Unknown[/eluser]
After searching the forum for answers I decided to post my problem. I never used any framework to develop application in PHP.

I created a test app to learn CI. Direct to the point.

My file structure, comments in brackets:

+ ci_sjc (I rename code igniter folder)
+ system
+ application (I understand you can move your application folder)
+ controller
- sjc.php (my controller file)
+ libraries
- sjc_menu.php (my menu class)
+ views
- sjc_emp_input.php
- sjc_footer.php
- sjc_header.php
- sjc_main.php
- sjc_menu.php


Code in my config.php
Code:
$config['base_url']    = "http://localhost/ci_sjc/";
$config['index_page'] = "";

Code in my autoload.php
Code:
$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url', 'form');
Code in my routes.php
Code:
$route['default_controller'] = "sjc";
       $route['scaffolding_trigger'] = "";

Code fo my sjc_menu.php (libraries)
Code:
<?php
class Sjc_Menu{
     function show_menu(){
          $obj =& get_instance();
          $obj->load->helper('url');
          $menu  = "<ul>";
          $menu .= "<li>";
          $menu .= anchor("sjc/index","List of Employer");
          $menu .= "</li>";
          $menu .= "<li>";        
          $menu .= anchor("sjc/input","Input Employer");        
          $menu .= "</li>";        
          $menu .= "</ul>";
        
          return $menu;
     }
}
?&gt;

My controller code sjc.php
Code:
&lt;?php

    class Sjc extends Controller {

        function Sjc()
        {
            parent::Controller();
            //$this->load->model('sjc_model');
        }
        function index(){
              $this->load->library('sjc_menu');
              $menu = new sjc_menu;
              $data['menu'] = $menu->show_menu();
              $this->load->view('sjc_main',$data);
         }
    
         function input(){
              $this->load->library('sjc_menu');    
              $menu = new sjc_menu;
              $data['menu'] = $menu->show_menu();    
              $this->load->view('sjc_emp_input',$data);    
         }
?&gt;

my views files -
sjc_main.php
Code:
&lt;html&gt;
&lt;head&gt;&lt;/head>
&lt;body&gt;
<div id="header">
&lt;?php $this->load->view('sjc_header'); ?&gt;
</div>
<div id="menu">
&lt;?php $this->load->view('sjc_menu'); ?&gt;
</div>

test list

<div id="footer">
&lt;?php $this->load->view('sjc_footer'); ?&gt;
</div>

&lt;/body&gt;
&lt;/html&gt;

sjc_input.php
Code:
&lt;html&gt;
&lt;head&gt;&lt;/head>
&lt;body&gt;
<div id="header">
&lt;? $this->load->view('sjc_header'); ?&gt;
</div>
<div id="menu">
&lt;? $this->load->view('sjc_menu'); ?&gt;
</div>

test input

<div id="footer">
&lt;? $this->load->view('sjc_footer'); ?&gt;
</div>

&lt;/body&gt;
&lt;/html&gt;

sjc_menu.php
Code:
&lt;?php
    echo $menu;
?&gt;

sjc_header.php
Code:
&lt;?php
    echo 'Test Header';
?&gt;
sjc_footer.php
Code:
&lt;?php
    echo 'Test Footer';
?&gt;

Tested the application http://localhost/ci_sjc/ it works as expected i.e. it displayed the header, the menu and the footer.

Problem is when I click any of the link in the menu it gives an error

Not Found
The requested URL /ci_sjc/index was not found on this server.

I need someone to suggest where I did wrong in the coding above. All the coding above I picked up from various toturial on the net.

Thank you.
#2

[eluser]Fabdrol[/eluser]
Few things:
Code:
$this->load->library('sjc_menu');
$menu = new sjc_menu;

That is not the CodeIgniter way. Load it like this:
Code:
public function __construct() {
    $this->load->library('sjc_menu');
    // putting the load code in __construct() makes it available tru the entire controller
}

public function index() {
    $data['menu'] = $this->sjc_menu->show_menu();
    // rest of my code
}

The problem however, is not related to that. Try adding the following to config.php:
Code:
$config['index_page'] = "index.php";
Now the anchor() functions should include index.php, and it should work. (If not, the problem is somewhere else!)
#3

[eluser]toopay[/eluser]
take a look at user guide, at this section http://ellislab.com/codeigniter/user-gui.../urls.html, and http://ellislab.com/codeigniter/user-gui...elper.html
hope that help you.
#4

[eluser]InsiteFX[/eluser]
Also you should not have a input method, it maybe confusing CodeIgniters
input library.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB