Sign in to follow this  
cignox1

[web] Best practice for dinamic site

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thank you very much! Then I will go with the path thing.

Quote:
Original post by Wan

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.


No, I don't do that now, because what I mean is that I did not know how to extract/store the category without having to explicitly declaring it in each link of the site. Using the path should work, and I will use a default name (i.e. links.html) for the related links section, to be found in each folder.

One last thing: currently I would specify each link as follows:

page.php?path=programming/programming.php

Is that the only way, or there are better alternatives?

Thank you again!

Share this post


Link to post
Share on other sites
Using the CodeIgniter framework, a path like:
.../page.php?path=programming/programming.php
would be:
.../page/programming
What this does is load controllers/page.php and execute the function called programming. The programming function might load views/programming.php and print out your HTML.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Using the CodeIgniter framework, a path like:
.../page.php?path=programming/programming.php
would be:
.../page/programming
What this does is load controllers/page.php and execute the function called programming. The programming function might load views/programming.php and print out your HTML.


Nice, I will give it a look. I use now the path, and it works quite well for my pouposes.
Thank you all.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this