Register schedules in the database

0

I have a school calendar system that is in this template:

Ineedtoincludeinthedatabasethathasthisstructure:

Forexample:

Inthehoursof07:00to07:05,IchosetheMathematics,PortugueseandBiologysubjectsonMonday,WednesdayandFriday.

Andinthetimeof09:00to09:25,IchoseonTues,ThursandSatthesubjects:Philosophy,ChemistryandArts.

ButIcannot.

Seetheresult

I'mdoingthefollowing:

Gettingformdata

$horarioEntrada=$_POST["HorarioEntrada"];
$horarioSaida = $_POST["HorarioSaida"];
$materias = $_POST["Materias"];
echo $metodos->cadastrarGradeEscolar($horarioEntrada,$horarioSaida,$materias);

Schedules

<select class="form-control" name="HorarioEntrada[]" style="width:90px">
  <?php
      for($horarioInicio = 7; $horarioInicio <= 22; $horarioInicio++){
           $horarioInicio = ($horarioInicio < 10)?("0".$horarioInicio):($horarioInicio);
    ?>
        <option value="<?php echo $horarioInicio; ?>:00"><?php echo $horarioInicio; ?>:00</option>
  <?php } ?>
</select>
<span class="input-group-addon" style="background-color: #FAFAFA">às</span>
<select class="form-control" name="HorarioSaida[]" style="width:90px">
  <?php
    $horaFinal = "07:00";

      for($horaFim = 0; $horaFim < 180; $horaFim++){
          $horaFinal = date('H:i', strtotime('+5 minute', strtotime($horaFinal)));
      ?>
      <option value="<?php echo $horaFinal; ?>"><?php echo $horaFinal; ?></option>
      <?php } ?>

  ?>
</select>

Matters

$sqlListar = mysqli_query($this->conexao,"SELECT * FROM pe_materias WHERE IdEscolas = '".$idEscolas."';");

  $listar = "<select name='Materias[]' class='form-control'>";
  $listar .= "<option value='Selecione'>Matéria</option>";
  while($jmListar = mysqli_fetch_object($sqlListar)){
        $listar .= "<option value='".$diaMateria."_".$jmListar->IdMaterias."'>".$jmListar->Materias."</option>";
  }
   $listar .= "</select>";
   return $listar;
}

Method to signGradeSchool ()

public function cadastrarGradeEscolar($horarioEntrada,$horarioSaida,$materias){
    foreach($horarioEntrada as $valor => $horaE){
        for($i = 0; $i < count($materias); $i++){
            if($materias[$i] != "Selecione"){
                 echo $horaE . "= " .$materias[$i];
             }    
          }  
    }
}
    
asked by anonymous 20.07.2017 / 15:59

1 answer

0

This unexpected behavior happens because of the Materias name that is applied to all selects. Look at this example, which I imagine to be similar to your situation:

<form method="post">
<div>
horario 1
<!-- Segunda -->
	<select name="Materias[]">
	  <option value="Selecione">Materia</option>
	  <option value="portugues" selected>Portguês</option>
	  <option value="matematica">Matemáica</option>
	  <option value="artes">Artes</option>
	</select>
<!-- Terça -->
<select name="Materias[]">
	  <option value="Selecione">Materia</option>
	  <option value="portugues">Portguês</option>
	  <option value="matematica" selected>Matemáica</option>
	  <option value="artes">Artes</option>
</select>

<!-- Quarta -->
	<select name="Materias[]">
	  <option value="Selecione">Materia</option>
	  <option value="portugues">Portguês</option>
	  <option value="matematica">Matemáica</option>
	  <option value="artes" selected>Artes</option>
	</select>
</div>

<div>
horario 2
<!-- Segunda -->
	<select name="Materias[]">
	  <option value="Selecione">Materia</option>
	  <option value="portugues">Portguês</option>
	  <option value="matematica selected">Matemáica</option>
	  <option value="artes">Artes</option>
	</select>
<!-- Terça -->
<select name="Materias[]">
	  <option value="Selecione">Materia</option>
	  <option value="portugues">Portguês</option>
	  <option value="matematica">Matemáica</option>
	  <option value="artes" selected>Artes</option>
</select>

<!-- Quarta -->
	<select name="Materias[]">
	  <option value="Selecione" selected>Materia</option>
	  <option value="portugues">Portguês</option>
	  <option value="matematica">Matemáica</option>
	  <option value="artes">Artes</option>
	</select>
</div>

<input type="submit">
</form> 

When the form is submitted the subjects of schedules 1 and 2 are placed in the same variable. See var_dump of variable $_POST after submitting form:

<?php

if(!empty($_POST)){
var_dump($_POST);
}

Produce as output:

array (size=1)
  'Materias' => 
    array (size=6)
      0 => string 'portugues' (length=9)
      1 => string 'matematica' (length=10)
      2 => string 'artes' (length=5)
      3 => string 'Selecione' (length=9)
      4 => string 'artes' (length=5)
      5 => string 'Selecione' (length=9)

The solution would be to give a different name for the material selections of schedules 1 and 2. For example, Materias_Horario1 and Materias_Horario2.

Renaming a select

In one of the codes that you put there is the excerpt: return $listar; . I assume a function is called and it returns the html of the select. So starting from that, it is possible from the returned select, create another by replacing only the name. Example:

<?php

function retornaSelect(){
    //após a busca no banco
    $lista = '<select name="Materias[]">
      <option value="Selecione" selected>Materia</option>
      <option value="portugues">Portguês</option>
      <option value="matematica">Matemáica</option>
      <option value="artes">Artes</option>
    </select>';

    return $lista;
}

$select_horario_1 = retornaSelect();
//str_replace substitui o Materias[] por MateriasOutroNome[]
$select_horario_2 = str_replace('name="Materias[]"', 'name="MateriasOutroNome[]"', $select_horario_1);

var_dump($select_horario_1);
var_dump($select_horario_2);

Produces as a result:

'<select name="Materias[]">
      <option value="Selecione" selected>Materia</option>
      <option value="portugues">Portguês</option>
      <option value="matematica">Matemáica</option>
      <option value="artes">Artes</option>
    </select>' (length=230)

'<select name="MateriasOutroNome[]">
      <option value="Selecione" selected>Materia</option>
      <option value="portugues">Portguês</option>
      <option value="matematica">Matemáica</option>
      <option value="artes">Artes</option>
    </select>' (length=239)
    
20.07.2017 / 20:13