Include data via a Mysql/PHP explode

Asked

Viewed 126 times

0

I have the following table:

inserir a descrição da imagem aqui

However, I need to include the materials within the respective fields of the day of the week refeeded to the schedules. For this, I am using 02 methods that create the image below:

inserir a descrição da imagem aqui

public function cadastrarHEscolas($idEscolas,$idGrades){
   ...
   $this->comboboxGrade($jmVisualizar->IdHorarios,$idEscolas); 
   ...
}

public function comboboxGrade($idHorarios,$idEscolas){

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

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

So far so good, but for me to store inside the Idhorarios, I included in the second method, in the option attribute the following line:

$listar .= "<option value='".$jmListar->Materias."_".$idHorarios."'>".$jmListar->Materias."</option>";

The above code returns me the Table Id Times and Matter, separated by the underline.

And when I click to register, I use the method below to register in the database:

    public function cadastrarGradeMaterias($materias){

   // $materias me retorna portugues_1, matematica_1, fisica_2, etc.

       for($m = 0; $m < count($materias); $m++){ 
           $mat = explode("_",$materias);

        $sqlCadastrar = mysqli_query($this->conexao,"UPDATE tabela SET Segunda - '', Terca = '', Quarta = '', Quinta = '', Sexta = '', Sabado = '' WHERE IdHorario = ''");
       }
    }

Here’s what the problem is. How would I blow up and include within the query the selected fields and their respective ID?

  • How do you intend to register the days if you are not posting membership values between day, subject and time? You are only sending news and time. And in fact would have to send the matter the schedule and the day of the week to make this update.

  • Hello Rafael. The schedules are already registered, I just want to update the table including the materials that are going in the first block of code along with the table Id. Ex.: portugues_1, matematica_1, etc.

  • In this case when you perform your explode by the underline in $mat[0] is the matter and $mat[1] the idHorario

  • this... only that according to the second image, has several fields referring to the schedules. That’s where I’m struggling. How do I change the table, including the materials, in the respective fields of the day of the week and time?

  • That’s what my first message referred to... maybe you didn’t understand what I said. In fact I would make this option by assigning the name attribute as Materiassegunda[] , Materiasterca[] and so on because when posting the value you will know the day to matter and the idHorario

  • I get it... I’ll try here...

Show 1 more comment

1 answer

1


There’s one more detail Notice you just set it up like this :

       <select name='Materias[]' id='materias' class='form-control'>

i asked to rename, to also have the understanding of the day there in the update routine. But if you are setting a list type in your html, name=MateriasSegunda[] there in your php code you will receive an array of values of select that has this name in the case following by your image the values of select 07:00 - 07:40 , 08:00 - 08:40 and 09:00 - 09-40 so this line of explode will not work:

  $materias_segunda = $_POST['MateriasSegunda'];
  $mat = explode("_",$materias_segunda);

You’d have to execute the blast like this:

  $materias_segunda = $_POST['MateriasSegunda'];
  foreach($materias_segunda as $materia_hora){
     $mat = explode("_",$materia_hora); 
     $materia_segunda[]        = $materia_hora[0]; 
     $materia_segunda_idHora[] = $materia_hora[1]; 
  }

Namely the $_POST['MateriasSegunda'] is an array containing the materials and idHorario on Monday.

After doing this on all days of the week, and in possession of the values of the day hour and matter ai could effect its update.

Improve my example I wrote quickly only for understanding.

  • Hello Rafael. Right. I’ll try to do it this way.

Browser other questions tagged

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