Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-18-2012, 06:09 PM   PM User | #1
turpentyne
Regular Coder

 
Join Date: Aug 2010
Posts: 309
Thanks: 10
Thanked 1 Time in 1 Post
turpentyne is an unknown quantity at this point
breaking apart an array for math

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.

Code:
$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
	
	$qty_total=$qty_insert*$z_row['workshop_price'];
	$total = $total + $qty_total;
turpentyne is offline   Reply With Quote
Old 02-18-2012, 06:45 PM   PM User | #2
Chris Hick
Regular Coder

 
Join Date: Oct 2010
Location: Florence, MS
Posts: 476
Thanks: 10
Thanked 33 Times in 32 Posts
Chris Hick is an unknown quantity at this point
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?
__________________
Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
I always recommend the HEAD First series of books for learning a new coding language. ^_^
Chris Hick is offline   Reply With Quote
Old 02-18-2012, 06:53 PM   PM User | #3
turpentyne
Regular Coder

 
Join Date: Aug 2010
Posts: 309
Thanks: 10
Thanked 1 Time in 1 Post
turpentyne is an unknown quantity at this point
Ok. Here's the form on the first page:

PHP Code:
 <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>
<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'])) ;
$i++;
    
//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>
</select>
</td>

<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
  
?>
</table>
<input type="submit" value="Submit" class="buttontype"></form>
turpentyne is offline   Reply With Quote
Old 02-19-2012, 11:12 PM   PM User | #4
Prime8
New Coder

 
Join Date: Oct 2010
Posts: 51
Thanks: 2
Thanked 0 Times in 0 Posts
Prime8 is an unknown quantity at this point
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:
Code:
$qty = $_POST['participantqty'][$id];
Prime8 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:19 PM.


Advertisement
Log in to turn off these ads.