About Club osCommerce

Showcasing osCommerce…the good, the bad and the ugly!

StumbleUpon It! DIGG It!

Change Header Background Colour Per Category

Written By Gary on May 02 2008 · Comments (3)

Buy Gary A Beer?
Buying me a "beer" helps me to keep my contributions updated and keep this blog alive - and you get a link from my homepage to your site. Cheers!


Interesting questions posed at the osCommerce Forum;

i want to make the header color change when the visitor selects a main category. each main category should have it's own color.

My reply;

Use a switch on $current_category_id with a stylesheet named the same number.

In depth;

  • We know that $current_category_id gives the ID of the category that is currrently active. We therefore are able to create lots of different stylesheets in order to make each category look different.
  • We know that index.php is the only file that controls Categories - so the code change needs to go in this file.
  • We know that CSS allows us to cascade the style - in other words, css is overridden by later css of the same name.

So, we are able to use a PHP function called "switch" to show a 2nd stylesheet! Like this:

PHP:
  1. <?php
  2. switch ($current_category_id) {
  3.    case 1:
  4.    echo '<link rel="stylesheet" type="text/css" href="red.css">';
  5.    break;
  6.    case 2:
  7.    echo '<link rel="stylesheet" type="text/css" href="purple.css">';
  8.    break;
  9.    case 3:
  10.    echo '<link rel="stylesheet" type="text/css" href="yellow.css">';
  11.    break;
  12. }
  13. ?>

This code would be added to index.php right underneath the existing line of code;

PHP:
  1. <link rel="stylesheet" type="text/css" href="stylesheet.css">

Contained in each .css file would be css code for background colours to OVER-RIDE what you have already defined in the usual stylesheet.css - I'm not going to go into css on this blog post, as I only want to give a brief overview of how I would enable a different theme per category - once I have made a "purple.css" this would then be automatically used for Category ID 2.

Hopefully this blog post will give you some ideas for your own site.

3 Comments

  1. Excellent work, Exactly what I have been looking for. Doe's anyone know if you can do this with the Sub-Categories as well?

    Comment by Nathan — April 2, 2009 @ 7:17 pm


  2. Hi. You can do this for any category or subcategory.

    Comment by Gary — April 20, 2009 @ 12:02 pm


  3. Great work. Thanks a lot. Joan from Barcelona (Spain)

    Comment by Joan — July 2, 2009 @ 5:28 pm


Leave a comment

RSS feed for comments on this post · TrackBack URL

Hot 100 osCommerce Shops

View the osCommerce HOT 100These are the best looking, most exceptional osCommerce Stores as voted for by you.

New to osCommerce - get inspiration from these beautiful shops. Reckon your site has what it takes to become a member of the HOT 100? Submit it!