Page 1 of 1

Generating a random button

Posted: Mon Apr 04, 2005 2:55 pm
by s.dot
Okay, I have this code that generates random values for 5 buttons.

Code: Select all

<?
$rand1 = rand(1,9);
$rand2 = rand(1,9);

$random = $rand1.$rand2;

$randb1 = rand(1,9);
$randb12 = rand(1,9);
$button1 = $randb1.$randb12;


$randb2 = rand(1,9);
$randb22 = rand(1,9);
$button2 = $randb2.$randb22;


$randb3 = rand(1,9);
$randb32 = rand(1,9);
$button3 = $randb3.$randb32;


$randb4 = rand(1,9);
$randb42 = rand(1,9);
$button4 = $randb4.$randb42; ?>

To continue click: <B><? echo $random; ?></B><BR><BR>
<input type="button" value="<? echo $random; ?>">
<input type="button" value="<? echo $button1; ?>">
<input type="button" value="<? echo $button2; ?>">
<input type="button" value="<? echo $button3; ?>">
<input type="button" value="<? echo $button4; ?>">
Now, my question is, how can I get those 5 buttons to display in a random order?

Posted: Mon Apr 04, 2005 3:13 pm
by feyd
add to an array.. call shuffle()

Posted: Mon Apr 04, 2005 3:34 pm
by s.dot

Code: Select all

&lt;?
$rand1 = rand(1,9);
$rand2 = rand(1,9);

$random = $rand1.$rand2;

$randb1 = rand(1,9);
$randb12 = rand(1,9);
$button1 = $randb1.$randb12;


$randb2 = rand(1,9);
$randb22 = rand(1,9);
$button2 = $randb2.$randb22;


$randb3 = rand(1,9);
$randb32 = rand(1,9);
$button3 = $randb3.$randb32;


$randb4 = rand(1,9);
$randb42 = rand(1,9);
$button4 = $randb4.$randb42; ?&gt;

To continue click: &lt;B&gt;&lt;? echo $random; ?&gt;&lt;/B&gt;&lt;BR&gt;&lt;BR&gt;
&lt;? $buttonsarray = array('$random','$button1','$button2','$button3','$button4');
shuffle($buttonsarray);
while (list(, $button) = each($buttonsarray)) {
   echo &quote;&lt;input type=\&quote;button\&quote; value=\&quote;$button\&quote;&gt;&quote;; } ?&gt;
This does randomize the buttons, but each value is $button1, $button2, $button3, etc.

Why aren't the values showing up instead of the variables?

Posted: Mon Apr 04, 2005 3:44 pm
by feyd
line 27: single quotes versus double quotes issue.

Posted: Tue Apr 05, 2005 12:20 am
by s.dot

Code: Select all

echo "<input type=\"button\" name=\"cheat\" value=\"$button\" onClick=\"this.form.submit();\"> ";
This is not posting the value to my script, do buttons post value?