Just another WordPress.com site

Posts tagged “PHP

PHP: pChart


I am finishing up the beginning course for PHP and was asked to write a blog on a related topic.  I came across an article from Smashing Magazine about 50 useful tools for PHP developers. You can find the article here.  (the article is from 2009, but many of the technologies are still being updated/supported today)

There were tools for everything.. Debugging, Testing/Optimization, Documentation, Security, Code Compression, Version-Control, etc.  While many of these would probably be far more useful, far more often for any PHP developer, the creative/designer side of me was drawn to the Graphical tools mentioned in the article.

The first one I read about was pChart. It is a “class oriented framework designed to create aliased charts.”  This tool allows you to pull data from SQL queries to populate and construct a graphical chart.  Some of the core features of pChart are:

  • Native anti-aliasing (for all basic objects)
  • Shadow support (drawn using the internal anti-alias algorithm)
  • Alpha-transparency (directly computed by GD binaries for performance)
  • Spline, cubic curves

pChart has created classes that allow you to “fully configure your series and axis” so the raw data is stored with clean and efficient code. The site provides documentation for these classes on their website. There are custom classes for:

  • Building your data series (addPoints)
  • Defining the name of X/Y Axis (setAxisUnit)
  • Define the way to show values (setAxisDisplay)
  • Bind a series to one axis (setSerieOnAxis)
  • Name axis (setAxisName)
  • Set position of axis (setAxisPosition)
  • and many more..

There are mutiple chart formats you can create with pChart. You can create a standard chart such as plot, line or curve charts.  You can also create bar and stacked bar charts, radar and polar charts, 2D and 3D pie charts, linear bubble charts and more!  Sample documentation is given for these individual chart types on the website as well.

Some of the extended functionalities of the pChart library are pSpring that allows you to create a visual rendition of a network, sandbox engine that allows you to design a chart and generate the code, and pCache that allows you to store a cached version of previously created charts (from SQL queries already generated by other users) and output it directly for better performance on frequently viewed data sets.

For more information or help on setting up pChart on your server and generating charts for use on your sites, visit pChart’s forum support section at wiki.pchart.net/forum/.

Sources: http://www.SmashingMagazine.com, http://www.pChart.net

Smarty templates

As part of my database development and Drupal class assignments, I was asked to research something related to the course. I have been learning about Drupal and how content is added to a site, but I knew there were other ways of adding content other than just typing it directly into the CMS. For instance, if you have an e-commerce site with regular customers who have signed up for an account, they may have unique content that should be pulled into certain nodes within your site based on their specific login credentials.  I decided to look into Smarty templates and what they are, how they work, and why they are used.
Smarty is a template engine for PHP. It allows you to use the more simplified Smarty syntax to create templates which are compiled and transformed into PHP scripts and are cached for quick retrieval. The entire concept behind Smarty templates is to separate application logic from presentation logic. When you are trying to create a semantic HTML site and then attempt to add in PHP it gets messy and creates workflow issues when you have designers and developers working on a site simultaneously. When you have a large amount of content (especially dynamic content) and multiple pages within a site,you can use Smarty templates to separate the server-side logic from the display logic to keep coding cleaner and easier to manage on large sites.
To begin using Smarty you should visit the website at http://www.smarty.net and look around, view the link Crash Course and look through some of the sources to templates created by others in the FAQ(wiki) section of the website. After you have familiarized yourself with Smarty and how it works you can follow these steps:

  1. Download the Smarty library files from this link: http://www.smarty.net/download
  2. Unzip the files/open the Smarty folder/ copy the ‘libs’ folder into the root folder of your website
  3. Create the following folders in your libs folder: ‘templates’, ‘templates_c’, ‘configs’, ‘cache’
  4. Create an ‘index.tpl’ file in your templates folder
  5. Create an ‘index.php’ file in your htdocs folder (located in your site files)

(Note: this is only a brief overview of the steps for setup – for full documentation, see the Smarty website)

Now that your files are in place, you can open your index.php file in Notepad++ and add the following:

require_once( ‘smarty.class.php’);                  (this calls for the Smarty library/code)
$smarty = new Smarty();                  (this creates a new object – know as a smarty)

This is the file where you will create objects from arrays and display them in your index.tpl file.

Here is an example of a couple simple/static smarty objects created to populate a welcome section on your homepage (refer to the Smarty website for info on using arrays):

require_once (‘smarty.class.php’);
$smarty = new Smarty();
$smarty->assign(‘name’, joe smith’);       (creates a Smarty object called $name,value = ‘joe smith’)
$smarty->assign(‘username’,’jsmith2345′);      (creates Smarty object called $username,value = ‘jsmith2345’)

$smarty->display(‘index.tpl’);  (states where these Smarty objects will be used/displayed)

Now that you’ve created your Smarty objects you can write the template markup in your index.tpl file (example):

<title>Welcome Page</title>
Hello, {$name|capitalize}<br>
username: {$username}<br>

The resulting output would be:


Hello, Joe Smith

username: jsmith2345

This is just a very basic example of using Smarty.  While using Smarty you can create an array (and arrays within an array and so on..), loop over an array, cycle through values, call functions, control caching and do many more things to make your Smarty objects and templates do what you need them to for your sites.  For more information, and to access the Smarty files visit http://www.smarty.net/.