Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts

    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;

  • #2
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    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. ^_^

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts
    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>

  • #4
    New Coder
    Join Date
    Oct 2010
    Posts
    51
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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];


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •