0

Bear with me, I'm not the best at PHP, and I have to take over a project somebody else abandoned. They built a form where people choose one or more classes, and how many people they're enrolling.

Once the user picks a class and the number of enrollees, they press submit and go to registration page 2 where the totals are to be summed based on class costs and # of students. But the number of students isn't appearing for each class. Here's where I'm having trouble:

Page one does send the number of students for each class in an array that matches the class number. For example, if there were 4 classes available and the user selected class 213 - 1 student, and class 214 - 2 students, a print_r shows on page two:

Array ( [215] => [213] => 1 [212] => 2 [214] => )

But I'm not understanding how to generate that number when I print to the page:

here's what I've got, so far, on page two:

<?php
// several other variables were set here, dumped into db then...
// store all posted intemnos and descriptions in local arrays 

$ids = $_POST['workshop_id'];
print_r($_POST['participantqty']);

// the above shows the arrays  are sending and printing correctly: Array ( [215] => [213] => 1 [212] => 2 [214] => )
 
foreach ($_POST['workshop_id'] as $id){
   $qty = $_POST['participatqty'][$id];
}

// is the above not how I do this? Does it go somewhere else

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];
print_r($qty_insert);

			$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);
	$qty_total=$qty_insert*$z_row['workshop_price'];
	$total = $total + $qty_total;
    
    if ($ids[$i] <> '') { 
	// below at qty_insert is where the number isn't generating
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: &nbsp;</td><td valign=top> $". $qty_total ."</td></tr>";

	    } 
} 	
}



			
?>
2
Contributors
11
Replies
12
Views
5 Years
Discussion Span
Last Post by turpentyne
0
$p = (array) $_POST['participantqty'];
$total_students = array_sum($p);
echo $total_students;

will give total students

0

Hmmm... that gives me the total students, but adds the number again for each class chosen?

one class gives:
Array ( [215] => [213] => 2 [212] => [214] => ) 22
two classes:
Array ( [215] => [213] => 2 [212] => 1 [214] => ) 333
three classes:
Array ( [215] => 4 [213] => 2 [212] => 1 [214] => ) 7777

Edited by turpentyne: n/a

0

Ah, duh! I'm realizine what you suggested is a little different than what I need. I'm basically trying to break down the array to get

# students x cost of class A = totalcostA
Plus
# students x cost of class B = totalcostB

totalcostA + totalcostB = Total.

I can't just total up the students, because the classes are different prices.
So I was trying to loop to get the math working for each class chosen. They may choose only one class, but sometimes two or more.

Edited by turpentyne: n/a

0
$filtered = array_filter($_POST['participantqty']);
sort($filtered);
$reg_id = '888';

foreach($array as $k=>$v){
	$vals[] = "('$reg_id','$k',$v)";
	$keys[] = $k;	
}

$valstring = implode(",",$vals);
$keystring = "'" . implode("','", $keys) . "'";

$q = mysql_query("INSERT INTO tbl_registration_workshop (registration_id, workshop_id, regworkshop_qty) VALUES $valstring");

$q = mysql_query("SELECT * FROM tbl_workshops WHERE workshop_id IN($keystring) ORDER BY workshop_id");
if(mysql_num_rows($q)){
	$output = "";
	while($d = mysql_fetch_array($q)){
		$qty_insert = $filtered[$d['workshop_id']];
		$qty_total = $qty_insert * $d['workshop_price'];
		$output .= "<tr><td valign=\"top\"><b>Description:</b> {$d['workshop_title']}</td><td align=\"right\" colspan=\"2\" width=\"300\" valign=\"top\">$qty_insert at {$d['workshop_price']} each: &nbsp;</td><td valign=\"top\"> \${$qty_total}</td></tr>";
	}
}


echo <<<EOD
<table>
$output
</table>
EOD;

Does that do it?

Edited by diafol: n/a

0

I don't know if my brain's just fried. But I'm obviously not getting this at all. I'm wayyyyyy over my paygrade.

I got this:

Warning: Invalid argument supplied for foreach() in /home/workshop/public_html/register2.php on line 124

Warning: implode() [function.implode]: Invalid arguments passed in /home/workshop/public_html/register2.php on line 129

Warning: implode() [function.implode]: Invalid arguments passed in /home/workshop/public_html/register2.php on line 130

888

Here's the whole darned page just so you can see where I'm at. I tried incorporating that, and I don't know what I've done. :(

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<div id="container">




      <p><!-- start central content area -->


<br><br>

  <b><font face="georgia" size=5em>Step 2 - SubTotal/Discount</font></b>
  <br>


            <form method="post" action="register3.php">
            <table border="0"><tr><td></td><td></td><td></td><td></td></tr>
            <tr><td colspan=4> <br><hr style="color: #000;background-color: #000;height: 3px;"><br> </td></tr>

<?php
// connect to database 
include("dbconnection.php"); 
$fname = mysql_escape_string($_POST['fname']);
$lname = mysql_escape_string($_POST['lname']);
$address = mysql_escape_string($_POST['address1']);
$address2 = mysql_escape_string($_POST['address2']);
$city = mysql_escape_string($_POST['city']);
$state = mysql_escape_string($_POST['state']);
$zip = mysql_escape_string($_POST['zip']);
$phone = mysql_escape_string($_POST['phone']);
$fax = mysql_escape_string($_POST['fax']);
$email = mysql_escape_string($_POST['email']);
$hear = mysql_escape_string($_POST['hear']);
$how1 = mysql_escape_string($_POST['how1']);
$how2 = mysql_escape_string($_POST['how2']);
$how3 = mysql_escape_string($_POST['how3']);
$how4 = mysql_escape_string($_POST['how4']);
$res = mysql_escape_string($_POST['resident']);
if( $res == NULL ) { 
    $res="No";        
} 
$how = $how1 . $how2 . $how3 . $how4;
            $query_insertItem = "INSERT INTO tbl_registration (reg_fname, reg_lname, reg_address, reg_address2, reg_city, reg_state, reg_zip, reg_phone, reg_fax, reg_email, reg_how, reg_how_detail, reg_dc) VALUES ('$fname', '$lname', '$address', '$address2', '$city', '$state', '$zip', '$phone', '$fax', '$email', '$hear', '$how', '$res')";
            $dberror = "";
            $ret = mysql_query($query_insertItem);
                $reg_id = mysql_insert_id(); 
                $id_reg = $reg_id;
            // store all posted intemnos and descriptions in local arrays

$filtered = array_filter($_POST['participantqty']);

    sort($filtered);
$reg_id = '888';

    foreach($array as $k=>$v){
    $vals[] = "('$reg_id','$k',$v)";
    $keys[] = $k;
    }

    $valstring = implode(",",$vals);
    $keystring = "'" . implode("','", $keys) . "'";

    $q = mysql_query("INSERT INTO tbl_registration_workshop (registration_id, workshop_id, regworkshop_qty) VALUES $valstring");

    $q = mysql_query("SELECT * FROM tbl_workshops WHERE workshop_id IN($keystring) ORDER BY workshop_id");
    if(mysql_num_rows($q)){
    $output = "";
    while($d = mysql_fetch_array($q)){
    $qty_insert = $filtered[$d['workshop_id']];
    $qty_total = $qty_insert * $d['workshop_price'];
    $output .= "<tr><td valign=\"top\"><b>Description:</b> {$d['workshop_title']}</td><td align=\"right\" colspan=\"2\" width=\"300\" valign=\"top\">$qty_insert at {$d['workshop_price']} each: &nbsp;</td><td valign=\"top\"> \${$qty_total}</td></tr>";
    }
    }


echo "<table>".$reg_id.""




?>

  <tr><td colspan="3" align="right" ><b>Discount code? &nbsp;</b><br></td><td> <input type="text" name="discount" style="width:35px;" /></td></tr>
  <tr><td colspan="4"><br><hr style="color: #000;background-color: #000;height: 1px;"><br></td></tr>
  <tr><td colspan="2"> <b>Would you like to make a donation?</b> Please put an amount in the following box, if you wish to contribute to the Workshops for Youth and Families Scholarship Fund. (The grand total of the registration will be recalculated at checkout preview.)</td><td></td>
  <td><input type="text" name="donation" style="width:35px;"/></td>  </tr>

<tr><td></td><td></td><td></td><td></td></tr>
  <tr><td colspan="4"><br><hr style="color: #000;background-color: #000;height: 3px;"><br></td></tr>
<tr><td></td><td></td><td>Total: </td><td>$<?= $total ?></td></tr>
<tr><td colspan="4"</td><br><br></td></tr>
<tr><td colspan=2></td><td colspan=2 align=right><input type="hidden" name="reg_id" value="<?= $id_reg ?>" />
<input type="hidden" name="total" value="<?= $total ?>" />
<input type="submit" value="Checkout now!" /></form></td></tr>
</table><br /><br />

<font color=#999999>DISCOUNT CODES:<br /><br />
$10 off afternoon workshops = SW+1<br />
$50 off summer workshops if registered by Jan. 31 = Jan50<br />
$25 off summer workshops if registered by Feb. 14 = 25VD<br /></font>    

      <!-- end central content area -->

<br /><br />

Edited by mike_2000_17: Fixed formatting

0

sorry change from:

$reg_id = '888';
 
foreach($array as $k=>$v){

to:

foreach($filtered as $k=>$v){

I just hard-coded $reg_id - you've already got this, so take out the $reg_id = 888

0

wait!... maybe I've got it? taking out $reg_id = 888 took away errors. but then it printed nothing.

It looks like the Sort function was clearing the keys which match the class id's. Without that, it seems to work? I'm gonna test it a little to see.

0

yep.. it's good! Now I just gotta total the cost totals from each class into an overall total. I'm passing that to my next page... which thankfully is already working just fine.

Is there a simple way to total all the $qty_total's? I've figured out how to echo out the total of students. I just have to figure out this last tidbit.

0

For totals:

$student_total = array_sum($filtered);
        $running_total = 0;
        while($d = mysql_fetch_array($q)){
		$qty_insert = $filtered[$d['workshop_id']];
		$qty_total = $qty_insert * $d['workshop_price'];
                $running_total += $qty_total;
		$output .= "<tr><td valign=\"top\"><b>Description:</b> {$d['workshop_title']}</td><td align=\"right\" colspan=\"2\" width=\"300\" valign=\"top\">$qty_insert at {$d['workshop_price']} each: &nbsp;</td><td valign=\"top\"> \${$qty_total}</td></tr>";
	}
...



echo "total students : $student_total . <br />total cost : $running_total";
0

aha! $total = $total + $qty_total;

done! Thank you thank you thank you!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.