Brazilian Army Scheduling System

Asked

Viewed 655 times

6

I have a somewhat disturbing problem, I have researched several ways, but found no solution.

Below the explanation:

A very simple system, it looks like a calendar, but within each day of the calendar, there are 3 fields in checkbox to be clicked, in these fields I used the nomenclature of C, A, J (Coffee, Lunch and Dinner) and also made clickable the number of the day.

being as follows:

inserir a descrição da imagem aqui

With this system the Military can schedule the day he will eat in the kitchen.

The problem I’m having is that when it selects more than 1 day number the result appears all duplicated, like this:

inserir a descrição da imagem aqui

Follow the code below:

<!DOCTYPE HTML>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Sistema de Arranchamento</title>
    <?php
    /*include "conectando.php"; */
        date_default_timezone_set('America/Sao_Paulo');

         $dates = date('Y/m/d');
         $hoje = getdate(strtotime($dates));
         $ultimoDia = cal_days_in_month(CAL_GREGORIAN,
                                       $hoje['mon'],
                                       $hoje['year']);

        $primeiraSemana = (($hoje['wday'] + 1) -
                          ($hoje['mday'] - ((int)($hoje['mday'] / 6) * 7))) % 7;

    ?>

    <style>

        td[data-semana="0"] { color: #000000; }
    </style>
</head>
<body>
    <h1>Estamos em <?= $hoje['year'] ?></h1>
    <p><?= sprintf('Hoje é dia <strong>%0d / %0d</strong>, agora são %02d horas e %0d minutos.',
                   $hoje['mday'], $hoje['mon'], $hoje['hours'], $hoje['minutes'])
    ?></p>

    <table border="1">
        <tr>
            <th>Dom</th>
            <th>Seg</th>
            <th>Ter</th>
            <th>Qua</th>
            <th>Qui</th>
            <th>Sex</th>
            <th>Sáb</th>
        </tr>
        <tr>
    <form action="checkbox.php" method="post">
        <?php
        for($semana = 0; $semana < $primeiraSemana; ++$semana) {
            echo '<td>&nbsp;</td>';
        }
        for($dia = 1; $dia < $ultimoDia; ++$dia) {
            if( $semana > 6 ) {
                $semana = 0;
                echo '</tr><tr>';
            }

            echo "<td data-semana=\"$semana\"><center><font size='2px'/>";
            echo "$dia <input type='checkbox' name='dia[]' value='$dia'> <center><input type='checkbox' name='opcao[]' value='C'>C <input type='checkbox' name='opcao[]' value='A'>A <input type='checkbox' name='opcao[]' value='J'>J</td>";
            ++$semana;
        }
        for(; $semana < 7; ++$semana) {
            echo '<td>&nbsp;</td>';
        }

        ?>

    <?php
if( !empty( $_POST['dias'] ) ) {
    foreach( $_POST['dias'] as $key => $value ) {
        echo "<br />Semana $key<br />";
        foreach( $value as $dias ) {
            echo "$dias<br />";
        }
    }
}
?>

    <input type=submit value="Arranchar">
       </form>
        </tr>

    </table>
</body>
</html>

php checkbox.

<?php
// Verifica se usuário escolheu algum número

if(isset($_POST["dia"])){
   if(isset($_POST["opcao"])){ 


     echo "Você se arranchou para os dias:<BR>";

     // Faz loop pelo array dos numeros
     foreach($_POST["dia"] as $dia){
       foreach($_POST["opcao"] as $numero){


        echo " - " . $dia . " -" . $numero . "</BR>";
       }
     } 
   }
}
else
{
    echo "Você não se arranchou para nenhum dia!<br>";
}
?>
  • Give a print_r($_POST); see what returns to you...

  • The error is in the checkbox forach.. because you are doing two foreach.. so it doubles even :)

  • The problem is that the days have to be associated with the other checkbox

  • 1

    Once the question lock is finished, in about 45 minutes, please edit the question to change any kind of information that harms you, but without invalidating the answers. Thank you.

2 answers

2

I was able to solve your problem with array two-dimensional, because you need to know which day you choose and you wanted meals associated with that day.

echo "$dia 
<input type='checkbox' name='"; echo "arrachar[$dia][dia]";    echo"' value='$dia'> <center>
<input type='checkbox' name='"; echo "arrachar[$dia][opcaoC]"; echo"' value='C'>C 
<input type='checkbox' name='"; echo "arrachar[$dia][opcaoA]"; echo"' value='A'>A 
<input type='checkbox' name='"; echo "arrachar[$dia][opcaoJ]"; echo"' value='J'>J</td>";

And in the php checkbox.:

if(isset($_POST["arrachar"]))
{
    echo "Você se arranchou para os dias:<BR>";

    foreach($_POST["arrachar"] as $infos)
    {
        if(count($infos)==1) //verifica se tem alguma refeição marcada
                continue;
        $first = 1;
        foreach($infos as $info)
        {
            if($first == 1) //primeiro elemento do array é o dia.
            {
                echo "No dia $info";
                $first = 0;
            }
            else
                echo "e refeição $info";
            echo "<br>";
        }
        echo "<br>";
    }
}

See here the example

0

Draw up the foreach in this way: The way you did it, it’s duplicated... you can pass a single parameter.

foreach($_POST as $key){
    echo "{$key->dia} - {$key->opcao}";
}
  • I don’t think it works that way...

  • Because every day can have more than one option...

  • Take the test. I usually take the post and put it inside the foreach to fetch the arrays I want, no matter how many are for each day..

  • http://phpfiddle.org/main/code/uwsx-rcv7

  • It has to be with two-dimensional array to associate options with days...

  • Managed to elaborate?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.