Is this structuring valid for a site?

Hi there everyone,

I’m making a site on my localhost and i would like to know if the way that i’m grabbing my php pages are valid or not.

The Setup:
inside my index.php there is the basic html tags but inside the body tags i have a if statement to check if there is a “page” variable set in the url and if there is then a function runs to see what page is selected.

The function itself check to see if the actual php file exists and if it does the file is included in the body of the index page, now my question is - is this a valid way to dynamically load in content pages?

I have a .htaccess file that rewrites the urls to be more user friendly it changes the index.php?page=somepage to /somepage.
As i understand it google reads the url to understand where it is on your site.

Am i totally missing something or is this way of getting php file valid?

Thanks for reading this,any help will be great.

Valid? Yes. Good? No.

This is normally the way it is done:

  1. Determine the page requested
  2. Load appropriate logic, run any queries
  3. Output HTML content

This is the way you are doing it:

  1. Output HTML content
  2. Determine page requested
  3. Load appropriate logic, run any queries
  4. Continue outputting HTML content.

The first method pushes the data into the template. All of the necessary logic is run to create the data before it is output in the template. Your method pulls data into the template which violates the principal of separation of logic and content and makes your code ugly and difficult to manage. There are times when your method is suitable, such as long after the code has been written someone wants to add additional content. In that case, it is easier to just require additional logic when the template is reached than have to go back and integrate within the existing code base. But if you are writing your own code, the first method is generally the way to start.

Hi cheesedude,
Thanks for the replay.

How i understand it (as you explain it) my code is doing the following:
html (loading head section and the other tag)
php (loading all the page content)
html (loading in the footer and closing tags)

is this what you mean? and if so how do i change the way my content is loaded in after the html is done?
I have already coded the most of the site and i can’t start from the beginning or am i screwed.

I recommend reading the first half of From Flat PHP to Symfony2. The first half is all plain PHP (no frameworks), and it’ll show you how you can refactor code to be better organized.

Jeff Mott, Thank you for pointing me to that amazing article, i now really understand how MVC’s works.
The thing is the way that i work with my code is there anything wrong with using it that way (for now)? i’m on a time constraint and if i have to converting all my code to MVC it will take to long, so i’m thinking of finishing the site (i’m nearly done only styling and some functions left) and then converting everything to MVC, is that a bad idea?

That’s actually a good way to proceed. “First make it work. Then make it better.” :slight_smile:

Just make sure the page variable doesn’t allow funky values, such as “…/config/db” or something like that. But other than that, your setup should be good to at least get you up and running.

Thanks again, i have one more question and this one is about google.

When google goes thought your site does it count every click (step) it takes? or do they use your url to see how deep your site is?
Let my explain the what i mean with click (step):

Lets say google is on the home page (.com/) on this page there is a link that google will follow (category/headsets) ----- so that will be (.com/category/headsets)

google will see the home page (index page) as level 0 in your site structure, now my question is:
When google follows that link will they see the new page as level 1 because they only had one step to go to get to the page or will they read the url and see two sections and see that it’s level 2?

This is something that has been bothering me if anyone has any idea how this work that will be so great.
I’m new to this form and i have to say you guys are amazing with helping me, thanks again.