Welcome Guest, Not a member yet? Register   Sign In
linking to a subview
#1

[eluser]chuvak[/eluser]
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

Code:
<a href="&lt;?=base_url()?&gt;view/subview">

and in the controller for View,

Code:
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

[eluser]gtech[/eluser]
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
Code:
..
  // 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
Code:
$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
Code:
&lt;?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');
        }
}
?&gt;
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
Code:
<a href="&lt;?=site_url()?&gt;/home/loadview/testview">Link</a>

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

views/testview.php
Code:
hello world

6:not forgetting to create the header and footer view Smile.
view/header.php
Code:
<h2>HEADER</h2>
view/footer.php
Code:
<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/h...<PARAMETER>
#3

[eluser]chuvak[/eluser]
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

[eluser]chuvak[/eluser]
Ok, the loadview approach works perfectly, thanks!
I have another blatantly ignorant question, however Smile
- Why can't I pass $view to index, thereby eliminating the "loadview" segment in the URL ("site_url/home/loadview/view")?
so...
Code:
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

[eluser]gtech[/eluser]
yes then you would have to use <site_url>/home/index/<parameter>

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

[eluser]chuvak[/eluser]
What prevents "$this->uri->segment()" in my initial post from working?
#7

[eluser]gtech[/eluser]
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/<parameter> for it to work as you are passing the parameter through the url.

if you link to <site_url>/home/<parameter> 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
Code:
$view   = $this->input->post('view', true);
#8

[eluser]gtech[/eluser]
views/start.php .. you can use a dropdown if you prefer to give a list of options.
Code:
&lt;FORM action="&lt;?=site_url()?&gt;/home" method="post"&gt;
   Enter View Name: &lt;INPUT type="text" id="viewname"&gt;<BR>
&lt;/FORM&gt;

home controller
Code:
....
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.




Theme © iAndrew 2016 - Forum software by © MyBB