Array within PHP Array

Asked

Viewed 383 times

1

I’m creating a system where you can add drugs 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’s always registering all the same items, not separating the names from the remedies see the image: inserir a descrição da imagem aqui


Follow the images on how is my information registration form:

This image is with only 1 medicine Esta imagem esta com apenas 1 medicamento This image already with 2 medicines Esta imagem já com 2 medicamentos

2 answers

1

The $code variable 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 $nomePres, it has to go inside the foreach.

As for the rest, I think the logic is not good. I see that you receive 5 values via POST method: item, name-item, name, tmp and Qtd. Do these 5 POSTS receive arrays of the same size? They should be paid for a table to be correctly filled in. Otherwise, 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++;

  }
  • But it does not do the array inside each of them, I will send the photo of my form.

  • Follow the image link https://i.stack.Imgur.com/z8tOx.png

  • The variable $codigo is really necessary? one can not just change the column id_prescricao for AUTO INCREMENT ?

  • Yes it is necessary, because I need to recover it later, like the prescription 1029 has item 1, 2, 3 with medicines 4, 6 , 7, understood?

0

I’m sorry it took so long, maybe I’ve already figured it out. Anyway let’s go.

Just confirm the following regarding the POSTS received:

  • 'item 'comes from protocol input';

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

  • 'name' comes from the 'medicine name' input';

  • 'Qtd' comes from the quantity input';

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

Something else, a code should be generated for each protocol or for each remedy?

There really must be a foreach inside another, I think the code is as follows:

$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 takes care of the protocol, the second foreach of the medicine account, and the counter within that second foreach is able to read at the same time the item arrays, amount and infusion time.

Although if the protocol is always one at a time, not even needed that first foreach.

Just an observation, always try to use variable names as close as possible to what they represent, because otherwise it is very easy to get lost. For example, in POSTS you receive item, name-item and name, and call them $Itema, $itemB, $name, $nomePres it is very easy to confuse and difficult to know what they match.

  • Leandro, sorry for my ignorance, but is giving the following error Warning: Invalid argument supplied for foreach() in F: wamp www froc sistema acao insere-prescricao-medicamento.php on line 48

  • Come on, I didn’t understand your question: 'name-protocol' - would be the name of the protocol 'medicine[]' - the name of the remedy 'name-item[]' - would be the name of the item 'Qtd' - the quantity of the item 'tmp' - infusion time Remembering that he may all have various remedies (medicines) with other items within him Medicine 1 ITEM THE AMOUNT OF TIME INFUSION A ITEM A-1 QUANTITY A-1 TIME INFUSION A-1 MEDICINE 2 ITEM 2 QUANTITY B TIME INFUSION B

  • I am registering but registering the name of the repeated items Cmo get: string'medicament' (length=11) string 'ITEM 1' (length=6) string 'item 2' (length=6) string 'item 3' (length=6) string 'item 1-a' (length=8) string 'medicine 2' (length=13) string 'ITEM 1' (length=6) string 'item 2' (length=6) string 'item 3' (length=6) string 'item 1-a' (length=8) Whereas DRUG 2 only registered 1 item, and it brings all others. Thanks for your help in trying to resolve this condition

Browser other questions tagged

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