Manage() provides data management functionality for your application and/or process.
Below is the code for Manage():
//
// Manage()
//
// Populates table and perform a simple query
//
function Manage($ses) {
print("\t<h4>MANAGE</h4>\n");
// populate the tables with data
Add_CustomerMaster_Records($ses);
Add_ItemMaster_Records($ses);
Add_Transactions($ses);
// display the orders and their items
Display_CustomerOrders($ses);
Display_OrderItems($ses);
}
//
// Delete_Tables()
//
// This function removes all existing tables
//
function Delete_Tables ($ses) {
$qry = ctsql_query("DROP TABLE ordritem", $ses);
if (!$qry)
Handle_Error("ctsql_query(DROP TABLE)");
$qry = ctsql_query("DROP TABLE custordr", $ses);
if (!$qry)
Handle_Error("ctsql_query(DROP TABLE)");
$qry = ctsql_query("DROP TABLE custmast", $ses);
if (!$qry)
Handle_Error("ctsql_query(DROP TABLE)");
$qry = ctsql_query("DROP TABLE itemmast", $ses);
if (!$qry)
Handle_Error("ctsql_query(DROP TABLE)");
}
//
// Add_CustomerMaster_Records()
//
// This function adds records to table CustomerMaster from an
// array of strings
//
function Add_CustomerMaster_Records ($ses) {
print("\t\tAdd records in table CustomerMaster...<br>\n");
$data = array(
"('1000','92867','CA','1','Bryan Williams','2999 Regency','Orange')",
"('1001','61434','CT','1','Michael Jordan','13 Main','Harford')",
"('1002','73677','GA','1','Joshua Brown','4356 Cambridge','Atlanta')",
"('1003','10034','MO','1','Keyon Dooling','19771 Park Avenue','Columbia')"
);
foreach ($data as $values) {
$qry = ctsql_query("INSERT INTO custmast VALUES $values", $ses);
if (!$qry)
Handle_Error("ctsql_query(INSERT)");
}
if (!ctsql_commit($ses))
Handle_Error("ctsql_commit()");
}
//
// Add_ItemMaster_Records()
//
// This function adds records to table ItemMaster from an
// array of strings
//
function Add_ItemMaster_Records ($ses) {
print("\t\tAdd records in table ItemMaster...<br>\n");
$data = array(
"(10,19.95,'1','Hammer')",
"(3, 9.99,'2','Wrench')",
"(4, 16.59,'3','Saw')",
"(1, 3.98,'4','Pliers')"
);
foreach ($data as $values) {
$qry = ctsql_query("INSERT INTO itemmast VALUES $values", $ses);
if (!$qry)
Handle_Error("ctsql_query(INSERT)");
}
if (!ctsql_commit($ses))
Handle_Error("ctsql_commit()");
}
//
// Add_Transactions()
//
// Add an Order and associated Items "as a transaction" to their
// respective tables. A transaction is committed or aborted if the
// customer number on the order is confirmed valid. Likewise each
// item in the order is verified to be a valid item.
//
function Add_Transactions ($ses) {
print("\t\tAdd transaction records...<br>\n");
$orders = array(
array(
'ordrdate' => '09/01/2002',
'promdate' => '09/05/2002',
'ordrnumb' => '1',
'custnumb' => '1001'
),
array(
'ordrdate' => '09/02/2002',
'promdate' => '09/06/2002',
'ordrnumb' => '2',
'custnumb' => '9999' // bad customer number
),
array(
'ordrdate' => '09/22/2002',
'promdate' => '09/26/2002',
'ordrnumb' => '3',
'custnumb' => '1003'
)
);
$items = array(
array(
'ordrnumb' => '1',
'sequnumb' => '1',
'quantity' => '2',
'itemnumb' => '1'
),
array(
'ordrnumb' => '1',
'sequnumb' => '2',
'quantity' => '1',
'itemnumb' => '2'
),
array(
'ordrnumb' => '2',
'sequnumb' => '1',
'quantity' => '1',
'itemnumb' => '3'
),
array(
'ordrnumb' => '2',
'sequnumb' => '2',
'quantity' => '3',
'itemnumb' => '4'
),
array(
'ordrnumb' => '3',
'sequnumb' => '1',
'quantity' => '2',
'itemnumb' => '3'
),
array(
'ordrnumb' => '3',
'sequnumb' => '2',
'quantity' => '2',
'itemnumb' => '99' // bad item number
)
);
foreach ($orders as $order) {
// add order record
$qry = ctsql_query("INSERT INTO custordr VALUES (
'$order[ordrdate]',
'$order[promdate]',
'$order[ordrnumb]',
'$order[custnumb]')",
$ses);
if (!$qry)
Handle_Error("ctsql_query(INSERT)");
foreach ($items as $item) {
// process order items
if ($item[ordrnumb] == $order[ordrnumb]) {
$qr2 = ctsql_query("INSERT INTO ordritem VALUES (
$item[sequnumb],
$item[quantity],
'$item[ordrnumb]',
'$item[itemnumb]')",
$ses);
if (!$qr2)
Handle_Error("ctsql_query(INSERT)");
}
}
}
if (!ctsql_commit($ses))
Handle_Error("ctsql_commit()");
}
//
// Delete_Records()
//
// This function deletes all the records in a tables
//
function Delete_Records ($ses, $table) {
print("\t\tDelete records...<br>\n");
$qry = ctsql_query("DELETE FROM $table", $ses);
if (!$qry)
if (100 == ctsql_errno())
return;
else
Handle_Error("ctsql_query(DELETE)");
if (!ctsql_commit($ses))
Handle_Error("ctsql_commit()");
}
//
// Display_CustomerOrders()
//
// This function displays the contents of CustomerOrders table.
//
function Display_CustomerOrders ($ses) {
print("\t\tCustomerOrders Table...<br>\n");
$qry = ctsql_query("SELECT co_ordrnumb \"Order\", co_custnumb \"Customer\" FROM custordr", $ses);
if (!is_resource($qry))
Handle_Error("ctsql_query(SELECT)");
else {
print("\t\t<TABLE border=1>\n\t\t\t<TR><TH>Order</TH><TH>Customer</TH></TR>\n");
while ($values = ctsql_fetch_row($qry)) {
print("\t\t\t<TR>");
foreach ($values as $content) {
if (is_null($content))
print ("<TD>NULL</TD>");
else
print ("<TD>$content</TD>");
}
print("</TR>\n");
}
print("\t\t</TABLE>\n");
if (ctsql_errno())
Handle_Error("ctsql_fetch_row()");
ctsql_free_result($qry);
}
}
//
// Display_OrderItems()
//
// This function displays the contents of OrderItems table.
//
function Display_OrderItems ($ses) {
print("\t\tOrderItems Table...<br>\n");
$qry = ctsql_query("SELECT oi_ordrnumb \"Order\", oi_itemnumb \"Item\" FROM ordritem", $ses);
if (!is_resource($qry))
Handle_Error("ctsql_query(SELECT)");
else {
print("\t\t<TABLE border=1>\n\t\t\t<TR><TH>Order</TH><TH>Item</TH></TR>\n");
while ($values = ctsql_fetch_row($qry)) {
print("\t\t\t<TR>");
foreach ($values as $content) {
if (is_null($content))
print ("<TD>NULL</TD>");
else
print ("<TD>$content</TD>");
}
print("</TR>\n");
}
print("\t\t</TABLE>\n");
if (ctsql_errno())
Handle_Error("ctsql_fetch_row()");
ctsql_free_result($qry);
}
}