I am trying to create an order entry form with 4 form post fields $quantity, $ description, $amount and $unitprice. The form will do a PHP_SELF load and all the 4 fields above will be store in a multidimensional array when user pressed the submit button. The multidimesional array looks like $cart[$oid][$cartno[oid]] [0 to 4].
The first level array $cart[$oid] is identify each unique order id. The second level $cart[$oid][$cartno[$oid]] is to store the serial numbers (example, if 3 items was ordered, then the number would begins from 1 and increment by 1 until it reach 3. The purpose is to identify the three different items ordered.) The last level, is to store the four form variables.
Whenever the user fill in the form and press the submit button, the form will reload again with the data entered just now being shown at a table below the entry form. However, I',m facing a problem where whenever I first enter data and submit the form, nothing appears except this error message(Warning: Undefined offset: 0 in C:\project\uno\orderdetail.php on line 304). When i proceed to enter another data and submit, everything seems fine except the previous record was not shown.
Any help would be highly appreciated. I enclosed my sample code below.
<?php
session_start();
if (session_is_registered("username"))
{
include("C:\project\uno\db.inc"); //connection to DB
if (Isset($insert)) //hidden variable in form indicating submission
{
if (Isset($oid)) //unique orderid
{
if ((!session_is_registered("cart")) AND (!session_is_registered("cartno")))
{
$cartno=array($oid=>1);
session_register("cartno");
$cart=array($oid=>array($cartno[$oid]=>array()));
session_register("cart");
}
else
{
if ((($quantity!="") AND ($description!="")) AND (($amount!="") or ($unitprice!="")))
{
if ($unitprice=="")
{
$unitprice=round(($amount/$quantity),2);
}
if ($amount=="")
{
$amount=round(($unitprice*$quantity),2);
}
$quantity=number_format($quantity);
$amount=number_format($amount,2);
$unitprice=number_format($unitprice,2);
$cart[$oid][$cartno[$oid]]= array($quantity, $description, $unitprice, $amount);
$cartno[$oid]=$cartno[$oid]+1;
}
else
{
print("Make Sure to fill in either the unit price or amount field");
}
}
}
}
}
if (session_is_registered("username"))
{
?>
HTML CODE TO DISPLAY THE FORM
<?php
}
if (Session_is_registered("cart"))
{
$num=count($cart[$oid]);
if ($num>0)
{
print("<TABLE Border=\"1\">");
print("<TR>");
print("<TH>");
print("Quantity");
PRINT("</TH>");
print("<TH>");
print("Description");
PRINT("</TH>");
print("<TH>");
print("Unit Price");
PRINT("</TH>");
print("<TH>");
print("Amount");
PRINT("</TH>");
print("</TR>");
for ($row=1; $row<=$num; $row++)
{
print("<TR>");
for ($row2=0; $row2<=3; $row2++)
{
//while (list($key, $value)=each($cart[$oid][$row][$row2]))
//{
print("<Td>");
print($cart[$oid][$row][$row2]);
//print("$value");
print("</Td>");
//}
}
print("</TR>");
}
print("</Table>");
}
}
?>