Posts Tagged ‘AppObsess’

MySQLQuery Dataset to Create Dynamic Smarty Menus (AppObsess)

preview image

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:

  1.    // Dynamically create the main menu
  2.     function add_MainMenu($sender, $params, $MainData) {
  3.       $query = new MySQLQuery($MainData);
  4.       $query->Database = $MainData->MySQLDB;
  5.       $query->SQL = ‘select * from menu where menu_type_id = (select id from menu_type where menu_type_name = "MainMenu")’;
  6.       $query->Prepare();
  7.       $query->open();
  8.  
  9.       if ($query->RecordCount > 0 ) {
  10.         $query->first();
  11.         while (! $query->EOF) {
  12.           $result = $result. ‘<li><a href="’.$query->Fields[‘menu_url’].‘">’.$query->Fields[‘menu_name’].‘</a></li>’;
  13.           $query->next();
  14.         }
  15.       }
  16.       return $result;
  17.     }

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.

  1. CREATE TABLE menu_type (
  2.         id bigint(20) NOT NULL AUTO_INCREMENT,
  3.   menu_type_name varchar(60) NOT NULL,
  4.         menu_type_desc varchar(200) NOT NULL,
  5.  
  6.         UNIQUE KEY menu_type_name (menu_type_name),
  7.   PRIMARY KEY (id, menu_type_name)
  8. ) TYPE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1000;
  9.  
  10. CREATE TABLE menu (
  11.         id bigint(20) NOT NULL AUTO_INCREMENT,
  12.         menu_type_id bigint(20) NOT NULL,
  13.   menu_name varchar(60) NOT NULL,
  14.   menu_url text NOT NULL,
  15.  
  16.         UNIQUE KEY menu_name (menu_name, menu_type_id),
  17.   PRIMARY KEY (id, menu_name),
  18.   FOREIGN KEY (menu_type_id) REFERENCES menu_type(id) ON DELETE CASCADE
  19. ) 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)

preview image

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:

  1. $MainData->MySQLDB->Host = HOSTNAME;
  2. $MainData->MySQLDB->DatabaseName = DATABASE;
  3. $MainData->MySQLDB->UserName = USERNAME;
  4. $MainData->MySQLDB->UserPassword = PASSWORD;
  5. $MainData->MySQLDB->Open();
  6. if (! $MainData->MySQLDB->Connected) {
  7.   echo ("Unable to connect to " . DATABASE . "\r\n");
  8.   exit ("maindb.php MainData->MySQLDB critical error");
  9. }

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:

  1.    DEFINE(‘HOSTNAME’, ‘localhost’);
  2.     DEFINE(‘DATABASE’, ‘obsess’);
  3.     DEFINE(‘USERNAME’, ‘obsess’);
  4.     DEFINE(‘PASSWORD’, ‘demo’);

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:

  1. DROP DATABASE IF EXISTS obsess;
  2. CREATE DATABASE IF NOT EXISTS obsess;
  3. USE obsess;
  4.  
  5. CREATE TABLE page (
  6.         id bigint(20) NOT NULL AUTO_INCREMENT,
  7.   page_name varchar(200) NOT NULL,
  8.   page_body text NOT NULL,
  9.  
  10.         UNIQUE KEY page_name (page_name),
  11.   PRIMARY KEY (id, page_name)
  12. ) 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!