Product Documentation

FairCom SQL for PHP PDO

Previous Topic

Next Topic

Manage

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");

if (!$ses->beginTransaction())

Handle_Error($ses, "beginTransaction()");

// populate the tables with data

Add_CustomerMaster_Records($ses);

Add_ItemMaster_Records($ses);

if (!$ses->commit())

Handle_Error($ses, "commit");

Add_Transactions($ses);

// display the orders and their items

Display_CustomerOrders($ses);

Display_OrderItems($ses);

}

//

// 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 = $ses->exec("INSERT INTO custmast VALUES $values");

if (!$qry)

Handle_Error($ses, "exec(INSERT)");

}

}

//

// 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 = $ses->exec("INSERT INTO itemmast VALUES $values");

if (!$qry)

Handle_Error($ses, "exec(INSERT)");

}

}

//

// 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");

if (!$ses->beginTransaction())

Handle_Error($ses, "beginTransaction()");

$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 = $ses->exec("INSERT INTO custordr VALUES (

'$order[ordrdate]',

'$order[promdate]',

'$order[ordrnumb]',

'$order[custnumb]')");

if (!$qry)

Handle_Error($ses, "exec(INSERT)");

foreach ($items as $item) {

// process order items

if ($item['ordrnumb'] == $order['ordrnumb']) {

$qr2 = $ses->exec("INSERT INTO ordritem VALUES (

$item[sequnumb],

$item[quantity],

'$item[ordrnumb]',

'$item[itemnumb]')");

if (!$qr2)

Handle_Error($ses, "exec(INSERT)");

}

}

}

if (!$ses->commit())

Handle_Error($ses, "commit");

}

//

// Delete_Records()

//

// This function deletes all the records in a tables

//

function Delete_Records ($ses, $table) {

print("\t\tDelete records...<br>\n");

$qry = $ses->exec("DELETE FROM $table");

if (!$qry)

if (0000 == $ses->errorCode())

return;

else

Handle_Error($ses, "exec(DELETE)");

}

//

// Display_CustomerOrders()

//

// This function displays the contents of CustomerOrders table.

//

function Display_CustomerOrders ($ses) {

print("\t\tCustomerOrders Table...<br>\n");

$qry = $ses->query("SELECT co_ordrnumb \"Order\", co_custnumb \"Customer\" FROM custordr");

if (!$qry)

Handle_Error($ses, "query(SELECT)");

else {

print("\t\t<TABLE border=1>\n\t\t\t<TR><TH>Order</TH><TH>Customer</TH></TR>\n");

foreach ($qry as $row)

print "\t\t\t<TR><TD>" . $row['Order'] . "</TD><TD>" . $row['Customer'] . "</TR>\n";

print("\t\t</TABLE>\n");

if ($qry->errorCode())

Handle_Error($qry, "fetch()");

$qry = null;

}

}

//

// Display_OrderItems()

//

// This function displays the contents of OrderItems table.

//

function Display_OrderItems ($ses) {

print("\t\tOrderItems Table...<br>\n");

$qry = $ses->query("SELECT oi_ordrnumb \"Order\", oi_itemnumb \"Item\" FROM ordritem");

if (!$qry)

Handle_Error($ses, "query(SELECT)");

else {

print("\t\t<TABLE border=1>\n\t\t\t<TR><TH>Order</TH><TH>Item</TH></TR>\n");

foreach ($qry as $row)

print "\t\t\t<TR><TD>" . $row['Order'] . "</TD><TD>" . $row['Item'] . "</TR>\n";

print("\t\t</TABLE>\n");

if ($qry->errorCode())

Handle_Error($qry, "fetch()");

$qry = null;

}

}

TOCIndex