Jump to content

array glitch in passing variable?


turpentyne

Recommended Posts

I have an issue with a form where people choose from several classes, and how many people they're enrolling. If they choose the first class, select the number of students and hit submit, it is supposed to carry the number of students to the 2nd page where a variable is created for it:

$qty = $_POST['participantqty'];

 

It works if they pick the first class and choose a number of students.

It works if they pick the first class and another, plus the number of students for each.

 

But if they choose any one class except the first one (and only that class), choose their number of students and submit...

This is when it doesn't carry the number forward...

 

It also glitches when they select two classes that don't include the first class. It carries the number of students for one, but not the other.

 

qty_insert and qty_total on page two are where the data dissappears. I'm assuming it has something to do with the array, but I'm in a little over my head.

 

On the second page, I added in print_r($myarray); and it shows the variable is passing to the second page.

 

page one:


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

<? //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));


    ?>

<input type="checkbox" name="workshop_id[]" value="<?= $c_row['workshop_id'] ?>" /> <?= $c_row['workshop_title'] ?> </td><td><?= $c_row['workshop_date'] ?></td><td><select name="participantqty[]" 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>
$ <?= $c_row['workshop_price'] ?></td></tr><tr><td colspan=3><hr></td></tr> 


  <?

  } //end while
  ?>

<input type="submit" value="Submit" class="buttontype">

 

and on page two where it totals things up:

 

<form method="post" action="register3.php">
<?php
// connect to database 
include("dbconnectionfile.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 
$ids = $_POST['workshop_id']; 
$qty = $_POST['participantqty'];
echo $qty;
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);
$qty_total=$qty_insert*$z_row['workshop_price'];
$total = $total + $qty_total;

    // the qty_insert and the qty_total below are what appear blank.

    if ($ids[$i] <> '') { 

echo "Description:</b> ". $z_row['workshop_title'] ."   " . $qty_insert . " at ". $z_row['workshop_price'] ." each:  </td><td valign=top> $". $qty_total ."<br>";

    } 
} 	
}			
?>

<input type="hidden" name="reg_id" value="<?= $id_reg ?>" />
<input type="hidden" name="total" value="<?= $total ?>" />
<input type="submit" value="Checkout now!" /></form>

 

Link to comment
Share on other sites

<input type="checkbox" name="workshop_id[]" value="<?= $c_row['workshop_id'] ?>" /> <?= $c_row['workshop_title'] ?> </td><td><?= $c_row['workshop_date'] ?></td><td><select name="participantqty[]" id="" class="">

 

Your relying on the indexes of workshop_id and participatqty being equal, but your code does not ensure that.  Take for example if you have 5 courses listed and the user checks the box for only the first and last (and selects those quantities).

 

$_POST['workshop_id'] will only contain two indexes: 0,1.  Reason is that only boxes that are checked are sent, and [] just appends the value on the next available numeric index.  So the first box becomes index 0, and the last becomes index 1.

 

$_POST['participatqty'] on the other hand, will contain all indexes from 0 to 4, with 1-3 being the empty string, and 0 and 4 being the users chosen values.  Empty select box values are sent so they use up the indexes as you'd expect.

 

What you need to do is manually specify your indexes so they always match up between the two arrays.  I great way to do this is to use the ID numbers for whatever the checkbox represents.  In this case, the course ID.

 

<input type="checkbox" name="workshop_id[]" value="<?= $c_row['workshop_id'] ?>" /> <?= $c_row['workshop_title'] ?> </td><td><?= $c_row['workshop_date'] ?></td><td><select name="participantqty[<?= $c_row['workshop_id'] ?>]" id="" class="">

 

<?php

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

Link to comment
Share on other sites

Forgive me... I seem to be confused on how to incorporate that into my second page... It's passing the variables attached to the id's just as you said, but  I can't figure out how to incorporate the foreach statement you showed: Here's what I've got so far -  I replaced $qty = $_POST['participantqty'];

with your snippet:

 

 

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

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] <> '') { 

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

    } 
} 	
}




?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.