SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict seanmayhew's Avatar
    Join Date
    Aug 2002
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using Class to create color scheme

    I'm trying to create a site with two class files a Category Index file and an article page for each category. I'd like to to set the color scheme for each category based on an include file
    PHP Code:
    function ArtScheme($SchemeSelected){ 
    switch($SchemeSelected) 

    case "home": 
    include ('homescheme.php'); 
    break; 
    default: echo "Your Color Scheme does not work"; 


    $SchemeNeeded = "home"; 
    ArtScheme($SchemeNeeded); 

    <table width="775" border="0" cellpadding="1" cellspacing="0" bgcolor="<?php $bordercolor?>">
    Here is what my include file looks like
    PHP Code:
    <?
    $highlightcolor 
    "#EBD7AB";
    $bordercolor "#CC9933";
    $stripecolor "#FFFFCE";
    ?>
    All this produces is a bunch of empty quotes bgcolor=""

    Any help would be greatly appreciated

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are missing the = operand from the short tags. In other words, this:

    bgcolor="<? $bordercolor ?>"

    should read:

    bgcolor="<?= $bordercolor ?>"

  3. #3
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I think when you include the file inside of a function the stuff in the included file, including the variables are restricted to the scope of the function that they were included in.

    Does that make sense? The variables are only accessible from within the ArtScheme function. You can change that by using the GLOBAL keyword though. Or by using the $GLOBALS array.


    If I were you I'd probably have the ArtScheme function return an
    array with the stuff in it you need.


    Something like:

    PHP Code:
    <?php


    function ArtScheme($SchemeSelected){ 
    switch(
    $SchemeSelected

    case 
    "home"
    include (
    'homescheme.php'); 
    break; 
    default: echo 
    "Your Color Scheme does not work"


    $colorArr = array('borderColor'=> $bordercolor
                      
    'highlightColor' => $highlightcolor );

    return 
    $colorArr;


    $SchemeNeeded "home"
    $colorVarsArr ArtScheme($SchemeNeeded); 

    ?>

    <table width="775" border="0" cellpadding="1" cellspacing="0" bgcolor="<?php echo $colorVarsArr['borderColor'];?>">

    --ed

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or produce a number of css style sheet files and have PHP load up the one required.

  5. #5
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your function isn't returning anything. This would be a quick fix:
    PHP Code:
    <?php 
    function ArtScheme($SchemeSelected){ 
        switch(
    $SchemeSelected
            { 
            case 
    "home"
            include (
    'homescheme.php'); 
            break; 
            default: echo 
    "Your Color Scheme does not work";
            }
        return 
    $color;
        }
    ?>
    $SchemeNeeded = "home"; 
    $color = ArtScheme($SchemeNeeded); 

    <table width="775" border="0" cellpadding="1" cellspacing="0" bgcolor="<?php echo $color['border']?>"> 

    <?php
    // this in the include file
    $colorscheme['home']['highlight'] = "#EBD7AB";
    $color['border'] = "#CC9933";
    $color['stripe'] = "#FFFFCE";
    ?>
    And this is another, more compact, way:
    PHP Code:
    <?php
    include('colorschemes.inc.php');
    $SchemeNeeded "home"
    $color $colorscheme[$SchemeNeeded];
    ?>
    <table width="775" border="0" cellpadding="1" cellspacing="0" bgcolor="<?php echo $color['border']?>"> 

    <?php
    // the below goes in the include file colorschemes.inc.php
    $colorscheme = array(
        
    'home' => array('highlight' => '#EBD7AB''border' => '#CC9933''stripe' => '#FFFFCE'),
        
    'away' => array('highlight' => '#FFFF00''border' => '#FF0000''stripe' => '#CCCCCC')
        );
    ?>
    No need for functions, just use the power of the array!

    Well, and if I can be a pain, why not use CSS and load a different stylesheet?

    Have fun with it!

    Rik
    Last edited by Zoef; May 14, 2003 at 15:15.
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  6. #6
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn! Late again

    Something else... what about a default style?

    Here's a modification to the above example:
    PHP Code:
    <?php
    include('colorschemes.inc.php'
    $SchemeNeeded "home";
    if 
    is_array($colorscheme[$SchemeNeeded]))
        {
    $color $colorscheme[$SchemeNeeded];}
    else
        {
    $color $defaultcolors;}
    ?>
    <table width="775" border="0" cellpadding="1" cellspacing="0" bgcolor="<?php echo $color['border']?>"> 

    <?php
    // the below goes in the include file colorschemes.inc.php
    $colorscheme = array(
        
    'home' => array('highlight' => '#EBD7AB''border' => '#CC9933''stripe' => '#FFFFCE'),
        
    'away' => array('highlight' => '#FFFF00''border' => '#FF0000''stripe' => '#CCCCCC')
        );
    $defaultcolors = array('highlight' => '#EEEEEE''border' => '#000000''stripe' => '#CCCCCC')
    ?>
    But torrent is right, best way to do this is with CSS.

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •