Documentation:

Back
phpchartPlus Documentation
Tsiavos Chris <[email protected]>
Date: 18-Feb-2005
License: GPL
3.2.4 Using the PEARDBData Strategy
requires PEAR DB
To use the PEARDBData Strategy you have to follow 4 steps
1. Include the appropriate file
2. Initialize the PEARDBData Strategy class
3. Strategize the DataParser instance with the PEARDBData Strategy instance passing as a parameter the array of database connection parameters
4. Pass the SQL database query as a parameter to the strategized DataParser instance

To specify the database connection parameters you need an 8-slot associative array with the following keys defined:

- DB_Type: the name of the database server (mysql,pgsql etc)
- DB_Username: the database username
- DB_Passwd: the database user password
- DB_Host: the DNS name or ip number of the database server
- DB_Name: the name of the database
- Field_GroupName: the name of the table field holding the group name
- Field_ItemName: the name of the table field holding the item name of the above group
- Field_ItemValue: the name of the table field holding the item value

The SQL query can be specified with a one-slot associative array:
array("Query"=>"SELECT * FROM my_table")

To make things clear let's see an example:
Suppose you have the following database table and want to graph its contents:

In this case to generate the chart properly you have to specify the following attributes:

Field_GroupName: Hour
Field_ItemName:
Host
Field_ItemValue: Traffic
SQL Query:
SELECT * FROM rc_stat
code portion from file: "Examples/example.php"
include("../Sources/phpchartPlus.php");
include("../Sources/CacheToFile_Strategy.php");

include("../Sources/PEARDBData_Strategy.php");
include("../Sources/XMLConfig_Strategy.php");

$DataParams=array("DB_Type"=>"", "DB_Username"=>"", "DB_Passwd"=>"", "DB_Host"=>"", "DB_Name"=>"", "Field_GroupName"=>"", "Field_ItemName"=>"", "Field_ItemValue"=>"");

$ConfigParams=array("filename"=>"../Data/phpchartplus.conf.xml");

$DataStrategy=new PEARDBData_Strategy();
$ConfigStrategy=new XMLConfig_Strategy();
$CacheStrategy=new CacheToFile_Strategy();
$im=new phpchartPlus();
                   
try
{
$im->DataParser_->strategize($DataStrategy,$DataParams);
$im->DataParser_->parse(array("Query"=>"SELECT * FROM my_table"));
}
   catch (DataParser_Exception $e) 
   {
     print $e->getMessage();
     exit();
   }
   catch (DataStrategy_Exception $f) 
   {
     print $f->getMessage();
     exit();
   }
   
try
{
$im->ConfigParser_->strategize($ConfigStrategy,$ConfigParams);
$im->ConfigParser_->parse();
}
   catch (ConfigParser_Exception $e) 
   {
     print $e->getMessage();
     exit();
   }
   catch (ConfigStrategy_Exception $f) 
   {
     print $f->getMessage();
     exit();
   } 
   
try
{
$im->get_Instance();
}
   catch (ImageSupport_Exception $e)
   {
     print $e->getMessage();
     exit();
   }
   catch (FontSupport_Exception $f)
   {
     print $f->getMessage();
     exit();
   }
   
$im->Chart_->set_CachingStrategy($CacheStrategy,2); 
//Cache for 2 minutes 
$im->Chart_->draw_Chart();
               
 

 
Hosted by www.Geocities.ws

1