Product Documentation

FairCom SQL for PHP

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 performs a simple query

//

function Manage($ses) {

print("\t<h4>MANAGE</h4>\n");

// populate the tables with data

Add_CustomerMaster_Records($ses);

Add_CustomerOrders_Records($ses);

Add_OrderItems_Records($ses);

Add_ItemMaster_Records($ses);

if (!ctsql_commit($ses))

Handle_Error("ctsql_commit()");

// perform a query:

// list customer name and total amount per order

// name total

// @@@@@@@@@@@@@ $xx.xx

// for each order in the CustomerOrders table

// fetch order number

// fetch customer number

// fetch name from CustomerMaster table based on customer number

// for each order item in OrderItems table

// fetch item quantity

// fetch item number

// fetch item price from ItemMaster table based on item number

// next

// next

$qry = ctsql_query(

"SELECT cm_custname \"Name\", SUM(im_itempric * oi_quantity) \"Total\"" .

"FROM custmast, custordr, ordritem, itemmast " .

"WHERE co_custnumb = cm_custnumb AND co_ordrnumb = oi_ordrnumb AND oi_itemnumb = im_itemnumb " .

"GROUP BY cm_custnumb, cm_custname",

$ses);

if (!is_resource($qry))

Handle_Error("ctsql_query(SELECT)");

else {

print("\t\t<TABLE border=1>\n\t\t\t<TR><TH>Name</TH><TH>Total</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);

}

}

//

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

);

Delete_Records($ses, "custmast");

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_CustomerOrders_Records()

//

// This function adds records to table CustomerOrders from an

// array of strings

//

function Add_CustomerOrders_Records ($ses) {

print("\t\tAdd records in table CustomerOrders...<br>\n");

$data = array(

"('09/01/2002','09/05/2002','1','1001')",

"('09/02/2002','09/06/2002','2','1002')"

);

Delete_Records($ses, "custordr");

foreach ($data as $values) {

$qry = ctsql_query("INSERT INTO custordr VALUES $values", $ses);

if (!$qry)

Handle_Error("ctsql_query(INSERT)");

}

if (!ctsql_commit($ses))

Handle_Error("ctsql_commit()");

}

//

// Add_OrderItems_Records()

//

// This function adds records to table OrderItems from an

// array of strings

//

function Add_OrderItems_Records ($ses) {

print("\t\tAdd records in table OrderItems...<br>\n");

$data = array(

"(1,2,'1','1')",

"(2,1,'1','2')",

"(3,1,'1','3')",

"(1,3,'2','3')"

);

Delete_Records($ses, "ordritem");

foreach ($data as $values) {

$qry = ctsql_query("INSERT INTO ordritem 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')"

);

Delete_Records($ses, "itemmast");

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

}

//

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

}

TOCIndex