[web] Best practice for dinamic site

Started by
11 comments, last by cignox1 14 years, 1 month ago
Well, sorry for the title, I was not sure what to write. I'm in the process of learnin a bit of web programmin and am building a simple site. The site is split in a few sections, and a menu will alway highlight the icon of the section you are in. So far I've used a query string variable to identify the section, and this works well if the section has only one page. The problem is that in order to make that work with multiple pages per section, I should add that variable to any link in my site, and this sounds a bad thing. How is that usually done? My site is this The only two sections ready are those built with only one page. [Edited by - cignox1 on March 8, 2010 10:50:29 AM]
Advertisement
Normally there would be a variable in the database or set in the page that indicates what section the page belongs to. It really depends on how you store and process pages before displaying them.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Thank you. I don't use a database currently. Pages are stored as files. Perhaps I could put the sections in different folders, and then parse the path to get the section it belongs to?

To elaborate a bit how I currently load my pages: I have a main page.php wich contains, among the others, a left, a right and a 'content' sections. When I load the page I pass 2 variables in the query string: one is the page to load for the content (and thus decides which 'button' to highlight) while the other is the 'related links' page to open in the right column...

[Edited by - cignox1 on March 9, 2010 2:43:58 AM]
You could store your data in Xml and query at runtime. Performance shouldn't be too bad if the files are small. Using Xml you could define a "SectionId" tag, or something similar to help tie things together.

My previous site did something similar, but it's offline now.
Mmm, storing pages in xml would work, but since what I'm looking for is something that does not require per page work, this would not be easier/faster than simply specifying the variables in any link.
I expected this to be a fairly common problem with an equally popular answer...

Do you know any good tutorial about php real web site programming (not the trivial examples I see everywhere)?
Hello cignox1,

the 'active' page is stored in $_SERVER [´PHP_SELF´]. All you need is an array with the names of your pages and an index which menu is 'active'.

<?php
$i_am_here = $_SERVER [´PHP_SELF´];
$my_navi_array = array();
$my_navi_array["index.php"] = 0;
$my_navi_array["index2.php"] = 0;
$my_navi_array["music.php"] = 1;
$my_navi_array["music.php"] = 2;

$index_of_active_menu_entry = $my_navi_array[$i_am_here];

?>

Now you can build your leftmenu with foreach.
<?php
foreach(array_keys($my_navi_array) as $k => $entry){
if($my_navi_array[$entry] == $index_of_active_menu_entry){
//Display active link template
}else{
//Display normal link template
}
}
?>

You do not need to do this for every page. Just do this once in a menu.inc.php (or whatever) and include it everywhere you need it.

Or/and have a look at Smarty. http://www.smarty.net/

--GWDev
Thank you. I am not sure to understand what you mean though. Why do I need th PHP_SELF? I already know that the current page is page.php and the content page is the one passed as variable in the query string.
For the array: do you mean I have to add every page of the site to that menu? This also means that every page must be a php page (no simple html) because I have to 'include' this array...
Just to get this straight: the problem is how to highlight a menu item based on the category (music, programming etc.) a page belongs to?

In the past I have used a CSS solution akin to this. So in your case it would look something like this, I imagine:
(use page variable directly, or the extract category from page path)<?php $page = $_GET["page"];?><html>(header stuff shared by all pages goes here)<body id="<?php print $page ?>"><?php require("content/" . $page . ".htm"); ?></body></html>
(obviously a simplified example without any input validation whatsoever)
Quote:Original post by Wan
Just to get this straight: the problem is how to highlight a menu item based on the category (music, programming etc.) a page belongs to?

In the past I have used a CSS solution akin to this. So in your case it would look something like this, I imagine:
(use page variable directly, or the extract category from page path)<?php $page = $_GET["page"];?><html>(header stuff shared by all pages goes here)<body id="<?php print $page ?>"><?php require("content/" . $page . ".htm"); ?></body></html>
(obviously a simplified example without any input validation whatsoever)


Not really, I already have the working menu. I just would like to know which is the common way (if such way exists) to have different pages associated with a given 'section' of the page, so that I can build links inside my pages without worrying everytime to build the correct query string. If I had a database I could set a field that specifies the section of every page.
I'm not sure how to go (perhaps I'm completely off with that architecture at all).
In short, and if I understand your example, what I don't know is the

(use page variable directly, or the extract category from page path)

part. I think I could go with the path though and see how it works...

I would like to say again that I never did web programming, so if that whole architecture sounds 'strange' it most probably is, and if you can point me to more conventional ways to do that I will be grateful :-)

Thank you for the help.
Ah sorry, I understand now. [smile]

Then in your case I would probably go with the simple directory structure approach. It's a pretty natural way of organizing things. Of course it does have its problems:
  • The naming of the categories is restricted by what the file system allows as directory name: you can't use special characters, and maybe you won't be able to use long file names.

  • You can only name categories, you can't attach meta data to them, like a description.

  • It wouldn't work too well with a content management system: you'd need to modify, create and delete files and folders in order to change or reorganize content. There's a security issue as well, because this system would need almost full access to the file system.

  • You would need to have file duplicates if a page belongs to more than one category, which is of course a maintenance nightmare.


Using an XML file, or even having the structure hard-coded in a PHP script, would eliminate most of the above problems, however if you only have a few pages and categories, and you're the only person maintaining the website, you may not care.

Usually, the categories would be defined in a database. Most databases aren't actually very well suited for (deep) hierarchical structures, but since all the content already resides there, it's easier to have it all in one place and be able to match it against each other. And it increases maintainability and flexibility tremendously.


Quote:Original post by cignox1
In short, and if I understand your example, what I don't know is the

(use page variable directly, or the extract category from page path)

part.

Heh, I didn't want to insert too much comments in between the code, so I kept it short and unclear. [grin]
I was just hinting at determining the category from the relative path:
programming/perl/pong.htm ->main category = programming, subcategory = perl

You probably already do that.

This topic is closed to new replies.

Advertisement