Dynamic/Changing Sidebars

I get a lot of people sending me e-mails, or using my Q&A thing to ask me how I did my dynamic/changing sidebars on the site. I think people over-complicate it and assume it's a magic PHP thing when really, it's not. Now, you have to bear in mind I don't use WordPress and my pages are all static to cut down on unnecessary database queries, but generally there's no reason why anyone should have problems applying the same code to their own sites.

Firstly, my site relies on a handful of files that I request using include() a.k.a. "PHP includes". I have a header, which stays the same, and then a footer for each section of the site. That is effectively the core of the changing sidebars. My site is then structured into actual folders (I don't use URL rewriting or anything snazzy there) and the files in each folder reference the main header (using "../" to go up a folder), and the individual footer. It's that simple.

Here's a visual example of my folder structure:

root folder
	index.php
	header.php
	footer.php

about
	index.php
	footer.php

Each footer.php contains the sidebar structure code (e.g. <div id="sidebar">), and then the custom text for each section. So my main footer has blog archive links, the About footer has the About sub-section links, and so on. The index.php page for each section (and each extra file) end up looking a bit like this:

<?php include('../header.php'); ?>

some text here

<?php include('footer.php'); ?>

...and that's it. Saves having to create conditional statements and/or checking URLs. Of course, the only downside is having to update multiple footers if I change my coding about, but it's hardly a huge chore.

Comments (15)

  1. gravatar

    On Tue 8th Jan 2008 @ 20:18, Rachael said:

    Do I have to make a comment along the lines of "I can't believe you're talking about this like it's new sh*t" ?  :P Seriously, though, it's a useful technique for people who don't already know it. Yay for enlightenment! Is that a word?

  2. gravatar

    On Tue 8th Jan 2008 @ 20:21, Jem said:

    If you like Rachael  :P

  3. gravatar

    On Tue 8th Jan 2008 @ 20:27, Rachael said:

    Nah, I'm not that flip. Besides, everything is considered "new" to people who don't already know it.

  4. gravatar

    On Tue 8th Jan 2008 @ 22:01, Hev said:

    Hey Rachael, I didn't know how she did it, lol. I figured it was php includes, but wasn't sure. I just figured it was part of Jem's blog coding. I should have figured it would be something really simple. Thanks Jem.

  5. gravatar

    On Tue 8th Jan 2008 @ 22:13, Arwen said:

    Lawl. I do the same thing with my "dynamic" sidebars.

  6. gravatar

    On Tue 8th Jan 2008 @ 22:34, Annie said:

    I always wondered how you did that. Thanks  :)

  7. gravatar

    On Tue 8th Jan 2008 @ 23:19, Ruth said:

    I used to do something similar (when I had enough stuff on my site to bother, that is). Just curious - what's in your root folder footer.php?

  8. gravatar

    On Tue 8th Jan 2008 @ 23:23, Ruth said:

    Duh, scrub that, I see now.

  9. gravatar

    On Wed 9th Jan 2008 @ 11:41, Chris said:

    hehe! I kind fo fiugured it out... but I think it's good how your telling people to do it!  :)

  10. gravatar

    On Wed 9th Jan 2008 @ 11:52, Niki said:

    I figured it out before you posted this. I thought you used some kind of pwning PHP ninja script more advanced than includes. Lol. Happpy belated birthday, Jem! It was my dad's and friend's birthday yesterday, too.

  11. gravatar

    On Wed 9th Jan 2008 @ 13:06, Carly said:

    Yes, happy belated birthday... I just got my head around conditional statement type things in wordpress, all along there was a much simpler way of doing it! Woo...

  12. gravatar

    On Wed 9th Jan 2008 @ 15:45, Vera said:

    Hm... I remember once I read a review... on captious pedants I think, where they asked the person why the need to change the folders and not just have the images pertaining to it in a different folder. Aside that, I always test my coding offline, and like for it to be flexible so that it works with minimal changes online as well. If I have subfolders, I always have to use the absolute path... which I forget to change either offline or online... So mine is somewhat based on your tutorial for custom titles with PHP. I have a $section variable which has 4 different values, depending on where I am (me, you, site, home). I define this variable before I call the include functions. Next, I have 5 different footer files (I have the sidebar info in my footer) one for each section and a generic one. In the generic footer file, I have a switch statement, which checks the value of $category, and then includes the correct file. eg: switch($category) case "me": include(footer1); case "you": include(footer2); case "site": include(footer3); default: include(footer_def); endswitch ^ don't quote me on the syntax. I looked it up, but promptly forgot it after using True, the downside of this, is that I have to specify the $category in every single file. The upside is, that I don't have THAT much trouble with fixing links from the offline - online version, and the other way around.... ... um... did I make sense? =P

  13. gravatar

    On Wed 9th Jan 2008 @ 17:42, Sanne said:

    Interesting! Thanks for sharing this with us, I'm going to try to implement it with Wordpress.

  14. gravatar

    On Wed 9th Jan 2008 @ 20:25, Kristina said:

    You've just totally frazzled my already highly confused brain, Jem. I like the changing sidebar, thought. ^^; Hah, what a stupid comment compared to everybody else's intellectual commentism.

  15. gravatar

    On Mon 11th Feb 2008 @ 02:10, Sarah said:

    How would you do this using wordpress? Sorry for my ignorance.

Leave a Comment

No HTML (it will be stripped); first time comments are moderated; comments left without a legitimate first name or nickname will be deleted (no, your momma didn't call you "cheap quality wardrobes").








This entry was posted on Tue 8th Jan 2008 @ 20:11, and there are currently 15 comments. Leave your own?

Tags:

If you like this entry, you may also like:

« previous: BellaBuzz into WordPress


Enjoyed this entry?
Then why not subscribe to my blog.