Jump to content

Recommended Posts

Hi all I am new to the forum and to PHP for that matter, so pardon the ignorance.  I have two arrays one which has prices and the other which is captured by an html form using $_POST.  I essentially want to be able to have the user enter quantities in my form and be able to multiply those quantities by my pricing array to obtain a total price per item. 

It seems like my $_POST data is being collected fine.  It is just my resulting array $result has no values to it no matter what I change on the user end.

 

<?php

if (($handle = fopen("fullbox.csv", "r")) !== FALSE) {
  while(($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $fullbox[] = $row[0];
  }
}

if(isset($_POST['submit']))
{

unset($_POST['submit']);
$userqty=array();
$userqty=$_POST;	

function array_multiply($a, $b) {

    if (!is_array($a) || !is_array($b)) {
        exit('Needs to be an array.');
    }

    $c1 = count($a);
    $c2 = count($b);

    if ($c1 != $c2) {
        exit('$setOne and $setTwo must be the same length.');
    }

    for ($i = 0; $i < $c1; $i++) {
        $r[$i] = $a[$i] * $b[$i];
    }

    return $r;

}

$result=array_multiply($userqty, $fullbox);

var_dump($fullbox);
echo '<br>';
echo '<br>';
var_dump($userqty);
echo '<br>';
echo '<br>';
var_dump($result);
}


?>

 

 

Here is the output assuming I entered values of 2,5,10 and12 as the first four entries in my HTML form.  Any help would be appreciated.

 

array(86) { [0]=> string(5) "8.53 " [1]=> string(5) "4.74 " [2]=> string(5) "5.00 " [3]=> string(5) "2.50 " [4]=> string(5) "6.48 " [5]=> string(5) "3.99 " [6]=> string(5) "8.53 " [7]=> string(5) "4.74 " [8]=> string(5) "8.53 " [9]=> string(5) "4.74 " [10]=> string(5) "6.73 " [11]=> string(5) "4.49 " [12]=> string(5) "8.53 " [13]=> string(5) "4.74 " [14]=> string(5) "7.98 " [15]=> string(5) "4.49 " [16]=> string(6) "20.49 " [17]=> string(6) "10.25 " [18]=> string(5) "9.50 " [19]=> string(5) "4.75 " [20]=> string(6) "17.05 " [21]=> string(5) "9.47 " [22]=> string(6) "17.97 " [23]=> string(5) "9.47 " [24]=> string(6) "25.16 " [25]=> string(6) "13.98 " [26]=> string(6) "17.95 " [27]=> string(5) "9.97 " [28]=> string(6) "17.10 " [29]=> string(5) "9.50 " [30]=> string(6) "14.22 " [31]=> string(5) "7.90 " [32]=> string(5) "9.85 " [33]=> string(5) "5.47 " [34]=> string(5) "8.95 " [35]=> string(5) "4.97 " [36]=> string(5) "8.95 " [37]=> string(5) "4.97 " [38]=> string(5) "8.95 " [39]=> string(5) "4.97 " [40]=> string(5) "8.95 " [41]=> string(5) "4.97 " [42]=> string(6) "12.55 " [43]=> string(5) "6.97 " [44]=> string(6) "12.55 " [45]=> string(5) "6.97 " [46]=> string(6) "17.95 " [47]=> string(5) "9.97 " [48]=> string(6) "12.55 " [49]=> string(5) "6.97 " [50]=> string(6) "17.95 " [51]=> string(5) "9.97 " [52]=> string(6) "17.95 " [53]=> string(5) "9.97 " [54]=> string(6) "50.38 " [55]=> string(6) "27.99 " [56]=> string(6) "71.98 " [57]=> string(6) "39.99 " [58]=> string(6) "36.53 " [59]=> string(6) "28.99 " [60]=> string(6) "43.98 " [61]=> string(6) "21.99 " [62]=> string(6) "43.64 " [63]=> string(6) "22.97 " [64]=> string(5) "5.99 " [65]=> string(5) "3.33 " [66]=> string(5) "4.64 " [67]=> string(5) "2.58 " [68]=> string(6) "10.75 " [69]=> string(5) "5.97 " [70]=> string(6) "13.45 " [71]=> string(5) "7.47 " [72]=> string(6) "14.35 " [73]=> string(5) "7.97 " [74]=> string(6) "14.35 " [75]=> string(5) "7.97 " [76]=> string(6) "14.35 " [77]=> string(5) "7.97 " [78]=> string(5) "7.64 " [79]=> string(5) "4.97 " [80]=> string(6) "35.98 " [81]=> string(6) "23.43 " [82]=> string(6) "43.18 " [83]=> string(6) "30.29 " [84]=> string(6) "44.98 " [85]=> string(6) "32.62 " }

 

array(86) { ["product01"]=> string(1) "2" ["product02"]=> string(1) "5" ["product03"]=> string(2) "10" ["product04"]=> string(2) "12" ["product05"]=> string(1) "0" ["product06"]=> string(1) "0" ["product07"]=> string(1) "0" ["product08"]=> string(1) "0" ["product09"]=> string(1) "0" ["product10"]=> string(1) "0" ["product11"]=> string(1) "0" ["product12"]=> string(1) "0" ["product13"]=> string(1) "0" ["product14"]=> string(1) "0" ["product15"]=> string(1) "0" ["product16"]=> string(1) "0" ["product17"]=> string(1) "0" ["product18"]=> string(1) "0" ["product19"]=> string(1) "0" ["product20"]=> string(1) "0" ["product21"]=> string(1) "0" ["product22"]=> string(1) "0" ["product23"]=> string(1) "0" ["product24"]=> string(1) "0" ["product25"]=> string(1) "0" ["product26"]=> string(1) "0" ["product27"]=> string(1) "0" ["product28"]=> string(1) "0" ["product29"]=> string(1) "0" ["product30"]=> string(1) "0" ["product31"]=> string(1) "0" ["product32"]=> string(1) "0" ["product33"]=> string(1) "0" ["product34"]=> string(1) "0" ["product35"]=> string(1) "0" ["product36"]=> string(1) "0" ["product37"]=> string(1) "0" ["product38"]=> string(1) "0" ["product39"]=> string(1) "0" ["product40"]=> string(1) "0" ["product41"]=> string(1) "0" ["product42"]=> string(1) "0" ["product43"]=> string(1) "0" ["product44"]=> string(1) "0" ["product45"]=> string(1) "0" ["product46"]=> string(1) "0" ["product47"]=> string(1) "0" ["product48"]=> string(1) "0" ["product49"]=> string(1) "0" ["product50"]=> string(1) "0" ["product51"]=> string(1) "0" ["product52"]=> string(1) "0" ["product53"]=> string(1) "0" ["product54"]=> string(1) "0" ["product55"]=> string(1) "0" ["product56"]=> string(1) "0" ["product57"]=> string(1) "0" ["product58"]=> string(1) "0" ["product59"]=> string(1) "0" ["product60"]=> string(1) "0" ["product61"]=> string(1) "0" ["product62"]=> string(1) "0" ["product63"]=> string(1) "0" ["product64"]=> string(1) "0" ["product65"]=> string(1) "0" ["product66"]=> string(1) "0" ["product67"]=> string(1) "0" ["product68"]=> string(1) "0" ["product69"]=> string(1) "0" ["product70"]=> string(1) "0" ["product71"]=> string(1) "0" ["product72"]=> string(1) "0" ["product73"]=> string(1) "0" ["product74"]=> string(1) "0" ["product75"]=> string(1) "0" ["product76"]=> string(1) "0" ["product77"]=> string(1) "0" ["product78"]=> string(1) "0" ["product79"]=> string(1) "0" ["product80"]=> string(1) "0" ["product81"]=> string(1) "0" ["product82"]=> string(1) "0" ["product83"]=> string(1) "0" ["product84"]=> string(1) "0" ["product85"]=> string(1) "0" ["product86"]=> string(1) "0" }

 

array(86) { [0]=> float(0) [1]=> float(0) [2]=> float(0) [3]=> float(0) [4]=> float(0) [5]=> float(0) [6]=> float(0) [7]=> float(0) [8]=> float(0) [9]=> float(0) [10]=> float(0) [11]=> float(0) [12]=> float(0) [13]=> float(0) [14]=> float(0) [15]=> float(0) [16]=> float(0) [17]=> float(0) [18]=> float(0) [19]=> float(0) [20]=> float(0) [21]=> float(0) [22]=> float(0) [23]=> float(0) [24]=> float(0) [25]=> float(0) [26]=> float(0) [27]=> float(0) [28]=> float(0) [29]=> float(0) [30]=> float(0) [31]=> float(0) [32]=> float(0) [33]=> float(0) [34]=> float(0) [35]=> float(0) [36]=> float(0) [37]=> float(0) [38]=> float(0) [39]=> float(0) [40]=> float(0) [41]=> float(0) [42]=> float(0) [43]=> float(0) [44]=> float(0) [45]=> float(0) [46]=> float(0) [47]=> float(0) [48]=> float(0) [49]=> float(0) [50]=> float(0) [51]=> float(0) [52]=> float(0) [53]=> float(0) [54]=> float(0) [55]=> float(0) [56]=> float(0) [57]=> float(0) [58]=> float(0) [59]=> float(0) [60]=> float(0) [61]=> float(0) [62]=> float(0) [63]=> float(0) [64]=> float(0) [65]=> float(0) [66]=> float(0) [67]=> float(0) [68]=> float(0) [69]=> float(0) [70]=> float(0) [71]=> float(0) [72]=> float(0) [73]=> float(0) [74]=> float(0) [75]=> float(0) [76]=> float(0) [77]=> float(0) [78]=> float(0) [79]=> float(0) [80]=> float(0) [81]=> float(0) [82]=> float(0) [83]=> float(0) [84]=> float(0) [85]=> float(0) }

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/265637-array-multiplication-issue/
Share on other sites

For each productNN and quantity you have,

0. If the quantity

1. Get just the NN part.

2. Subtract 1.

3. Look for the value in the array of prices under that (NN-1) key.

4. If and only if you find it then do whatever.

 

Even easier would be if you modified the form field name to be

name="product[$n]"

Then you can grab the number very easily with a foreach over $_POST["product"].

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.