Posts Tagged ‘Delphi for PHP’
Delphi for PHP (my verdict)
After having used Delphi for PHP to build an application framework, I managed to figure a lot of things out. I had to dig into the VCL and make some changes, search through tons of useless online documentation, and build and rebuild to finally get something I felt was fluid and sensible.
I’ve been a Delphi application developer since version 1, and I’ve grown with Delphi and it’s IDE and development style over the years. I’ve created dozens of custom components, taken advantage of it’s undocumented objects, and became involved with the Open Tools API.
I believe Delphi can be used to write some extremely poor applications, and I know it can be used to create some very slick applications as well. It’s all in the architecture and the developer who writes the code. Events can be elegant or nasty, depending on the experience the developer has with the IDE.
I tried to follow good practices in the Delphi for PHP environment. I’m not a fan of data-aware development in either Delphi environment, but I played with the technology in Delphi for PHP. I followed the rules, studied the code, and created some applications that took advantage of the templates, libraries, and it’s AJAX architecture.
After some very simple and mildly complex applications, written the way I believe Delphi for PHP was intended to be used, I gave up on it. Ignoring the weight of the VCL and just trying to make a decent application with an open mind (and some high hopes), I just couldn’t keep my code simple and elegant. I also couldn’t consistently produce XHTML/CSS code that remained W3C compliant or validated.
The future of Delphi for PHP is always in question when you are determining the direction of an application. Will it be supported in 5 years? Will they improve it? Can it even be improved? Will the VCL stand the test of time? Will there be a pool of developers to draw from when it comes time to hire an employee?
I just cannot believe it has a bright future as a commercial development environment, especially when there is an entire force of PHP developers openly improving upon Eclipse for PHP. With that said, I could in no way recommend a client begin new development with this product. In my opinion, they would eventually be stuck with a heavy weight application that can’t meet W3C standards, has outdated and neglected libraries, and frowned upon my any respectable PHP developer.
This is where I stand with Delphi for PHP and will no longer be creating articles or videos in the Delphi for PHP IDE.
I will however, be contributing content to PHP application development using Zend Servers and Eclipse for PHP, as well as some of the Zend Framework.
MySQLQuery Dataset to Create Dynamic Smarty Menus (AppObsess)
In this video tutorial I use a MySQL connection to dynamically create a MySQLQuery object, extract menu data from the database, and then create data-driven menu items for a Smarty Template.
This is a continuation from the Connecting to MySQL in Delphi for PHP (AppObsess) post.
The basic concepts of this video continue to show how to work with the Smarty Templates and how to spin through a MySQLQuery dataset using Delphi for PHP.
If you are familiar with Delphi, then you probably already know how to use the First, Next, Last, and EOF functions of a TDataSet object to spin through the data and collect information using the FieldByName or FindField functions.
The same principle applies in Delphi for PHP and all the basic navigation methods are available. The only difference in Delphi for PHP is that the fields are in an array property called Fields and each field retrieved in the query is accessible via the MySQLQuery->Fields['FieldName'] array elements.
I’ve created a unit called template_functions.php and added some functions to help build some of the common elements of a template. The core functionality of creating the query and spinning through the results looks like this:
-
// Dynamically create the main menu
-
function add_MainMenu($sender, $params, $MainData) {
-
$query = new MySQLQuery($MainData);
-
$query->Database = $MainData->MySQLDB;
-
$query->SQL = ‘select * from menu where menu_type_id = (select id from menu_type where menu_type_name = "MainMenu")’;
-
$query->Prepare();
-
$query->open();
-
-
if ($query->RecordCount > 0 ) {
-
$query->first();
-
while (! $query->EOF) {
-
$result = $result. ‘<li><a href="’.$query->Fields[‘menu_url’].‘">’.$query->Fields[‘menu_name’].‘</a></li>’;
-
$query->next();
-
}
-
}
-
return $result;
-
}
I’ve also added two new tables to the database. As time goes by I’ll be adding ALTER statements and adapting the database structure as I continue playing with this project. I might not always add the SQL in the posting, but it will always be included in the downloadable project .ZIP file.
-
CREATE TABLE menu_type (
-
id bigint(20) NOT NULL AUTO_INCREMENT,
-
menu_type_name varchar(60) NOT NULL,
-
menu_type_desc varchar(200) NOT NULL,
-
-
UNIQUE KEY menu_type_name (menu_type_name),
-
PRIMARY KEY (id, menu_type_name)
-
) TYPE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1000;
-
-
CREATE TABLE menu (
-
id bigint(20) NOT NULL AUTO_INCREMENT,
-
menu_type_id bigint(20) NOT NULL,
-
menu_name varchar(60) NOT NULL,
-
menu_url text NOT NULL,
-
-
UNIQUE KEY menu_name (menu_name, menu_type_id),
-
PRIMARY KEY (id, menu_name),
-
FOREIGN KEY (menu_type_id) REFERENCES menu_type(id) ON DELETE CASCADE
-
) TYPE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1000;
If you would like to download this application in the state it is for this video, you can download it using the link below. The .SQL file, the template, and the Delphi for PHP project code is all included.
Download AppObsess-dev-002.zip here!
Connecting to MySQL in Delphi for PHP (AppObsess)
In this video tutorial I begin building an ongoing project called AppObsess, which I will use as a generic and very simple application engine using the Smarty Template engine and the MySQL database. I begin this video series having applied the techniques from the other video’s: Creating Editable Forms for Smarty Templates and Creating Dynamic Smarty Variables in Delphi for PHP.
Connecting to a MySQL database in Delphi for PHP is an extremely simple task. In this video I create a data module and a config file containing the connection strings, include the files and execute the program. That’s the entire video in a nutshell. Most of the video is spent getting up to speed on the state of the application.
The core code is written in a file called maindb.php (the data module) and it looks like this:
-
$MainData->MySQLDB->Host = HOSTNAME;
-
$MainData->MySQLDB->DatabaseName = DATABASE;
-
$MainData->MySQLDB->UserName = USERNAME;
-
$MainData->MySQLDB->UserPassword = PASSWORD;
-
$MainData->MySQLDB->Open();
-
if (! $MainData->MySQLDB->Connected) {
-
}
As always, you should have much better error control handling then I added for this simple tutorial. My connection testing was just to make sure something stupid didn’t happen while I was creating this video.
The config.inc file is very simple as well and only contains the connection strings for MySQL. It looks like this:
The starting template I am using for this AppObsess web framework in Delphi for PHP is the Blue Obsess template available from my XHTML/CSS Templates page.
The starting database SQL I have for this application is:
-
DROP DATABASE IF EXISTS obsess;
-
CREATE DATABASE IF NOT EXISTS obsess;
-
USE obsess;
-
-
CREATE TABLE page (
-
id bigint(20) NOT NULL AUTO_INCREMENT,
-
page_name varchar(200) NOT NULL,
-
page_body text NOT NULL,
-
-
UNIQUE KEY page_name (page_name),
-
PRIMARY KEY (id, page_name)
-
) TYPE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1000;
If you would like to download this application in the state it is for this video, you can download it using the link below. The .SQL file, the template, and the Delphi for PHP project code is all included.
Download AppObsess-dev-001.zip here!
Creating Editable Forms for Smarty Templates in Delphi for PHP
Once you begin working with the Smarty Template engine in Delphi for PHP and once you learn how to include and update smarty variable tags, you’ll want to start including work areas for your data forms. With a lack of VCL documentation, it’s not obvious how to get your controls in the smarty template and get them positioned correctly.
Additionally, for several days I was stumped as to why I couldn’t get my Button code to execute when I used a smarty template. This video shows how to accomplish all these tasks. This video extends the lesson in Creating Dynamic Smarty Variables in Delphi for PHP and in Hello World (Delphi for PHP).
The main thing to remember in all of these lessons is that in obtaining the desired effect, we are basically configuring the proper Object Inspector Properties or extending the code within the Object Inspector Events for the proper objects.
Creating Dynamic Smarty Variables in Delphi for PHP
One of the first things you’ll notice when you begin working with Smarty Templates in the Delphi for PHP environment is that it’s not obvious how you can include custom or dynamic smarty variable tags into your template from Delphi for PHP.
This video was created to demonstrate exactly how to get dynamic smarty variables into your smarty templates and display the correct variable information. The main thing to know and understand is, that the VCL in Delphi for PHP has a certain time it executes the Smarty Engine events.
To access the correct time and place to assign your variables is to use the Forms->OnTemplate event and access the smarty object through the provided $params. In this video I’ve created 3 dynamic smarty tag variables. $MyTitle, $MyDescription, and $MyKewords. I assign the values of these variables in Delphi for PHP and placed them in the HTML smarty template.
-
function Unit1Template($sender, $params)
-
{
-
$template = $params[‘template’];
-
$template->_smarty->assign(‘MyTitle’, ‘My Custom Page Title’);
-
$template->_smarty->assign(‘MyDescription’, ‘My Custom description…’);
-
$template->_smarty->assign(‘MyKeywords’, ‘keyword 1, Smarty Keyword’);
-
}
If you’ve found this video posting useful, you might also be interested in the video posting that extends this lesson: Creating Editable Forms for Smarty Templates in Delphi for PHP.


