Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts

    Random Background Color

    Hello...

    I am working on a php script that will randomly set the background color from a set of colors I have chosen. Below is what I have so far, but it doesn't work properly.

    I want the script to pick one of the 3 colors, but it picks a range of numbers between 000000 and 444444. The second number is ignored. Also I would like the script to work with hex numbers (ff0000). I don't think the RAND function works with hex numbers.


    PHP Code:
    <?php

        $background 
    = array ('000000''999999''444444');
        
    $number rand($background[0], $background[2] ); 
        
        echo 
    "<body bgcolor=\"#";
        echo 
    "$number\">";
    ?>

    Thanks for any help.

    Sincerely

    Leonard Whistler
    www.stubby.ca
    Leonard Whistler

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    rand returns a number between the 2 values supplied , so if you say rand(10,1000) you get an integer between 10 & 1000.... you are almost there , but you actually want a number between (with 3 array elements) 0 & 2 !

    try this

    PHP Code:
    <?
    $background 
    = array ('000000''999999''444444'); 
    $number rand, (count($background)-1) ); 
    echo 
    '#'.$background[$number];
    ?>
    I used 0,(count()-1) so you can add as many colors to the array as you want and it will still work
    Last edited by firepages; 06-11-2004 at 05:14 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Thanks firepages....

    ...It's late now but in the morning I will be checking out your code.

    Leonard Whistler
    www.stubby.ca
    Leonard Whistler

  • #4
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Create your own function to return a random hex code:
    PHP Code:
    function rand_hex()
    {
      for(
    $i=0;$i<10;$i++)
        
    $chars[]=$i;    // first 10 digits
      
    $chars[]='a'$chars[]='b'$chars[]='c';    // chars a-c
      
    $chars[]='d'$chars[]='e'$chars[]='f';    // chars d-f
      
    $result="#";
      for(
    $i=0;$i<6;$i++)
        
    $result.=$chars[rand(015)];
      return 
    $result;

    Then
    Code:
    <body bgcolor="<?=rand_hex()?>">
    Last edited by trib4lmaniac; 06-24-2004 at 10:00 PM.

  • #5
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops, I didn't read your post properly Never mind I can stil contribute
    Quote Originally Posted by firepages
    PHP Code:
    <?
    $background 
    = array ('000000''999999''444444'); 
    $number rand, (count($background)-1) ); 
    echo 
    '#'.$background[$number];
    ?>
    could become....
    PHP Code:
    $bgcolors=array('#000000''#999999''#444444');
    echo 
    $bgcolors[array_rand($bgcolors)]; 
    Last edited by trib4lmaniac; 06-25-2004 at 10:00 PM.


  •  

    Posting Permissions

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