开发者

stuck here with lotto grid

I have a grid with 42 nrs wher开发者_Go百科e I will use the rand() function to pick out numbers from the grid and and mark them

so far I came up with

    <?php
    $row="";
    print ("<table border=\"1\">");
    for ($i=0; $i<6; $i++)
        {
        print ("<tr>");
            for ($j=0; $j<7; $j++)
                {
                $random = rand(1,42);
                $row += "(string)$random";
                $som = $som + 1;
                print("<th>".$som);
                }

        ("</tr>");
        }

    print ("</table>");
    print ("$rij");

 // here I'm just testing to see if I can get a list of random numbers 
 for ($i=0; $i<6; $i++){
        $randomNr = rand(1,42);
        echo "$randomNr<br/>";
        }


    ?>

I guess the idea is to match the numbers out of the rand function to the indexes of the table. But i'm really stuck here at getting the table to convert to an arra so I can match the index with the random numbers.


You're probably not too far off with your own attempt. You would just need to generate 6 random unique numbers and compare against them. Easiest way to do that is to generate an array using range() and pick the random numbers with array_rand() (which actually returns indexes, so you need a bit of additional code to get the values). Then you just need to find whether the currently outputted number is in the chosen number array using in_array()

Here's an example function of the general case that expands Sondre's example a bit. The function in the example takes following arguments: Total random numbers picked, Smallest number in the grid, Biggest number in the grid and the numbers per row in the grid. The function returns the generated HTML table source a string.

<?php

function generateHighlightedLotteryTable ($count = 6, $min = 1, $max = 42, $perRow = 7)
{
    // Generate the picked numbers (actually we just get their indexes)
    $nums = array_rand(range($min, $max), $count);

    $output = "<table>\n";

    for ($n = $min; $n <= $max; $n++)
    {
        // get "index" of the number, i.e. $min is the first number and thus 0
        $i = $n - $min;

        if ($i % $perRow == 0)
        {
            $output .= "<tr>";
        }

        // If the current number is picked
        if (in_array($i, $nums))
        {
            $output .= "<td><strong>$n</strong></td>";
        }
        // If the current number hasn't been chosen
        else
        {
            $output .= "<td>$n</td>";
        }

        if ($i % $perRow == $perRow - 1)
        {
            $output .= "</tr>\n";
        }
    }

    // End row, if the numbers don't divide evenly among rows
    if (($n - $min) % $perRow != 0)
    {
        $output .= "</tr>\n";
    }

    $output .= "</table>";

    return $output;
}

echo generateHighlightedLotteryTable();

?>

I hope this is what you were trying to achieve.


This would create a grid of 42 numbers and mark out a random one. If you want to mark out more create and array and check against that insted of just the rand variable. In you're original code there you were actually running the rand-function 42 times which I guess is unintended.

EDIT: Or did you need the grid to be filled with random numbers?

$rand = rand(1, 42);

echo "<table>";
for($i = 1;$i <= 42; $i++) {
    if($i%7 == 1) {
        echo "<tr>";
    }
    $print = $rand == $i ? "<strong>" . $i . "</strong>" : $i;
    echo "<td>" . $print . "</td>";
    if($i%7 == 0) {
        echo "</tr>";
    }
}
echo "</table>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜