Sunday, January 13, 2013

php mysql ghraph chart example

PHPGraphLib Tutorial #2: MYSQL and PHPGraphLib

Overview

 

In this example, we are going to show a simple example of how to use PHPGraphLib with MYSQL.

Include the source file in a new script and create the PHPGraphLib Object

Include the source PHPGraphLib class file at the top of your stand alone php script. This script will only include the PHP calls to create your graph. Next, create a PHPGraphLib object.





<?php
include("phpgraphlib.php");
$graph=new PHPGraphLib(550,350);

Add data from MYSQL

We are going to query our database, add that data to an array, and then pass that information to PHPGraphLib. The sales count will be used as y-axis values, and the sales group name will be used as the x -axis values. See http://www.php.net/mysql for more information on how to connect to your MYSQL server with PHP. You will need to change the system dependent variables, such as username, password, database, and table name when using this example.



$link = mysql_connect('localhost''username''password')
or die('Could not connect: ' . mysql_error());     
mysql_select_db('salestracking'or die('Could not select database');
$dataArray=array(); 
//get data from database
$sql="SELECT salesgroup, COUNT(*) AS 'count' FROM sales GROUP BY salesgroup";
$result = mysql_query($sqlor die('Query failed: ' . mysql_error());
if ($result) {
  while ($row = mysql_fetch_assoc($result)) {
      $salesgroup=$row["salesgroup"];
      $count=$row["count"];
      //add to data areray
      $dataArray[$salesgroup]=$count;
  }
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("Sales by Group");
$graph->setGradient("lime""green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>

Include the created script as the src of an image on the desired page

On the page you want the graph to appear, create an HTML image tag and set the src="" to the location of the script you created above.


<html>
<h3>This is where I want to display my graph</h3>
<img src="mysql_graph_bar.php" />
</html>

Here is the graph you just created:
For other examples, view the Examples page

Saturday, January 12, 2013

php cybersource payment gateway integration

This PHP class can be used to process credit card payments via Cybersource.

It can send HTTP requests to Cybersource SOAP Web services API server to  perform several types
of operations to process credit card payments.

Currently it can request a payment authorization, capture the result of a  payment request and
request the reversal of a previous payment request.

This class would require php_soap and php_openssl extensions.

Code Example: Download Example

index.php
<?php
/**
 * Test script to show how the EPS_CYBERSOURCE class can be used.
 */
 
 require 'class.eps_cybersource.php';
 
/**
 * These should go in a config file somewhere on the box.
 */
 $trans_key = 'your SOAP transaction key';
 $merchant_id = 'your merchant id';
 $url = 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.53.wsdl';
 
/**
 * These are sent from some GUI and assembled into the applicable arrays.
 */
 $bill_array = array('firstName'=>'John','lastName'=>'Doe','street1'=>'1295 Charleston Road',
                     'city'=>'Mountain View','state'=>'CA','postalCode' => '94043','country'=>'US',
                     'email'=> 'null@cybersource.com','ipaddress'=>'10.7.111.111');
 $card_array = array('accountNumber'=>'4111111111111111','expirationMonth'=>'12',
                     'expirationYear'=>'2020','cvNumber'=>'123');
 $item_array = array(
 array('unitPrice'=>'.50','quantity'=>2,'productName'=>'product one'),
 array('unitPrice'=>'2.5','quantity'=>1,'productName'=>'product two'));
 $custom_array = array('one','two','three','four');
 
/**
 * Authorize a transaction.
 */
 try
 {
 $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key);
 $soap->setMerchantDefinedData($custom_array);
 $soap->setReferenceCode(array('CSTEST','YYYY','J','-','RNDM'));
 $soap->setCCRequest($bill_array,$card_array,$item_array);
 $soap->ccAuthorize();
 }
 catch (SoapFault $e)
 {
 exit($e->getMessage());
 }
 
 print_r($soap->reply);
 
/**
 * Capture the successful authorization.
 * A single ccCapture() could have been done instead of a ccAuthorize() followed by a ccCapture().
 */
 if ($soap->success) $soap->ccCapture();
 
/**
 * These return values would be stored locally.
 */
 $tok = $soap->reply->requestToken;
 $id = $soap->reply->requestID;
 $rc = $soap->reply->merchantReferenceCode;
 $amount = $soap->reply->amount;
 $currency = $soap->reply->currency;
 
 print_r($soap->reply);
 
 $trans_array = array('requestToken'=>$tok,
 'requestID'=>$id,
 'referenceCode'=>$rc,
 'amount'=>$amount,
 'currency'=>$currency);
 
/**
 * Reverse the capture or authorization.
 */
 if ($soap->success)
 {
 unset($soap);
 try
 {
 $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key);
 $soap->setCCReversalRequest($tok,$id,$rc,$amount);
 $soap->ccReverse('c');
 }
 catch (SoapFault $e)
 {
 exit($e->getMessage());
 }
 }
 
 print_r($soap->reply);   
 
/**
 * Credit the account.
 */
 if ($soap->success)
 {
 unset($soap);
 try
 {
 $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key);
 $soap->setCCCreditRequest($bill_array,$card_array);
 $soap->setReferenceCode(array('CR','YYYY','J','-',array('RNDM',5,5)));
 $soap->ccCredit('2.53');
 }
 catch (SoapFault $e)
 {
 exit($e->getMessage());
 }
 }
 
 print_r($soap->reply);   
 
/**
 * Get some help on the XML schema.
 */
 print_r($soap->getHelp());
 
 echo "current version: " . $soap->getHelpVersion() . "\n";
 
 print_r($soap->getHelp('item'));
 
 unset($soap);
 
?>