Yoeran
Yoeran

Reputation: 23

Change background of htmltable built of array variables

I'm trying to get a rand(10-69) numbers crossed of on the Bingo Card, changing the backgroundcolor to green(#008000).

Some of the requirements of the assignement:

• The bingo card displays 36 different numbers random determined.
• On the first row only numbers >= 10 and < 20, on the second only >= 20 and <30 etc.
• Make a function to fill in the bingo card and make it return the filled in bingo card.
• Make a function to display the bingo card.
• No globals.

I tried differend approaches on how to make the 2 functions work and display the Bingo card. ATM it's working (although i'm still asking myself if it's the right approach.)

I searched this site and other but could not get the code to work for me.

A direction is appreciated.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1252">
<title>Insert title here</title>
</head>
<body>
    <?php
    function BingoNumbers() {
        $numbers = range(10, 69);
        $min = 0;
        $max = 9;
        for ($i = 0; $i < 6; $i++) {
            $row_range = range($numbers[$min], $numbers[$max]);
            $min += 10;
            $max += 10;
            for ($x = 0; $x < 6; $x++) {
                $number = $row_range[array_rand($row_range)];
                if (in_array($number, $row_range)) {
                    unset($row_range[array_search($number, $row_range)]);
                }$card_numbers[] = $number;
            }
        }return $card_numbers;
    }

    function BingoCard() {
        $card_numbers = BingoNumbers();
        echo "<table border='1'>", "<tr>";
        $count = 0;
        foreach ($card_numbers as $key=>$value) {
            echo "<td align='center'>".$value."</td>";
            $count++;
            if ($count==6) {
                echo "</tr>";
                $count = 0;
            }
        }echo "</table>"; return $card_numbers;
    }

    $bingocard = BingoCard();
    $chosen_number = rand(10,69);
    $drawn_number = array();
    if (in_array($chosen_number, $bingocard)) {
        echo "<BR>",$chosen_number," On it";
        echo '<table><td style="background-color:#008000">',$bingocard[array_search($chosen_number, $bingocard)],'</td></table>';
    } else {
        echo "<BR>",$chosen_number," Not on it";
    }
    ?>
</body>
</html>

The above code does show the requested result in a table using the green background. The question is how do I display the background in the table created by the function?

All help is greatly appreciated.

Upvotes: 2

Views: 31

Answers (1)

Ingus
Ingus

Reputation: 1044

Try this :)

function BingoNumbers() {
    $numbers = range(10, 69);
    $min = 0;
    $max = 9;
    for ($i = 0; $i < 6; $i++) {
        $row_range = range($numbers[$min], $numbers[$max]);
        $min += 10;
        $max += 10;
        for ($x = 0; $x < 6; $x++) {
            $number = $row_range[array_rand($row_range)];
            if (in_array($number, $row_range)) {
                unset($row_range[array_search($number, $row_range)]);
            }$card_numbers[] = $number;
        }
    }return $card_numbers;
}

function BingoCard($chosen_number) {
    $card_numbers = BingoNumbers();
    echo "<table border='1'>", "<tr>";
    $count = 0;
    foreach ($card_numbers as $key=>$value) {
        echo "<td align='center'";  

            if($chosen_number==$value){
                echo ' style="background-color:#008000"';
            }

        echo ">".$value."</td>";

        $count++;
        if ($count==6) {
            echo "</tr>";
            $count = 0;
        }
    }echo "</table>"; return $card_numbers;
}


$chosen_number = rand(10,69);
$bingocard = BingoCard($chosen_number);
$drawn_number = array();
if (in_array($chosen_number, $bingocard)) {
    echo "<BR>",$chosen_number," On it";
    echo '<table><td style="background-color:#008000">',$bingocard[array_search($chosen_number, $bingocard)],'</td></table>';
} else {
    echo "<BR>",$chosen_number," Not on it";
}

What i did is passed your random number inside BingoCard() and if that matched in loop echo color!

Upvotes: 1

Related Questions