Results 1 to 6 of 6

Thread: Color Array

  1. #1
    Join Date
    Mar 2012
    Posts
    4

    Default Color Array

    That rather than having 6 colors chosen randomly in the code below:

    <?php

    function cssarray() {

    // Create a new array
    $color[1] = "blue";
    $color[2] = "purple";
    $color[3] = "green";
    $color[4] = "grey";
    $color[5] = "lightgreen";
    $color[6] = "pink";

    // total number of css files
    $total = "6";
    $start = "1";
    $num = mt_rand($start, $total);
    $colorfile = $color[$num];
    echo "$color[$num]";
    }
    ?>

    Is there a way I could just have the color come out numerically rather than randomly as shown in the code above?

    Thanks!

  2. #2

    Default

    I guess you want the html color code in hex like #000000. If so, when you define it, define in the format you wish. Like
    $color[1] = "#0000ff";

    simple?
    Design is not just what it looks like and feels like. Design is how it works.
    Hostmonster? Get a Hostmonster Coupon or read Hostmonster Reviews

  3. #3
    Join Date
    Mar 2012
    Posts
    4

    Default

    Ok, supposedly I comment out or delete the following code as to that I do not want the color chosen randomly but rather than numerically:

    $num = mt_rand($start, $total);

    How will I then do the following?

    $total = "6";
    $start = "1";
    $colorfile = $color[$num];
    echo "$color[$num]";

    What should be placed in the above codes in place of "$num"?

    Please advise. I'd appreciate it. Thanks in advance!

  4. #4

    Default

    You could:

    <?php
    // Create a new array
    $color[1] = "blue";
    $color[2] = "purple";
    $color[3] = "green";
    $color[4] = "grey";
    $color[5] = "lightgreen";
    $color[6] = "pink";

    // total number of css files
    $total = "6";
    $start = "1";

    while($start <= $total)
    {
    $colorfile = $color[$start];
    echo $colorfile;
    $start ++;
    }
    ?>

    That will just print the color names across the screen in numeric order of the array. I hope that is what you wanted.
    Matt

    My life can be summed up in one word, "indescribable".

  5. #5
    Join Date
    Mar 2012
    Posts
    4

    Default

    Quote Originally Posted by djmatt View Post
    You could:

    <?php
    // Create a new array
    $color[1] = "blue";
    $color[2] = "purple";
    $color[3] = "green";
    $color[4] = "grey";
    $color[5] = "lightgreen";
    $color[6] = "pink";

    // total number of css files
    $total = "6";
    $start = "1";

    while($start <= $total)
    {
    $colorfile = $color[$start];
    echo $colorfile;
    $start ++;
    }
    ?>

    That will just print the color names across the screen in numeric order of the array. I hope that is what you wanted.
    Hmm, we may be getting there.

    What I really want is to just choose and print one color at a time. Such that each time the array gets called upon, it'll choose just one color. For instance, the 1st time the array gets called upon it'll print $color[1] = "blue";, then the 2nd time it gets called upon it'll print $color[2] = "purple";, up to its 6th color, then starting again and going back to its first color and all.

    In another word, if you go back to the original codes in the 1st post, you'll see that it does choose and print just one color at a time each time the array gets called upon. The only difference is that it was chosen randomly, while I'd like to have it chosen numerically?

    Is it possible?

    P.S. Thank you all for your replies thus far. I appreciate it!

  6. #6

    Default

    If you used sessions you could add 1 to the session variable each time it loads a page, evaluate if it is over 6 {reset to 1} and then call the color.

    I don't know what you are trying to do with this code, but it is recommended that your website have a consistent, reliable look. It will help people know they are on your site and remember it better if they can say, "it's the web site with the green background." If a page is red one time, a visitor may want to go back to that page and try to look for a red page. Just for you to consider.
    Matt

    My life can be summed up in one word, "indescribable".

Posting Permissions

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