Hello everyone,

I'm stuck with coloring the value in a row. For now I am testing with one number to see if I can give the row with the value a color, but for now it keeps creating a new row wich will have the background-color I want. The code starts on row 42. The rest of the scripts works.

    <html>
    <body>
    <?php
    mt_srand((double)microtime()*1000000);
    //Een bijzondere aanpak voor het vullen
    function Vullen()
    {
        $getallen = array(0,1,2,3,4,5,6,7,8,9);
        shuffle($getallen);

        return $getallen;
    }
    $getallen = Vullen(); 
    //De functie display is om de getallen aan de rijnummers te koppelen: dus 1 + 0 t/m 9 voor rij 1 en 2 + 0 t/m 9 voor rij twee etc

    function display($getallen){
        $table = '<table  border="1px" cellpadding="1px" >';

        $bingokaart = array();
        //hoeveel rijen
        for ($row = 1; $row < 7; ++$row){

       $columnCount=0;
            $bingokaart[$row] = array();
            $table .= '<tr>';

            //Hier print je de getallen en de rijen 
            foreach ($getallen as $number){

                    if($columnCount < 6){
                    $bingokaart[$row] = $row .$number;  
                $table .= '<td>'.$bingokaart[$row]  . '</td>';
                        }
                        /*}*/
                        $columnCount++;
       //Einde van de bingokaart

       //Hier definieer ik de getallen die getrokken worden per rij en column

            //for testing I use just one number      
                        $backgroundColor = 'green';
                        $getrokkengetal = 15;
                  //here I use OR just to test if the color of the field will change      
             if (($bingokaart[$row]==$getrokkengetal) || ($columnCount  == $getrokkengetal)) {  

                //Here it makes a new row with the background color - But it must color the cell with the nummber on row 1
                //If i don't use the div it will echo (for example) 1 . background-color:#ff0000
                echo "<div style ='background-color:#ff0000'> $row </div>";

             }

            }

            $table .= '</tr>';

        }

        $table .='</table>';

        echo $table;

    }

    display($getallen);

    ?>
    </body>
    </html>

Thanks for the help!
Rem

$a = color 1;  $b = color 2 ; $c = color 1;
while ( xxx) {
echo $a;
$a = $b ;  $c = $a;  $b = $c; 
// with it being an odd number 3,  each loop would be a different color :)
}

thanks Charles_21,

I tried the while loop adjusted for my script, but without the result I wished for.

  $color1 = '#ff0000'; $color2 = 'red'; $color3 = 'orange';
                        while (( $row == $getrokkengetal) && ($columnCount == $getrokkengetal)) {
                            echo $color1;

                        }

I think the problem is in the setup of the table. So I made a change like this:

 $color1 =  'green';
                        if ( $bingokaart[$row] == $getrokkengetal)  {
                            $bingokaart[$row] = $table.='<tr style="background-color:green">';

                        }

Now it makes almost the row green, without number 15. So this should be the otherway around. The number 15 should be green and the other numbers of row 1 shouldn't have a color.

I will keep on working on it, but help is appreciated.

The goal is to make an array with numbers and if row and columncount match you will have an vertical green line with Bingo.

thanks everyone for feedback and help

R.

Oke, I made an error with <tr>. See post above.

This is now the code:

   $color1 =  'green';
                        if ( $bingokaart[$row] == $getrokkengetal)  {
                            $bingokaart[$row] . $table.='<td style="background-color:green">';

                        }

Only now it creates a new <td> with the green background, next to the td in wich number 15 is nested.

Hope somebody sees the error.

Thanks and happy Christmas everyone!

R.

Member Avatar for diafol
$table .= ($bingokaart[$row] == $getrokkengetal) ? '<td class="colored">' : '<td>';
$table .= $bingokaart[$row] . '</td>';

Best to apply a css rule to the class as .colored than to hard code it into the markup / php.

.colored{
    background-color: green;
}

thanks Diafol,

Almost there, but there is a strange thing:
Now if I run the scripts and it finds the number 15 , it will print an extra number 15 (<td>) with the background in the right color, next to the number 15 without an background color. This results in an extra column ($columnCount = 6 > will become $columncount =7).

The problem is with getallen = array(0,1,2,3,4,5,6,7,8,9);' on row 15 This gives me 6 rows and 10 columns with numbers. That's why I used $columnCount to reduce this to 6 columns. But is there a way to have really 6 columns from thegetallen = array(0,1,2,3,4,5,6,7,8,9);' ?
with still row 1 from 10 till 19 at random and row 2 from 20 till 29?
I think that will finish the script.

I will look also at an solution, but the last 2 hours I didn't find any.

Thanks for the support

R.

Member Avatar for diafol

SHow your new code

Hi Diafol,

I went back and used your suggestion, with the same result. Here is the complete code.
The problem is, when it finds the number is prints the found number extra with the background. See screen2.png.
Your solutions starts on line 57.

    <html>

    <style>
        .colored{
        background-color: green;
    }
    </style>

    <body>
    <?php
    mt_srand((double)microtime()*1000000);
    //Een bijzondere aanpak voor het vullen
    function Vullen()
    {
        $getallen = array(0,1,2,3,4,5,6,7,8,9);
        shuffle($getallen);

        return $getallen;
    }
    $getallen = Vullen(); 
    //De functie display is om de getallen aan de rijnummers te koppelen: dus 1 + 0 t/m 9 voor rij 1 en 2 + 0 t/m 9 voor rij twee etc

    function display($getallen){
        $table = '<table  border="1px" cellpadding="1px" >';

        $bingokaart = array();
        //hoeveel rijen
        for ($row = 1; $row < 7; ++$row){

       $columnCount=0;
            $bingokaart[$row] = array();
            $table .= '<tr>';

            //Hier print je de getallen en de rijen 
            foreach ($getallen as $number){

                    if($columnCount < 6){
                    $bingokaart[$row] = $row .$number;  
                $table .= '<td>'.$bingokaart[$row]  . '</td>';
                        }
                        /*}*/
                        $columnCount++;
       //Einde van de bingokaart

       //Hier definieer ik de getallen die getrokken worden per rij en column

            //for testing I use just one number      

                        $getrokkengetal = 25;
                  //here I the solution of Diafol and look if in the turn we find the number and adjust the
                  // background-color. When the number is not found just display the table.

                  // now if it find the number it makes an extra td with the number
                       if (( $bingokaart[$row] == $getrokkengetal) || ($columnCount == $getrokkengetal)) {

                       $table .= ($bingokaart[$row] == $getrokkengetal) ? '<td class="colored">' : '<td>';
                        $table .= $bingokaart[$row] . '</td>';

            }

            }

            $table .= '</tr>';

        }

        $table .='</table>';

        echo $table;

    }

    display($getallen);

    ?>
    </body>
    </html>

Thanks for the help and feedback R.

Member Avatar for diafol

Get rid of this conditional

  if (( $bingokaart[$row] == $getrokkengetal) || ($columnCount == $getrokkengetal)) {

You.re checking twice!

Hi Diafol,
That was one of my first attempts to get rid off the double check, but the result is screen 3. It will still print a extra number 25,but also a lot more numbers, then expected and wished.

The scripts looks like you suggested. Do you have any suggestion?

   $getrokkengetal = 25;
                  //here I the solution of Diafol and look if in the turn we find the number and adjust the
                  // background-color. When the number is not found just display the table.
                  $table .= ($bingokaart[$row] == $getrokkengetal) ? '<td class="colored">' : '<td>';
                        $table .= $bingokaart[$row] . '</td>';

        }

Thanks

R.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.