Array inside PHP Array

1

I'm creating a system where you can add medicines by protocols. Ex:    PROTOCOL NAME

    [ARRAY]
    NOME DO REMÉDIO
    [ARRAY 2]
    NOME DO ITEM        QUANTIDADE         TEMPO DE INFUSÃO [ARRAY] 
    NOME DO ITEM 2      QUANTIDADE 2       TEMPO DE INFUSÃO 2 [ARRAY]
    [ARRAY]
    NOME DE UM NOVO REMÉDIO
    NOME DO ITEM        QUANTIDADE         TEMPO DE INFUSÃO   [ARRAY]
    NOME DO ITEM 2      QUANTIDADE 2       TEMPO DE INFUSÃO 2 [ARRAY]
    NOME DO ITEM 3      QUANTIDADE 3       TEMPO DE INFUSÃO 3 [ARRAY]

I'm doing it this way:

$codigo = rand(5, 50000);
  // $codigo = uniqueAlfa(6);
  $itemA = $_POST['item'];
  $nomePres = $_POST['nome-item'];
   foreach ($itemA as $itemB):
     foreach (array($_POST['nome']) as $val1):
       foreach (array($_POST['tmp']) as $tmp):
    foreach (array($_POST['qtd']) as $val2):
      for ($i = 0; $i < count($val1); $i++):
        if($cadastra = $mysqli->query("INSERT INTO ifro_prescricao_medicamentos 
                  (id_prescricao, nome, item_padrao, item, quantidade, tempo_infusao) 
                  VALUES 
                 ('$codigo','$nomePres', '$itemB', '".$val1[$i]."', '".$val2[$i]."', '".$tmp[$i]."')")) {

          }  
        endfor;
      endforeach;
    endforeach;
  endforeach;
 endforeach;

But he is always registering all the same items, not separating the name of the remedies see the image:

Herearetheimagesonhowmyinformationregistrationformworks:

Thisimageisonly1drug Thisimagealreadyhas2medicines

    
asked by anonymous 23.08.2017 / 21:15

2 answers

1

The variable $ code is the same because it is outside the foreach, it should be placed inside so that a new random number is generated at each iteration.

The same goes for the variable $ namePres, it has to go into the foreach.

Now for the rest, it seems to me that logic is not good. I'm seeing that you get 5 values via POST method: item, item-name, name, tmp, and qtd. Do these 5 POSTS receive arrays of the same size? They should receive so that a table is correctly filled. Another thing, the values received via POST came out of HTML form inputs? If yes, the name attribute must contain the name followed by brackets. For example the value received tmp must be in an input name="tmp []".

Rewriting the code:

$itemA = $_POST['item'];

  $nomePres = $_POST['nome-item'];

  $nome = $_POST['nome'];

  $tmp = $_POST['tmp'];

  $qtd = $_POST['qtd'];

  $contador = 0;

  foreach ($itemA as $itemB) {

    $codigo = rand(5, 50000);

    $mysqli->query("INSERT INTO ifro_prescricao_medicamentos
            SET id_prescricao = '$codigo',
            nome = '$nome[$contador]',
            item_padrao = '$itemB',
            item = '$nome[$contador]',
            quantidade = '$qtd[$contador]',
            tempo_infusao = '$tmp[$contador]'");

    $contador++;

  }
    
24.08.2017 / 02:41
0

Sorry for the delay, maybe you have already managed to resolve. Anyway let's go.

Just confirm the following about the POSTS received:

  • 'item' comes from the input 'protocol';

  • 'item-name' comes from the input 'remedy 1, remedy 2, etc';

  • 'name' comes from the input 'name of the drug';

  • 'qtd' comes from the input 'quantity';

  • 'tmp' comes from the 'infusion time' input.

Another thing, should a code be generated for each protocol or for each medicine?

There really should be one foreach inside another, I think the code looks like this:

$itemA = $_POST['item'];

$nomePres = $_POST['nome-item'];

$nome = $_POST['nome'];

$tmp = $_POST['tmp'];

$qtd = $_POST['qtd'];

$contador = 0;

foreach ($itemA as $itemB) {

    $codigo = rand(5, 50000);

    foreach ($nomePres as $nomeRemedio) {

        $mysqli->query("INSERT INTO ifro_prescricao_medicamentos
            SET id_prescricao = '$codigo',
            nome = '$nome[$contador]',
            item_padrao = '$itemB',
            item = '$nome[$contador]',
            quantidade = '$qtd[$contador]',
            tempo_infusao = '$tmp[$contador]'");

        $contador++;

    }

}

The first foreach gives account of the protocol, the second foreach of the drug account, and the counter within that second foreach gives account of reading at the same time the item arrays, amount and infusion time.

Although if the protocol is always one at a time, nor did it need that first foreach.

Just make a note, always try to use variable names as close as possible to what they represent, otherwise it's easy to get lost. For example, in POSTS you get item, item name, and name, and call them $ itemA, $ itemB, $ name, $ namePres is very easy to confuse and hard to know what they match.     

26.08.2017 / 01:24