If you have an order table:
orders(orders_id , company_id (FK) , order, order_date_time , session_id)
Then you have a company table,
company (c_id, Company_name)
You have a table, order_definition:
definition_id (PK)
company_id (FK)
number_of_orders ----- how many order textfields needed
You have the "number_of_orders" the orders number, for example 6 ...
$number_of_orders=6;
You have a "definition_id" , for example: 112
create the textfields like this:
for($i=0; $i<$number_of_orders, $i++)
{
print ("<input type=\"text\" name=\"order{$definition_id}[$i]\"> \r" );
}
Now, store the definition_id into a hidden form, or a session... I store it in a hidden for example:
print "<input type=\"hidden\" name=\"def_id\" value=\"$definition_id\"> <br /> \r";
On the process page, select how many orders you expect using the previously stored definition_ID value, like:
$def_id=(int)$_POST["def_id"]; //this is the hidden field value
$sql="SELECT
order_definition.definition_id, order_definition.company_id, order_definition.number_of_orders
FROM
test4.order_definition
WHERE
order_definition.definition_id = '$def_id' ";
use the mysql fetch_assoc to get these value.
If that definition ID is exists in the database,
use a foreach cycle to get the values, like this:
$i=1;
foreach($_POST["order".$def_id] AS $row)
{
print ("Order :" . htmlspecialchars($row) ); /* Just for test*/
$sql= sprintf("INSERT INTO orders(definition_id , order , session_id) VALUES (%d , %s , %s)" , $def_id , mysql_real_escape_string($row) , session_id() );
print $sql; // JUST FOR TEST...
mysql_query($sql) or $error[]="Error with the $i. order of $def_id definition : ". htmlspecialchars($row)." - from: " . session_id() ;
$i++;
}
if(isset($error))
{
foreach ($error AS $rows)
{
/*mail_to_the_adminl($rows);*/
print $rows . "<br />";
}
}
And how to list the orders joining with the company, orders , and orders_definition table?
Click here to see:
http://phptool.extra.hu/orders.jpg
SELECT
`company`.`c_id` AS `c_id`, `company`.`Company_name` AS `Company_name`, `orders`.`orders_id` AS `orders_id`, `orders`.`order` AS `order`, `orders`.`order_date_time` AS `order_date_time`
FROM
((`company` JOIN `orders`) JOIN `order_definition` ON ((`order_definition`.`company_id` = `company`.`c_id`)))
ORDER BY
`company`.`Company_name`, `orders`.`order_date_time` DESC