Welcome Guest, Not a member yet? Register   Sign In
Pass a variable as the view name?
#1

[eluser]Adam Griffiths[/eluser]
I think my code will explain this a bit better...

Code:
<?php

class Pages extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {
        if($this->uri->segment(1) == NULL)
        {
            $this->load->view('header');
            $this->load->view('home');
            $this->load->view('footer');
        }
        else
        {
            $page = $this->uri->segment(2);

            $this->load->view('header');
            $this->load->view('$page'); // here is the problem
            $this->load->view('footer');
        }
    }
}

There's my controller...the problem is commented.

I get this error...

Quote:An Error Was Encountered

Unable to load the requested file: $page.php

Is there any way I can do this? I've tried many times with different quote marks etc, but nothing has worked so far.


Thanks.
#2

[eluser]Référencement Google[/eluser]
[quote author="Adam Griffiths" date="1217220130"]$this->load->view('$page');[/quote]

This won't parse the variable in the simple quotes. You should simply do:
$this->load->view($page);
#3

[eluser]Adam Griffiths[/eluser]
Thanks, but I already tried that. I did it again and got this error...

Quote:An Error Was Encountered

Unable to load the requested file: .php
#4

[eluser]Pascal Kriete[/eluser]
You're checking if segment 1 is empty, and then using segment 2. Segment 2 is probably empty as well.
#5

[eluser]Adam Griffiths[/eluser]
[quote author="inparo" date="1217221422"]You're checking if segment 1 is empty, and then using segment 2. Segment 2 is probably empty as well.[/quote]

haha, what a dumb mistake!!! Thank you! I really shouldn't be coding this late at night.




Theme © iAndrew 2016 - Forum software by © MyBB