Brazilian Army Scheduling System

6

I have a somewhat disturbing problem, I've tried it in several ways, but I have not found a solution.

Below the explanation:

A very simple system, it looks like a calendar, but within each calendar day, 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 the number of the day clickable.

being as follows:

WiththissystemtheMilitarymanagestoschedulethedayhewilleatinthekitchen.

TheproblemI'mhavingisthatwhenitselectsmorethan1daynumbertheresultappearsallduplicate,likethis:

The following is 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>

checkbox.php

<?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>";
}
?>
    
asked by anonymous 14.07.2015 / 17:29

2 answers

2

I was able to solve your problem with two-dimensional array , because you need to know which day to choose and 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 checkbox.php :

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 the example here

    
14.07.2015 / 18:36
0

Construct foreach like this: The way you did, it's duplicate ... You can only pass one parameter.

foreach($_POST as $key){
    echo "{$key->dia} - {$key->opcao}";
}
    
14.07.2015 / 17:51