Register schedules in the database

Asked

Viewed 142 times

0

I have a school schedule registration system that’s on this model:

inserir a descrição da imagem aqui

I need to include in the database that’s with this structure:

inserir a descrição da imagem aqui

Ex.:

From 07:00 to 07:05, I chose the following subjects: Mathematics, Portuguese and Biology.

And in the hours from 09:00 to 09:25, I chose the subjects: Philosophy, Chemistry and Arts.

But I’m not getting it.

See the result

inserir a descrição da imagem aqui

I’m doing it this way:

Taking data from the form

$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;
}

Registration method Choose()

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];
             }    
          }  
    }
}
  • what mistake you see?

  • 1

    Hello Fbidu. Not fitting in the table layout. I edited my post to show the result.

  • Improve your explanation, what your expectation?

  • Hello Claudio. My expectation is: I need that when registering the schedule grid (figure 1), it is stored in the database according to the layout of the table (figure 2), but I am not able to, because it is registered in all columns (figure 3).

1 answer

0

This unexpected behavior happens because of the name Materias which is applied in all selects. Look at this example, which I imagine is 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 materials of the schedules 1 and 2 are placed in the same variable. See the var_dump of the variable $_POST after the submission of the form:

<?php

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

Produces as an 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 to the material selects of the schedules 1 and 2. As for example Materials.

Renaming a select

In one of the code Voce placed there: return $listar;. I suppose it is called a function and it returns the html of select. Then from this, it is possible from the returned select, to 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)
  • Hello Juven_v. I understand. But in my case, where the select of the Materials is built from the BD records, as I would do to name them separately?

  • I added in response

  • Thank you for the help Juven_v, but unfortunately did not give to my solution, as it is added more schedules, more fields of materials is created.

Browser other questions tagged

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