Foreach group values

Asked

Viewed 300 times

-2

I have the following code

  foreach ($_POST['codmunicipio'] as $key => $valor2) {
      $codmunicipio = $valor2;

       $valor_ipl3_f = $_POST['IPL3_valor_F'];
       $valor_ipl3_j = $_POST['IPL3_valor_J'];

  $sql3 = "INSERT INTO financeiro_sici_anatel_ipl3 (ano, mes, fistel,codmunicipio, IPL3_valor_F, IPL3_valor_J)

  VALUES ('$ano_upload', '$mes_uplooad', '$fistel','$codmunicipio', '$valor_ipl3_f', '$valor_ipl3_j')"; 

  }

I’m trying to make sure that for each municipality code it takes a value f and j_

        exemplo: codmunicipio 1
     valor_f 20
     valorj_ 30

     codmunicipio 2
     valor_f 10
     valor_j 50

etc.

and then enter into a database.

any hint?

this information comes from the following page:

  <?php  

   $id = 1;  

    while($dado_ipl3 = mysqli_fetch_array($qry_ipl3)) { 

      $id++;

     $nome_municipio = $dado_ipl3['CIDADE'];
       $codmunicipio = $dado_ipl3['COD_CIDADE'];








           ?>

         <input type="hidden" name="id[]" value="<?php echo $id;?>">

        <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">


      <div class="container">


    <div class="row">       
    <label class="col-md-3">IPL3 Distribuição do quantitativo total de acessos fisícos em serviço por tipo de usuário (mensal)</label>                       
    </div>


     <div class="row">
     <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">        
    <label class="col-md-3">Nome Município</label>
    <div class="col-md-9 col-sm-6 col-xs-6 form-group has-feedback">
    <input type="text" name="nome_municipio" id="nome_municipio" class="form-control" value="<?php echo"$nome_municipio" ?>"   maxlength="18" size="18" title="Nome Município">
    </div>                       
    </div> 
    </div>


    <div class="row">
    <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">        
    <label class="col-md-3">Codigo Municipio</label>
    <div class="col-md-9 col-sm-6 col-xs-6 form-group has-feedback">
    <input type="text" name="codmunicipio[]" id="codmunicipio" class="form-control numero_livre" value="<?php echo $codmunicipio; ?>"  maxlength="7" size="7">
    </div>                       
    </div> 
  </div>


     <div class="row">
     <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">        
    <label class="col-md-3">Acesso físico P.F.</label>
    <div class="col-md-9 col-sm-6 col-xs-6 form-group has-feedback">
    <input type="text" name="IPL3_valor_F[]" id="IPL3_valor_F" class="form-control numero" value="0"   maxlength="18" size="18" title="Quantitativo de Acesso físico em serviço pelo tipo da Pessoa Física">
    </div>                       
    </div> 
</div>

     <div class="row">
    <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">        
    <label class="col-md-3">Acesso físico P.J.</label>
    <div class="col-md-9 col-sm-6 col-xs-6 form-group has-feedback">
   <input type="text" name="IPL3_valor_J[]" id="IPL3_valor_J" class="form-control numero" value="0"  maxlength="18" size="18" title="Quantitativo de Acesso físico em serviço pelo tipo da Pessoa Jurídica">
    </div>                       
    </div> 
   </div>


  </div>
   </div>


   <?php   


  }


 ?>
  • 1

    that $_POST['codmunicipio'] is an array? What exactly is the problem? You are unable to insert?

  • 1

    What you want is to enter these values at once?

  • 1

    Another thing.. These values are an array? $valor_ipl3_f = $_POST['IPL3_valor_F'];&#xA; $valor_ipl3_j = $_POST['IPL3_valor_J']; ?

  • I posted the source page of the values. They are within a structure While...this is my difficulty. move to another page to enter all information in the database

  • 1

    I think I understand... these values are numerical?

  • Yeah! They’re all numbers.

  • If you have a municipality with you normally. but when you have several I cannot pass the values to another page.

  • 1

    I get it... I’m analyzing it here. I’m going to formulate a response.

  • Thank you very much for your help.

Show 4 more comments

1 answer

1


I created the code and in it I am explaining in detail:

 // primeira parte da string de inserção
 $sql3 = " INSERT INTO financeiro_sici_anatel_ipl3 (ano, mes, fistel,codmunicipio, IPL3_valor_F, IPL3_valor_J) VALUES ";
 $chave = 0; // chave inicial

 foreach ($_POST['codmunicipio'] as $key => $valor2) {

      $codmunicipio = $valor2;
      $valor_ipl3_f = $_POST['IPL3_valor_F'][$chave]; // pega o valor relacionado ao municipio
      $valor_ipl3_j = $_POST['IPL3_valor_J'][$chave]; // pega o valor relacionado ao municipio
      $sql3 .= "('$ano_upload', '$mes_uplooad', '$fistel','$codmunicipio', '$valor_ipl3_f', '$valor_ipl3_j'),"; // concatena os valores
      $chave++; // adiciona a próxima chave
  }

  $sql3 = substr($sql3, 0, -1); // retira a ultima virgula
  1. Note that I have put the query string outside the foreach why you can upload the information at once. And I was concatenating the values that will be entered. Then, in the end, your string will look something like this:

Example with 2 municipalities:

$sql3 = "INSERT INTO financeiro_sici_anatel_ipl3 
(ano, mes, fistel,codmunicipio, IPL3_valor_F, IPL3_valor_J) 
VALUES 
('2017', '04', 'fistel1','10', '1', '2'),
('2016', '05', 'fistel2','12', '10', '5')";
  1. The values of arrays $valor_ipl3_f and $valor_ipl3_j can be accessed numerically. So each information accessed from the county code by foreach, the next information will be one of each of these arrays. So I did it this way, using the variable $chave.

If you have any more questions or problems, post here that we arrange.

  • 1

    Thank you again! I managed to enter in the database!

  • 1

    @Freitas that good! Hug!

Browser other questions tagged

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