Form elements and php with mysql problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Form elements and php with mysql problem

Post by mikebr »

I am trying to write a form that takes it's contents from various tables in a database so users can select items listed on the form and then post that form to me in an email with a list of the items selected. The database has the following tables: cameras, lenses, filters, grip, video and each table has the column values of ID, type and item, if I place the elements into a form called using the following script to populate the form (reduced code):

Code: Select all

<?php
//$list is the table , cameras, lenses etc.
$sql2="SELECT * FROM $list ORDER BY type, type, item";

while ($row = mysql_fetch_array($mysql_rental_result)) { // ---------- OPEN WHILE
	
	$num == $num++;
		
	if($list=="cameras") {
	$CID = $row["CID"];
	$type = $row["type"];
	$item = $row["item"];
		if($c==1) {
		include "$path_tables"."inc_camorder_table1.htm"; // LOAD IF 1 TABLE ONLY
		}	
		if($c>0) {
		include "$path_tables"."inc_camorder_table2.htm"; // LOAD THE LAST TABLE
		}
	}		
	if($list=="lenses") {
	$LID = $row["LID"];
	$type = $row["type"];
	$lens = $row["lens"];
	$item = $row["item"];
		if($c==1) {
		include "$path_tables"."inc_lenorder_table1.htm"; // LOAD IF 1 TABLE ONLY
		}		
		if($c>0) {
		include "$path_tables"."inc_lenorder_table2.htm"; // LOAD THE LAST TABLE
		}
	}				
	if($list=="video_assist") {
	$VID = $row["VID"];
	$type = $row["type"];
	$item = $row["item"];
		if($c==1) {
		include "$path_tables"."inc_vidorder_table1.htm"; // LOAD IF 1 TABLE ONLY
		}
			
		if($c>0) {
		include "$path_tables"."inc_vidorder_table2.htm"; // LOAD THE LAST TABLE
		}
	}
	$ud = ""; // Clear the ud variable
	$c++;
}

?>
OK so the items are now in the form but how do these get passed on or rather how can I reference each item that is listed? as the number of items can be "more than" or "less than" 300 items depending on the number of items in each table. For example if I have a form with the users name, address and age then I can reference each of these items with a variable for $name, $address and $age but how can I do this when the items are numerous "possibly over 300" and taken from various MySQL database tables? I know some basic php so sending the results in the email is not a problem but when I got to this "referencing each item in the list" I became totally stumped.

Many thanks in advance for any replies :cry:
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

You could try doing a foreach loop through $_POST, assuming that you use post to send your form. Hope that helps.
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

I seem to have sort of got this together, reading the mysql tables and entering them into the forms seem to be OK but I have a bit of a problem on returning the values of the variables.

(Brief form explanation: the form loads single line tables with a checkbox "called select1, select2, select3 etc depending on number of items in the table", type value also with hidden field "called type1, type2 etc" and item value also with a hidden field "called item1, item2 etc.)

The code below is what I am using to get the values from the form on submit.....

Code for getting the control values:

Code: Select all

<?php
if($theform=="cameras") {
$cb = 1;
$selectcb = "select"."$cb"; // Checkbox names cb + number
while ($cb <= $results) { // number of $results from query
		if($selectcb) { // If checkbox selected
		$typeentry = "$"."type"."$cb"; // **************************
		$itementry = "$".item"."$cb"; // item value
		$units = "$"."qt"."$cb"; // Quantity value

			$CAMERALIST[$cb] = "$units"." $typeentry "." $itementry";
		}
		
		echo "$cb => $CAMERALIST[$cb] <br>"; // Test print checking
		$cb++;
	}	
}
?>
Code for setting the control names in the form:

Code: Select all

<?php
while ($row = mysql_fetch_array($mysql_result)) { // ---------- OPEN WHILE
		
				$num++;
if($list=="cameras") {
				$type = $row["type"]; // the type from camera table
				$item = $row["item"]; // the item from camera table
				$qtname = "qt"."$num"; // the name for the quantity field
				$selectname = "select"."$num"; // the name for checkbox
				$qt = 1; // The default value for Quantity of items field
					if($num==1) {
					include "$path_tables"."inc_ord_table1.htm"; // LOAD IF 1 TABLE ONLY
					}
				
					if($num>0) {
					include "$path_tables"."inc_ord_table2.htm"; // LOAD THE LAST TABLE
					}
					echo "<INPUT TYPE="HIDDEN" NAME="list" VALUE="$list">";
					echo "<INPUT TYPE="HIDDEN" NAME="theform" VALUE="cameras">";
					echo "<INPUT TYPE="HIDDEN" NAME="thetype" VALUE="$thetype">"; // Only for 35mm, 16mm and/or video types
				}
?>


The problems are:
If I use the following to get the value of the variable called $typt1 the print out reads as $type1 which I expected but I didn't want this to print exactly as $type1 I expected iot to function as the variable $type1

Code: Select all

<?php
 $typeentry = "$"."type"."$cb"; // **************************
?>
but if I change the line to:

Code: Select all

<?php
 $typeentry = "$type1; // **************************
?>
I then get the value of the variable $type1 as I hoped, so as I don't know all the control names in advane as this is controled by the number of items in the table, how is it possible to write these variable so as they obtain the value as expected?

Another thing I have a problem with is the above code for Code for getting the control values is that the array seems to store an item for each control in the form and not just the values for the items of the checkbox, any ideas on what I am doing wrone here? I only want the values for the items checked.

It's late so I hope this is clear.
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

I had something similar where the user can select up to X number of checkboxes. I used the following code:

Code: Select all

for ($x = 1;$x < 15; $x++) {
      if (isset(${'cbx'.$x})) {
        $sql = "UPDATE bugs_lookups SET final='1' WHERE (id='".${'cbx'.$x}."')";
        if (!$result = mysql_query($sql, $db))
          die("Error modifying lookups table");        
      }
    }
Essentially what it does is loop through 15 times (which I've determined to be the max number of possible check boxes) and determines if $cbx1 through $cbx15 is set. 'cbx' is obviously what I was calling my check boxes.
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

Great,

This little line sorted it.....

Code: Select all

<?php
  if (isset(${'cbx'.$x})) { 
?>
Thanks for the help.
Post Reply