I want to know how will I pass this table values using a form.

I have a list of products and each item belongs to a section and each section has different attributes. The attributes section are columns where the user can input a number of products he wants to buy that has the attribute.


Ex. I fetched this already in the database and have this output in a table.The forms looks like this.

Cellphones:
Item# Description Price Red Blue Total Unit Total
HOR-LPR Nokia N93 $500 1 3 4 $2000
Grand Total $2000

Game Consoles
Item# Description Price Red Black Pink Total Unit Total
OKL-JK Sony PSP $100 2 0 5 7 $700
YTL-OP Sony PS3 $200 1 0 0 1 $200
Grand Total: $900
Here's my code.

function displayOrderList() //function to display all product information in order form
	{
		global $database;
		echo "<table class=sectionTable>";
		$sections = $database->getSections();
		
		foreach($sections as $section)
		{
			$section_id = $section['section_id'];
			$section_name = $section['section_name'];
			$section_description = $section['section_description'];
		echo "
			<tr>
				<td id=section_name><h1 id='$section_name'>".$section_name."</h1></td>
			</tr>
			<tr>
				<td>
					<table border=1 class=itemTable cellspacing=0px cellpadding= 0px>
					<tr>
						<th>ITEM CODE</th>
						<th>DESCRIPTION</th>
						<th>UNIT PRICE</th>";
						//display attributes columns for the section
						$attributes = $database->getAttributes($section_id);
						foreach ($attributes as $attribute)
						{
							$attribute_name=$attribute['attribute_name'];
							echo "<th>".$attribute_name."</th>";
						} //end of attribute headers
						
					echo "
						<th>TOTAL UNIT</th>
						<th>TOTAL $</th>
					</tr>";
						$items = $database->getItems($section_id);
					 	$row = 0;
						foreach ($items as $item) //loop for product info
					 	{
					 		$item_id = $item['item_id'];
					 		$item_number = $item['item_number'];
					 		$item_description = $item['item_description'];
					 		$item_price = $item['item_price'];
					 		
							//set unique id for each input
					 		$item_id = "item".$section_id."".$row;
							$item_code = "item".$section_id;
					 		$desc_id = "desc".$section_id."".$row;
					 		$desc_name = "desc".$section_id;
					 		$price_id = "price".$section_id."".$row;
					 		$price_name = "price".$section_id;
					 		$unit_id = "unit".$section_id."".$row;
					 		$unit_name = "unit".$section_id;
					 		$total_id = "total".$section_id."".$row;
					 		$t_title = "total".$section_id;
					 		$overall = "overall".$section_id;
					 		$title;
					 			 		
					 	echo "
					 	<tr>
					 	<td>".$item_number."</td>
					 	<td id=description>".$item_description."</td>
					 	<td>".$item_price."</td>";
					 	$n = 0;	
					 	while ($n < sizeof($attributes)) //loop for attribute inputs
						{
						$attrib_id = "qty".$n."".$section_id."".$row;
						$attrib_name = "qty".$n."".$section;
						$title = "qty".$section_id."".$row;
						echo "<td><input type=\"text\" size=4 name='$attrib_name'[] id='$attrib_id' title=\"".$title."\" value=\"0\" onkeyup=\"set_total(this.id,this.title,'$price_id','$unit_id','$total_id','$t_title','$overall')\"/></td>";
						$n++;
						} //end of attribute loop
						echo "
						<td>																																			
						<input type=\"text\" readonly class=\"product\" id=\"".$unit_id."\" name='$unit_name'[]  value=\"0\" />
						</td>
						<td>
						<input type=\"text\" readonly class=\"product\" title=\"".$t_title."\" id=\"".$total_id."\" name='$t_title'[] value=\"0\" />
						</td>
					 	</tr>
					 	<input type=\"hidden\" name='$item_code'[] id=\"".$item_id."\" value=\"".$item_number."\" />
						<input type=\"hidden\" name='$desc_name'[] id=\"".$desc_id."\" value=\"".$item_description."\" />
						<input type=\"hidden\" name='$price_name'[] id=\"".$price_id."\" value=\"".$item_price."\" />";
						$row++;
						}//end of product loop
					$colspan = 4 + sizeof($attributes);	
					echo "
						<tr>
						<td colspan=".$colspan." align=right>Grand Total:</td>
						<td>
						<input type=\"text\" readonly class=\"product\" name='overall[]' id='$overall' value=\"0\" />
						</td>
						</tr>
					</table>
				</td>
			</tr>";
		} //end of section loop
		echo "</table>";
	}

the code above is working I can display all the sections with its corresponding attributes and products... now I'm confused how can I pass all the values here to another page..(note: this is inside a form already) Can you give me some advice so I can pass all the values here?

setcookie() ?
session_start()
Are a couple of ways to do it.

I would create a class in PHP for collecting information from the database and store it in an external file called classes.php.

I'd then pass the inputted information from the forms onto other pages using either setcookie() or sessions. I'd then include my classes.php and query the database again.

You can learn about setcookie here: http://php.net/manual/en/function.setcookie.php
You can learn about sessions here: http://php.net/manual/en/features.sessions.php

Holding all that information in a cookie/session wouldn't be very efficient. I'd recommend doing as I suggest.

Hope this helps :)

I've done it using multidimensional array

Ex. I want to pass all product item code

<input type=hidden name=product[$section][$row][item_code] value=$item_code />

then submit form get the data through

$product =$_POST['product'];

print value
start loop for section
   start loop for row
      echo $product[$section][$row]['item_code'];
Member Avatar for rajarajan2017

Sample:

<?php 
	function createTextBoxes($the_array, $n){
		for($i = 0; $i < $n; $i++)
		 echo "<input type=\"text\" name=\"inp$i\" size=\"3\">";
	}
?>
<?php
$resArray = array_fill(0, 5, 1);
for($i = 0; $i < 5; $i++)
	$resArray[$i] = $_POST['inp'.$i];
print_r($resArray);
?>

make that array_fill value to your number of rows returned
Name your textbox as an inp1, inp2, inp3... formed by the loop of textbox page.

Rajarajan07 solution is very effective however I fail to see the need to do this?
Why not just query the database again?

:S

I did query the database again for informations that can be found in the database but values like total,number of products ordered for each product attributes and grand total needs to be pass. Well everything works now yes rarajan's solution works but I don't like the idea of having different names for input so I just created a multidimensional array called products and get all the values there.

Could you not use mySQL count to query again and get total values ETC?
For example say this was your database:

ID NAME ITEM PRICE
1 Ford CAR £4000
2 Mercedes CAR £2000
3 Ford CAR £3000

You could write this to find the total numbers:

SELECT NAME, COUNT(ITEM) FROM products GROUP BY NAME

By then writing this:

while($row = mysql_fetch_array($YOURRETURNEDSQL)){
	echo "There are ". $row['COUNT(NAME)'] ." ". $row['ITEM'] ."'s in the database.";
	echo "<br />";
}

This would then return:

There are 3 car's in the database


Just an idea, and sorry if any of this is wrong Im typing from an iPod! Ew!

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.