View Full Version : breaking apart an array for math

02-18-2012, 06:09 PM
Can anybody help me figure out...
I've got an array that has keys matching the class id, and value is how many students. I need to take (# students x cost-class213) + (# students x cost-class213) = total cost.

Below, I can't get qty_insert to work, so the math breaks down.

$ids = $_POST['workshop_id'];

foreach ($_POST['workshop_id'] as $id){
$qty = $_POST['participatqty'][$id];

// above 'participatqty' is an Array ( [215] => [213] => 1 [212] => 2 [214] => )

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];

// below is the math

$total = $total + $qty_total;

Chris Hick
02-18-2012, 06:45 PM
Ok, let me get this straight, you are using a form for submitting class ids and and the number of students in the class, correct? Could you post your form fields?

02-18-2012, 06:53 PM
Ok. Here's the form on the first page:

<form method="post" action="register2.php">

<table width=100%>
<tr><td bgcolor="#CCCCCC" colspan="4" height="50"><b>Please</b> check all workshops you'd like to register for at this time. New and returning participants always welcome!</td></tr>
<tr><td colspan=4><br></td></tr>
<td bgcolor="" width="150" valign=top style='padding-right:40px;'>Workshop<br></td>
<td bgcolor="" width="80" valign=top style='padding-right:10px;'>Dates<br></td>
<td bgcolor="" width="80" valign=top style='padding-right:10px;'># of participants<br></td>
<td bgcolor="" width="80" valign=top style='padding-right:10px;'>Tuition<br></td></tr>

<tr><td colspan=4><hr style="color: #000;background-color: #000;height: 5px;"></td></tr>

<? //output each row

while ($c_row = mysql_fetch_array($result_all_events)){
$display = date("F j", strtotime($c_row['workshop_date'])) ;
//date('d m y', strtotime($mysql_date));

<tr><td style='padding-right:20px;padding-top:5px;padding-bottom:5px;' valign=top>
<!-- this is where the number of the workshop pulls from database -->
<input type="checkbox" name="workshop_id[]" value="<?= $c_row['workshop_id'] ?>" />

<?= $c_row['workshop_title'] ?> </td><td style='padding-top:5px;padding-bottom:5px;' valign=top><?= $c_row['workshop_date'] ?></td>

<!-- this is where they choose how many students. this passes to the page where I'm having a problem -->
<td valign=top style='padding-top:5px;padding-bottom:5px;'><select name="participantqty[<?= $c_row['workshop_id'] ?>]" id="" class="">
<option value="">select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="10">9</option>
<option value="10">10</option>

<td valign=top style='padding-top:5px;padding-bottom:5px;'>$ <?= $c_row['workshop_price'] ?></td></tr>

<tr><td colspan=4><hr></td></tr>

} //end while
<input type="submit" value="Submit" class="buttontype"></form>

02-19-2012, 11:12 PM
Forgive my ignorance, but you have an 'n' in "<select name=participantqty[.." on your form which you don't have in your script. Could this be the problem?

Maybe try this:

$qty = $_POST['participantqty'][$id];