php - Create an array and calc

982

i'm from France i hope you'll understand me, i spent one year in usa, anyway...

Ok, i have a form, i received that from this form :

cmdId[] 25
cmdId[] 26
cmdId[] 27
cmdId[] 28
cmdId[] 29
cmdId[] 30
cmdId[] 31
cmdId[] 32
cmdId[] 33
cmdPrice[]  3.05
cmdPrice[]  3.46
cmdPrice[]  3.46
cmdPrice[]  550
cmdPrice[]  3.46
cmdPrice[]  4.3
cmdPrice[]  3.75
cmdPrice[]  3.3
cmdPrice[]  4.15
cmdQty[]    1
cmdQty[]    1
cmdQty[]    26
cmdQty[]    19
cmdQty[]    8
cmdQty[]    5
cmdQty[]    7
cmdQty[]    3

My form (just the important part)

  echo '<tr>';
   echo '<input type="hidden" value="'.$data3['LGCM_ID'].'" name="cmdId[]">';
   echo '<td><input value="'.$data3['LGCM_QTE_COMMANDE'].'" name="cmdQty[]" id="cmdQty" type="text"></td>';
   echo '<td>'.$data3['PRD_POIDS'].'</td>';
   echo '<td>...</td>';
   echo '<td>'.$data3['PRD_NAME'].'</td>';
   echo '<td>'.$data3['PRD_LIBELLE'].'</td>';
   echo '<td>data 2</td>';
   echo '<td><input value="'.$data3['LGCM_PRIX_UNITAIRE'].'" name="cmdPrice[]" id="cmdPrice" type="text"> € HT</td>';
   echo '<td>'.$data3['LGCM_PRIX_UNITAIRE'].' € TTC</td>';
   echo '</tr>';

I want to create a array, then a foreach and getting the total like this :

Array 1

cmdId : 25
cmdQty: 20
cmdPrice : 10
=> subtotal : cmdQty * cmdPrice = 200

Array 2

cmdId : 26
cmdQty: 40
cmdPrice : 100
=> subtotal : cmdQty * cmdPrice = 4000

Total (subtotal + subtotal etc...)

I don't arrive to create a good foreach and a correct array in my case.

thanks

609

Answer

Solution:

assuming that all arrays are the same size and posted in same order:

$cmdId = $_REQUEST['cmdId'];
$cmdPrice = $_REQUEST['cmdPrice'];
$cmdQty = $_REQUEST['cmdQty'];
$len = count($cmdId);

$output = array();
for ($i=0; i<$len; $i++)
    $output[] = array('cmdId' => $cmdId[$i],
        'cmdQty' => $cmdQty[$i],
        'cmdPrice' => $cmdPrice[i],
        'subtotal' => $cmdQty[$i] * $cmdPrice[i]);

print_r($output);
166

Answer

Solution:

i made a mistake, if i wrote my json answer without print_r, i got the same message, look,

     <?php
    header('content-type: application/json');

    include 'includes/config.php';

   $cmdId = $_REQUEST['cmdId'];
   $cmdPrice = $_REQUEST['cmdPrice'];
   $cmdQty = $_REQUEST['cmdQty'];
   $len = count($cmdId);


    $output = array();
    for ($i=0; i<$len; $i++)
        $output[] = array('cmdId' => $cmdId[$i],
            'cmdQty' => $cmdQty[$i],
            'cmdPrice' => $cmdPrice[$i],
            'subtotal' => $cmdQty[$i] * $cmdPrice[$i]);

    $items = $output;

    $response = $items;

    echo json_encode($response);

    ?>

line 13 :

 $output[] = array('cmdId' => $cmdId[$i],

Allowed memory size of 134217728 bytes exhausted (tried to allocate 79 bytes) in /home/public_html/admin/cmd_execute_step2.php on line 13

Thanks.

People are also looking for solutions to the problem: html - php code not sending email

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.