Choose your database:
AnySQL
MySQL
MS SQL Server
PostgreSQL
SQLite
Firebird
Oracle
SQL Anywhere
DB2
MaxDB

Subscribe to our news:
Partners
Testimonials
Andrew Rassel: "I just wanted to let you know how satisfied I am with your administrative tools. I have been consistently receiving your support. I think you have wonderful products and I am highly satisfied with the results that I am getting".
Terry Prank: "At first, I was skeptical as is anyone encountering a new product on the web. I even tried out a couple of the competitors because I figured SQL Maestro Group sounded too good to be true. I soon learned the hard way, nothing I have seen on the market right now compares. Let the results speak. Within the first several hours of operation, I understood that the products are beyond comparison".

More

Add your opinion

SQL Maestro Group / Products / MaxDB / Resources / Using a third-party library in the script created by PHP Generator

How to use a third-party library in the script created by PHP Generator?

Last modified: Sep 22, 2010

Prev Next

Abstract

The article presents a step-by-step explanation of a third-party library use in the script created by PHP Generator. As an example we take a case when there is a need to implement a syntax highlighting using the Geshi engine.

Problem

Creating web applications we are trying to make each page comfortable for visitors. Such comfort requires a lot of hard work from web developers, but fortunately in most cases the complex functionality has already been developed and incorporated into ready-to-use libraries, so it is not necessary to implement the same features once again. The good news is PHP Generator gives you a great opportunity to activate third-party components easily.

The final target of this tutorial is to get a webpage with the list of code fragments highlighted according to the programming language they've written in. To reach the goal, we'll use the well-known freeware Geshi library.

Data storage

Assume that the statements are stored in a table with the following SQL definition:
Listing 1. SQL definition of the 'statement' table
CREATE TABLE statement (
  id                     int NOT NULL,
  statement_text         text NOT NULL,
  statement_language_id  int NOT NULL DEFAULT '0'
  /* Keys */
  PRIMARY KEY (id)
) ENGINE = InnoDB;

The 'statement_language_id' column contains the information on the programming language (C++, Java, SQL) used for the creating of code snippets stored in the 'statement_text' column in the following way: the '1' value of the 'statement_language_id' column means that the corresponding code fragment is written in C++, '2' - in Java, and '3' - in SQL.

Solution

Here are the steps involved in the implementation:

  • Copy third-party library files to your webserver (if necessary);
  • Include library files into the project;
  • Use library objects within the proper event handlers.

Each step will be covered in detail below.

Step 1

If the necessary library is absent on your webserver, you need to copy all the library files to any catalog of the computer the webserver is installed on.

Step 2

To use a PHP library in the generated app, we have to include one or several library files into the generated pages within the OnBeforePageExecute event handler. In the example case study, to use the Geshi library, it is enough to include the only file named geshi.php. Assuming that the library is placed in the geshi directory located a level above the generated application, we need to specify the event code as follows:

Listing 2. The OnBeforePageExecute event
include_once '../geshi/geshi.php';

Step 3

Now the library routines are ready to use. In this example we need to replace the content of the 'statement_text' column, so it's necessary to operate with the Geshi objects in the OnCustomRenderColumn event handler code.

Listing 3. The OnCustomRenderColumn event
if 
  (
    ($fieldName == 'statement_text') && 
    ($rowData['statement_language_id'] != 0)    
  ) 
{
  switch ($rowData['statement_language_id']) {
    case 1: $language = 'cpp'; break;
    case 2: $language = 'java'; break;
    case 3: $language = 'sql'; 
  }
  $source = $rowData['statement_text']; 
  $geshi = new GeSHi($source, $language);
  $customText = 
    '<div align="left">'.$geshi->parse_code().'</div>';
  $handled = 1;
}
The screenshot below displays how the event handler looks in PHP Generator.

The Events tab of the 'Statement' Page EditorPicture 3. The Events tab of the 'Statement' Page Editor and the OnCustomRenderColumn Event Editor

Result

Now we can compare the result webpages without using the Geshi library and with implemented syntax highlighting:

Result webpagesPicture 4. Result webpages

Conclusion

Of course Geshi is not the only third-party library to be used with PHP Generator. Internet provides us with a lot of such things and all of them may be employed in the produced web applications.

For more information about a specific tool, see the appropriate page:

Prev Next