2

well what it is is I have a task in php where I need to pick a card at random, then a button named "draw" which grabs another random card.

            <td><b>Rectangle 10</b></br>

        <p>Your card is: </p>

        <?php
        $cards = array("AC","AD","AH","AS","1C","1D","1H","1S",
        "2C","2D","2H","2S","3C","3D","3H","3S","4C","4D","4H","4S",
        "5C","5D","5H","5S","6C","6D","6H","6S","7C","7D","7H","7S",
        "8C","8D","8H","8S","9C","9D","9H","9S","10C","10D","10H","10S",
        "JC","JD","JH","JS","QC","QD","QH","QS","KC","KD","KH","KS"
        );
        $card = array_rand($cards, '1');
        print_r($cards[$card]);
        ?>

        <form>
        <input type="button" value="Draw" onClick="window.location.reload()">
        </form>

        </td>

The code works on it's own, but in the whole page it doesn't, I'll paste the full page now.

<?php
$today = date_default_timezone_set('Europe/London');
$a = 3034;
srand($a);
?>

 <table border='1'>
    <tr>
        <td><b>Rectangle 1</b></br>Rob Dorsett </br> 12007071 </br><?php  echo $today = date('d/m/y')?></td>
        <td><b>Rectangle 2</b></br><?php echo $addition = (3+4+5) ?></td>
    </tr>

    <tr>
        <td><b>Rectangle 3</b></br> <?php srand(3034);
        echo (rand()); ?> </td>

        <td><b>Rectangle 4</b></br>
        <?php for ($i=30;$i<=40;$i++){
            if($i%2 == 0) {
            echo $i. '</br>';}
            }
        ?>
        </td>
    </tr>


    <tr>
        <td><b>Rectangle 5</b></br> 

        <form>
        </br>
        Enter four digits: </br>
        <input name="input" />
        </br><input type="submit" value="Sumbit" /> 
        </form>
        <?php
        $x = $_GET['input'];
        ?>
        </td>

        <td><b>Rectangle 6</b></br>

        <?php
        if ($x%2 == 0){
        echo 'Your number is even';
        }
        else 
        {
        echo 'Your number is odd';
        }
        ?>
        </td>

    </tr>

        <td><b>Rectangle 7</b></br>
            <form>
            Enter a seed for a random number: </br>
            <input type="text" name="random" /> </br>
            <input type="submit" value="Generate" /> </br>
            <?php
            $r7 = $_GET['random'];
            srand($r7);
            echo rand();                
            ?>
            </form>


        </td>
        <td><b>Rectangle 8</b></br>
        <?php
        echo str_rot13('Example Rot13');
        ?>
        </br>
        <i>Example Rot13</i>

        </td>
    <tr>
        <td><b>Rectangle 9</b></br>

        <script type="text/javascript">
function rot13 (txt) {
 var map = []
 var tmp = "abcdefghijklmnopqrstuvwxyz"
 var buf = ""

for (j = 0; j < tmp.length; j++) {
    var x = tmp.charAt(j); var y = tmp.charAt((j + 13) % 26)
    map[x] = y; map[x.toUpperCase()] = y.toUpperCase()
  }

for (j = 0; j < txt.length; j++) {
    var c = txt.charAt(j)
    buf += (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' ? map[c] : c)
}

return buf
}
        </script>

        <script>var foo = rot13 ('Example Rot13')</script>
        <p>Encoded: <script>document.write(foo)</script>
        <p>Decoded: <script>document.write(rot13(foo))</script>

        </td>


        <td><b>Rectangle 10</b></br>

        <p>Your card is: </p>

        <?php
        $cards = array("AC","AD","AH","AS","1C","1D","1H","1S",
        "2C","2D","2H","2S","3C","3D","3H","3S","4C","4D","4H","4S",
        "5C","5D","5H","5S","6C","6D","6H","6S","7C","7D","7H","7S",
        "8C","8D","8H","8S","9C","9D","9H","9S","10C","10D","10H","10S",
        "JC","JD","JH","JS","QC","QD","QH","QS","KC","KD","KH","KS"
        );
        $card = array_rand($cards, '1');
        print_r($cards[$card]);
        ?>

        <form>
        <input type="button" value="Draw" onClick="window.location.reload()">
        </form>

        </td>   
    </tr>
</table>    
5
  • 2
    What is the question? Commented Nov 25, 2013 at 23:24
  • 1
    This php works fine for me, returning a random card each time. Commented Nov 25, 2013 at 23:24
  • As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically. Commented Nov 25, 2013 at 23:46
  • What version of PHP are you running? Commented Nov 25, 2013 at 23:48
  • yeah, but it was a part of the task to use a seeded random number. Commented Nov 26, 2013 at 0:19

2 Answers 2

2

You are feeding the srand() function with a static value, so you will always get the same result. Also, look at my last comment.

Sign up to request clarification or add additional context in comments.

Comments

2

Removeing all srand(); makes your code work.

You should make some functions to get the random numbers and separate your PHP from your html/js. It would help the readabillity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.