ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

linking to a subview

May 01, 2008 6:51pm

Subscribe [0]
  • #1 / May 01, 2008 6:51pm

    chuvak

    3 posts

    Hello everyone,
    Please excuse my ignorance as I am just learning CI, but already do like it very much.
    Here is my question…

    I am trying to link to a view from the default view. So, in localhost/site/view, I have

    <a href="<?=base_url()?>view/subview">

    and in the controller for View,

    function index()
        {
            if (!$this->uri->segment(2))
            { 
                $view = 'view';
            }
            else
            {
                $view = $this->uri->segment(2);
            }
            $this->load->view('header');
            $this->load->view("view/$view");        
            $this->load->view('footer');
        }

    what happens is that the ‘else’ part in the conditional only works if I pass “view/view” in the URL, but not if it is “view/subview”, which is at the same level in the “view” directory, but has a different name. In this case, I just get a 404…

    I hope I explained the problem well enough. Thanks for looking and hope somebody can shed some light on this…

  • #2 / May 01, 2008 8:00pm

    gtech

    824 posts

    I must admit what you are doing send shivers down my timbers,

    Are you using a htaccess file? if so remove it lets keep things simple add it later if you get things working

    what is your system/application/config/config.php base_url and index_page set to?
    check your system/application/config/routes.php what is your default_controller set to

    what I think you are trying to do is use 1 controller for multiple views… so this is how I would do it:

    1:first I would check my system/application/config/config.php

    ..
      // obviously this may differ on your set up
      $config['base_url'] = 'http://localhost/CodeIgniter/';
      ..
      $config['index_page'] = "index.php";

    2:then check my default controller in system/application/config/routes.php

    $route['default_controller'] = "home";

    3:Then create a home controller in the controllers directory called home.php (note file names and class names are case sensitive lowercase for file names capital letter for class names)
    controllers/home.php

    <?php
    class Home extends Controller   {
            function Home(){
              parent::Controller();
            }
    
            function index() {
              $this->load->view('start');
            }
    
            // note if paramter not passed in its set to default view
            function loadview($view='view')
            {
              $this->load->view('header');
              $this->load->view("view/".$view);        
              $this->load->view('footer');
            }
    }
    ?>

    4: Then you should be able to link to it as follows in the view called start
    note I use site url to get the full url (my site_url() will produce “http://localhost/CodeIgniter/index.php”)
    views/start.php

    <a href="http://&lt?=site_url?&gt/home/loadview/testview">Link</a>

    5:you can then create a testview in your view directory

    views/testview.php

    hello world

    6:not forgetting to create the header and footer view 😊.
    view/header.php

    <h2>HEADER</h2>

    view/footer.php

    <hr>

    7:when you browse to http://localhost/CodeIgniter/ the view with the link should appear (your url may of course differ)

    *note to access the loadview method in the home controller directly the url will look somthing like:
    http://localhost/CodeIgniter/index.php/home/loadview/

  • #3 / May 01, 2008 11:04pm

    chuvak

    3 posts

    gtech, thanks.
    Sorry about sending shivers down your spine - like I said, I’m just getting the hang of CI, and my problem is not that I’m a complete noobie to PHP, but rather that I expect (after seeing CI being so great so many times before) things to be easier and more intuitive than they are.
    Single controller / multiple views is exactly what I’m trying to accomplish - I think your solution should work perfectly. I’ll let you know shortly if it works.
    Thank you!

  • #4 / May 02, 2008 11:37am

    chuvak

    3 posts

    Ok, the loadview approach works perfectly, thanks!
    I have another blatantly ignorant question, however 😊
    - Why can’t I pass $view to index, thereby eliminating the “loadview” segment in the URL (“site_url/home/loadview/view”)?
    so…

    function index($view='view')
    {
        $this->load->view('header');    
        $this->load->view("view/".$view);
        $this->load->view('footer');
    }

    Tried it, didn’t work. Is there any way to accomplish this?
    Thanks for your patience and helpfulness!

  • #5 / May 02, 2008 1:16pm

    gtech

    824 posts

    yes then you would have to use <site_url>/home/index/

    the other way you could do it is pass you paramaters through the POST mechanism or through the session variable, thus removing the need for a parameter to be passed through the URL.

  • #6 / May 02, 2008 4:52pm

    chuvak

    3 posts

    What prevents “$this->uri->segment()” in my initial post from working?

  • #7 / May 02, 2008 5:03pm

    gtech

    824 posts

    well nothing, I prefer to use variable names and parameters as it is more readable in my opinion and easier to default.

    if you use the segment method you will still suffer from the same problem and will need the href to <site_url>/home/index/ for it to work as you are passing the parameter through the url.

    if you link to <site_url>/home/ codeigniter will expect the parameter to be the controllers function name and that is why you get the 404 error, page not found.

    you can use a form to select the view rather than a href.. this means the variables will get POSTED to the controller rather than through the url.  then in the controller method you can retrieve the values

    $view   = $this->input->post('view', true);
  • #8 / May 02, 2008 5:12pm

    gtech

    824 posts

    views/start.php .. you can use a dropdown if you prefer to give a list of options.

    <FORM action="<?=site_url()?>/home" method="post">
       Enter View Name: <INPUT type="text" id="viewname">
    
     </FORM>

    home controller

    ....
    function index()
    {
        $view   = $this->input->post('viewname', true); 
    
        $this->load->view('header');    
        $this->load->view("view/".$view);
        $this->load->view('footer');
    } 
    ....

    as you are not passing parameters through URL you have no need to put the index part in the url… so your form will submit to <site_url()>/home.

    I hope that makes sense.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases