Johan Broddfelt
/* Comments on code */

Adding content

So now all we want to do is add some content. So we start by creating a structure in which we will organise our content. Create a folder called views, and inside that a folder called start. In the start folder we create the file main.php.

/views/start/main.php

<div>
    Her is our start page
</div>

The dives are again used to create spacing without distorting the content element.
In order to get this into our page we can easily write the following in our printContent function in index.php

function printContent() {
    include('views/start/main.php');
}

And then we and this to our main.css

#content>div {
  padding: 20px;
}

This might be okey if we only should hav one page on our site. But we want to add a few more pages. Therefore we need to device a method to select what page to show. I would like my url to look like http://www.mysite.se/index.php?module=[module]&view=[view]&id=[id].

So lets try this by entering the following code into our printContent function
Read more about the filter_values filter here http://php.net/manual/en/filter.filters.php

function printContent() {
    echo 'Module: ' . filter_input(INPUT_GET, 'module', FILTER_SANITIZE_URL) . '<br>';
    echo 'View: ' . filter_input(INPUT_GET, 'view', FILTER_SANITIZE_URL) . '<br>';
    echo 'Id: ' . filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT) . '<br>';
    include('views/start/main.php');
}

Now you can see the data when you run this url: http://www.mysite.se/index.php?module=start&view=main&id=9 and with that data we can tell the php-code to show the page selected in the url like this.

function printContent() {
    $module = filter_input(INPUT_GET, 'module', FILTER_SANITIZE_URL);
    $view = filter_input(INPUT_GET, 'view', FILTER_SANITIZE_URL);
    $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
    
    // Get the page we want to display. If no module is provided we will get the start page
    if ($module == '') {
        include('views/start/main.php');
    } else {
        include('views/' . $module . '/' . $view . '.php');
    }
}

We will save the id for later when we want to select data from the database to display. But with this knowledge I can go on and create my link page, about page and my start page. If no module is provided we will get the start page

Now let's add the following links in the menu of our main_template.php and as pages in our view folder

  <a href="index.php?module=page&view=links">Links</a>
  <a href="index.php?module=page&view=about">About</a>

/views/page/links.php

<div>
    <h1>Links</h1>
    Here are some of the greatest resources on the Internet when it comes to web development. 
    If I have missed any, please feel free to mail me some more great ones.<br>
    <br>
    PHP<br>
    ...
</div>

/views/page/about.php

<div>
    <h1>About</h1>
    Here goes some text about me...
</div>

Now we can click the links and vi the corresponding pages.

- Framework, PHP

Follow using RSS

<< Style your page How to use classes >>

Comment

Name
Mail (Not public)
Send mail uppdates on new comments
0 comment