turpentyne Posted February 18, 2012 Share Posted February 18, 2012 I have to take over somebody else's project. So I have the combination of being a beginner, not understanding their code well, and not experienced with arrays. I'm in over my head. I have an array passed to the page, that would be something like this: Array ( [215] => [213] => 1 [212] => 2 [214] => ) the user chose 1 person in 213, and 2 people in 212 and I basically need to break it apart to do math like this: (#people x cost of 213 = totalcostA ) + (# people x cost of 212 = totalcostB) = total The broken down data goes into the database, and the total is used for the cart. I've taken suggestions and gotten to this jumbled mess: $ids = $_POST['workshop_id']; print_r($_POST['participantqty']); // the above shows the arrays are sending to this page and printing correctly: Array ( [215] => [213] => 1 [212] => 2 [214] => ) foreach ($_POST['workshop_id'] as $id){ $qty = $_POST['participatqty'][$id]; } if(sizeof($_POST['workshop_id'])) { // loop through array $number = count($ids); for ($i=0; $i<=$number; $i++) { // store a single item number and description in local variables $itno = $ids[$i]; $qty_insert = $qty[$i]; $query_insertItemWorkshop = "INSERT INTO tbl_registration_workshop (registration_id, workshop_id, regworkshop_qty) VALUES ('$reg_id', '$itno', '$qty_insert')"; $dberror = ""; $ret = mysql_query($query_insertItemWorkshop); $query_selectAllItems_events = 'SELECT * FROM tbl_workshops where workshop_id = '.$itno; @$result_all_events = mysql_query($query_selectAllItems_events); @$numRows_all_events = mysql_num_rows($result_all_events); @$num=mysql_num_rows($result_all_events); @$z_row = mysql_fetch_array($result_all_events); // I want the below to work, but it won't until I can figure out how to break it all down. $qty_total=$qty_insert*$z_row['workshop_price']; $total = $total + $qty_total; if ($ids[$i] <> '') { // I want the info to also appear below, but can't until I figure out how to break them down. echo $total_students; echo "<tr><td valign=top><b>Description:</b> ". $z_row['workshop_title'] ."</td><td align='right' colspan='2' width=300 valign=top>" . $qty_insert . " at ". $z_row['workshop_price'] ." each: </td><td valign=top> $". $qty_total ."</td></tr>"; Quote Link to comment https://forums.phpfreaks.com/topic/257252-struggling-to-break-apart-array-for-math/ Share on other sites More sharing options...
ignace Posted February 18, 2012 Share Posted February 18, 2012 Since there is too much code to figure out where you are actually multiplying/adding the totals, here's some example code: echo calculate_total(array(215 => '', 213 => 1, 212 => 2, 214 => '')); function calculate_total(array $array) { $total = 0; foreach (array_filter($array) as $key => $value) { $total += $key * $value; } return $total; } The function cleans the array of unwanted values (empty). Then for each of the remaining values it will multiply the key with it's value and add it to the total. Quote Link to comment https://forums.phpfreaks.com/topic/257252-struggling-to-break-apart-array-for-math/#findComment-1318687 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.