Jan 08 2008

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.

Warning

This post is over 6 months old. This means that, despite my best intentions, it may no longer be accurate. Age, motherhood, experience, loss... these things have all changed me from when this blog was started back in the heady (ha) days of my youth.

As much as I would like to go back and edit 10 years of archives to provide an insight into the 'me' of now — to update coding snippets and revise website advice — it would probably take years to do so (by which point I'd have to start again!) This would defeat the point of keeping these archives anyway.

Please take these posts for what they are: a brief look into my past, my history, my journey.

15 Responses so far
  1. Rachael says: January 8, 2008 at 8:18 pm # ·

    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. Jem says: January 8, 2008 at 8:21 pm # ·

    If you like Rachael :P

  3. Rachael says: January 8, 2008 at 8:27 pm # ·

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

  4. Hev says: January 8, 2008 at 10:01 pm # ·

    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. Arwen says: January 8, 2008 at 10:13 pm # ·

    Lawl. I do the same thing with my “dynamic” sidebars.

  6. Annie says: January 8, 2008 at 10:34 pm # ·

    I always wondered how you did that. Thanks :)

  7. Ruth says: January 8, 2008 at 11:19 pm # ·

    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. Ruth says: January 8, 2008 at 11:23 pm # ·

    Duh, scrub that, I see now.

  9. Chris says: January 9, 2008 at 11:41 am # ·

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

  10. Niki says: January 9, 2008 at 11:52 am # ·

    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. Carly says: January 9, 2008 at 1:06 pm # ·

    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. Vera says: January 9, 2008 at 3:45 pm # ·

    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. Sanne says: January 9, 2008 at 5:42 pm # ·

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

  14. Kristina says: January 9, 2008 at 8:25 pm # ·

    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. Sarah says: February 11, 2008 at 2:10 am # ·

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