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.
Free Membership Required: Please Login or Register to read the rest of this content.
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.
Free Membership Required: Please Login or Register to read the rest of this content.
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.
Hello World (Delphi for PHP)
This is the very first and very basic “hello world” program with a few edit controls and a submit button, using Delphi for PHP. There is nothing complicated about this application. The entire purpose of this is to illustrate how to manipulate a Label by use of the OnClick event of a Button.
While Delphi for PHP is currently at version 2, it still has a lot of growth left. Nevertheless, this IDE is an excellent IDE for writing and developing PHP code and applications. Using the VCL is an option and not a requirement. If you wish to use the VCL controls, then of course the Forms and VCL is needed.
Note: I’ve found the Delphi for PHP IDE a little picky if not delicate. I’ve had times when I’ve built this project and the button doesn’t work. I’ve discovered the reason is environment variables which define where the VCL is located in the path. The default project location is included in your path, so if you create a special folder on another drive, perhaps you too will epxerience problems with the button not being executed properly. When you “distribute” the application and it’s packages, the paths work fine. It’s only during development in the IDE and Debug Listener, where you may experience such problems.


