vendredi 4 novembre 2016

Use an array string that is chosen randomly to determine the value of two other html table cells on output

The title is the easiest way I can explain it. This is for classwork, but I have been at it for hours and need some help. Basically, I have a string of the 12 Chinese Zodiac signs. On page load (or refresh), one of the signs is randomly selected and displayed as text in a cell of a 3-column, one row table. The next cell is supposed to be a matching image and then the final cell is supposed to be the 2016 Chinese Zodiac predictions, as text.

The problem is that I need to match the image and block of text with the sign that gets randomly generated. Here is the code I have now... It is randomly choosing both the Text for the first cell and the image for the second cell, which of course, are rarely ever the same...

I've tried running it calling on the current $ran and it displays the switch default message.

Running it with $pic = $ran returns the image of the monkey every time, which is the first case choice

You can see it live on my site. taskdollar.com / misc-code.php

Any help would be greatly appreciated

`

<?php
    function array_random($arr, $num = 1) {
        shuffle($arr);

        $r = array();
        for ($i = 0; $i < $num; $i++) {
             $r[] = $arr[$i];
    }
    return $num == 1 ? $r[0] : $r;
}
?>
<center>
<table width="80%" border="2px solid green" cellpadding="1px">
<th width="15%"><h1>Chinese Zodiac Sign</h1></th>
<th width="15%"><h1>Chinese Zodiac Sign Image</h1></th>
<th width="70%"><h1>Chinese Zodiac 2016 Predictions</h1></th>
<tr>
<td style="text-align: center; font-size: 36px"><h1><?php
$a =  array("Monkey","Rooster","Dog","Pig","Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Sheep");

    $ran = print_r(array_random($a, TRUE)); ?></h1></td>

<td><center><?php $pic = array_random($a, TRUE);
        switch($pic) {
            case "Monkey":
                echo "<img src='monkey.gif'>";
                break;
            case "Sheep":
                echo "<img src='sheep.gif'>";
                break;
            case "Dragon":
                echo "<img src='dragon.gif'>";
                break;
            case "Ox":
                echo "<img src='ox.gif'>";
                break;
            case "Pig":
                echo "<img src='pig.gif'>";
                break;
            case "Rabbit":
                echo "<img src='rabbit.gif'>";
                break;
            case "Rat":
                echo "<img src='rat.gif'>";
                break;
            case "Rooster":
                echo "<img src='rooster.gif'>";
                break;
            case "Snake":
                echo "<img src='snake.gif'>";
                break;
            case "Dog":
                echo "<img src='dog.gif'>";
                break;
            case "Horse":
                echo "<img src='horse.gif'>";
                break;
            case "Tiger":
                echo "<img src='tiger.gif'>";
                break;
            default:
                echo "Failed to load. Please refresh your browser.";
                break;  
            }   ?></center></td>
 <td>Case Statements will go here that will choose a block of text to match the sign...</td>
 </tr>
 </table></center>

`




Aucun commentaire:

Enregistrer un commentaire