Php and random

Discussion in 'PHP' started by Dirty-Rockstar, Feb 17, 2007.

  1. #1
    Below is something simple i created where everytime a page reloads a box will either be black or white. what i want to create is for this to automaticly happen without a refresh. for it to go black-white-black-white etc. reading up on php its telling me that the code is executed before the page loads. so i dont think its possible. Is it possible to do what im trying to accomplish?

    thanks :)

    <?
    $black="bgcolor='black'";
    $white="bgcolor='white'";
    $name=1;
    $name2=2;
    $random=rand($name,$name2);
    
    if($random==1)
    {
    
    Print"
    
    <TABLE BORDER=1>
    <TR>
      <TD $black width='25' height='25' value='value' name='name'></TD>
      
    </TR>
    
    </TABLE>";
    
    }else{
    
    Print"
    
    <TABLE BORDER=1>
    <TR>
      <TD $white width='25' height='25' value='value' name='name'></TD>
      
    
    </TABLE>";
    
    }
    ?>
    PHP:

     
    Dirty-Rockstar, Feb 17, 2007 IP
  2. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    Y your are doing it with PHP then? Create a random number in JS, and use function setTimeOut, Following is hint

    
    <script>
    function changeColor()
    {
    var num = sometechniqueheretogeneraterandomnumber;
    if(num == 1)
        var color = "ffffff";
    else
        var color = "000000";
    
    document.getElementById("mytd").style.background = color;
    }
    
    setTimeout("changeColor()", 5000);
    </script>
    
    <TABLE BORDER=1>
    <TR>
      <TD id="mytd" width='25' height='25' value='value' name='name'></TD>
      
    </TR>
    
    </TABLE>
    
    Code (markup):
    I have not tested it, but it shud work.
     
    designcode, Feb 17, 2007 IP
  3. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i dont know javascript but it doesnt look too difficult. thanks :)
     
    Dirty-Rockstar, Feb 17, 2007 IP
  4. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #4
    Please add to my rep, if that post helped you :)
     
    designcode, Feb 17, 2007 IP
  5. Clive

    Clive Web Developer

    Messages:
    4,507
    Likes Received:
    297
    Best Answers:
    0
    Trophy Points:
    250
    #5
    Here is a more advanced, yet easy to use random string generator:
    function randChar($x, $r) {
    	$rand ='';
    	for ($a = 0; $a < $x; $a++) {
    		$b = rand(0, strlen($r) - 1);
    		$rand .= $r[$b];
    	}
    	return $rand;
    }
    PHP:
    The following allows specifying which characters should be used to generate the string
    echo randChar(5, '1234567890');
    PHP:
    "5" represents the number of chars that the string should contain.
    "1234567890" is the character selection for the string to be generated from.
     
    Clive, Feb 17, 2007 IP
  6. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    pm me how and its done
     
    Dirty-Rockstar, Feb 17, 2007 IP
  7. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #7
    I think you have done it :D
     
    designcode, Feb 18, 2007 IP