🔧 Step 2: Write PHP site engine

We’ll use a very simple PHP architecture, to serve the site.
We will need an index.php file. Create a similar PHP structure and files as we did in this exercise: [add link once ready]

We need 6 files:

  • index.php (main file for including other files)
  • header.php
  • footer.php
  • view-tasks.php (content file which shows all the added tasks)
  • single-task.php (content file which shows one task)
  • add-task.php (content file which shows a form where you can add a new task)

What each file does?

index.php

# make database connection
# include header file
# if get "page" variable is empty, or get "page" variable equals "tasks"
    ## include view-tasks file
# if get "page" variable equals "task"
   ## include single-task file
# if get "page" variable equals "create"
   ## include add-task file
# include footer file

header.php

Header file will include all the beginning of the HTML document, so starting from <!DOCTYPE html> tags, and the code which would be shown in all our pages.
So include all the HTML part of your header file in this file as well, as you want the same header to be shown on every page.

view-tasks.php

As defined in the index.php file, view-tasks file is loaded, when GET[‘page’] parameter is “tasks”.
So to this file, include only the content part of the view-tasks html file.

single-task.php

As defined in the index.php file, single-task file is loaded, when GET[‘page’] parameter is “task”.
So to this file, include only the content part of the single-task html file.

add-task.php

As defined in the index.php file, add-task file is loaded, when GET[‘page’] parameter is “create”.
So to this file, include only the content part of the view-tasks html file.

footer.php

Footer file, just like header, will be included across your website. We didn’t create a design for our footer, but we still need to end the HTML document. So just copy the HTML document ending tags to footer.php file.

</body>
</html>

This will finish the rendered document in your browser.

Conclusion

So now if you visit:

  • yourdomain.com/?p=task, you should see a proper page with a task.
  • yourdomain.com/?p=create, you should see a proper page with a form to create a task
  • yourdomain.com/?p=tasks, you should see all your tasks

This is how the document is being built up with index.php

Scroll to Top