CodeIgniter Forums
href to display page - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forum-10.html)
+--- Thread: href to display page (/thread-74866.html)



href to display page - dmorgan20 - 11-17-2019

Afternoon,

PHP and code igniter is new to me, so still getting to grips in understanding what does what.

Im using the following HTML: 
Code:
<li><a href="/application/views/appointments/booking.php"><span class=""></span>BOOK NOW</a></li>

Im getting a white blank page.

The file I want to view is in:

- application -> views -> appointments

I assume I need to change some settings somewhere in order to get this functioning but ive no idea where to start


RE: href to display page - dave friend - 11-17-2019

Links go to controllers/function and optionally, arguments i.e. controllers/function/arg

Usually there are no file names, (i.e. booking.php) involved.

Read this and the tutorial.


RE: href to display page - dmorgan20 - 11-18-2019

I've tried this to no avail.

I've copied the tutorial and it doesn't exactly break it down step by step for someone with no knowledge.

Example, its doesn't say where this needs to be, does it go at the bottom of the pages.php controller? Is it meant to have a } at the end and one taken away from the class? Because the manual doesn't explain it

public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}


RE: href to display page - dave friend - 11-18-2019

(11-18-2019, 10:34 AM)dmorgan20 Wrote: I've tried this to no avail.

I've copied the tutorial and it doesn't exactly break it down step by step for someone with no knowledge.

Example, its doesn't say where this needs to be, does it go at the bottom of the pages.php controller?  Is it meant to have a } at the end and one taken away from the class? Because the manual doesn't explain it

    public function view($page = 'home')
    {
            if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
            {
                    // Whoops, we don't have a page for that!
                    show_404();
            }

            $data['title'] = ucfirst($page); // Capitalize the first letter

            $this->load->view('templates/header', $data);
            $this->load->view('pages/'.$page, $data);
            $this->load->view('templates/footer', $data);
    }

This might sound snarky but that's not my intent. Early in the tutorial, you created an empty view function. The above adds code to that function. That seems pretty obvious since the code comes in a section called Adding logic to the controller. (OK, that is snarky. Please forgive me.)

Now, to answer the question in the OP.

If you have worked all the way through the Static Pages tutorial page, then you should have added a couple of $routes. (This is explained at the bottom of the tutorial page.)  If that is done correctly you can browse to http://example.com/index.php and will see the "home" page.

NOTE: I have added "index.php" to the URL examples here because I assume you are not using a .htaccess file that will rewrite URLs so they don't need to include "index.php". If you have set up .htaccess then you don't need to use "index.php" in the URLs as shown here.

If you wanted a link on the "home" page that sends a user to the "bookings" page it would look like this.

Code:
<a href="index.php/bookings"><span class=""></span>BOOK NOW</a>

If you have .htaccess working then the link would be

Code:
<a href="bookings"><span class=""></span>BOOK NOW</a>

There's a bit of magic happening due to the combination of routes and the definition of Pages::view().

"bookings" is the name of the view file that winds up being the argument to view(), i.e. Pages::view('bookings'). The view file is at /application/views/Bookings.php.

Hope that helps.


RE: href to display page - dmorgan20 - 11-18-2019

Just wanted to thank you for replying - I got the tutorial setup working. Just to explain why I got confused...

The first part tells me to add:

Code:
<?php
class Pages extends CI_Controller {

        public function view($page = 'home')
        {
        }
}

The 'adding logic to the controller' said to add the following:

Code:
public function view($page = 'home')
{
        if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
}

In true newbie fashion I've put 2 + 2 together and got 6 with this: 

Code:
public function view($page = 'home')

{

public function view($page = 'home')
{
        if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
}

}


Apologies if I come across annoyed, as simple as this is to do I was getting frustrated.

Thank you for the help