For the purposes of demonstration, let's imagine we have a small online store that wants to provide access to its client list via a nice, modern Web-based interface. To provide the required functionality, let's put the data into a MySQL database and provide access to the list via an OpenLaszlo application.

To begin, log in to the MySQL client as the root, then create a database called store and a MySQL user called store _manager:
mysql> create database store; mysql> use mysql;
mysql> grant all on store. * to store_manager identified by 'passwordhere' ; id int not null auto_increment primary key, name varchar (64) not null,
address varchar (255),
contact_tel_no varchar (64),
email address varchar (64)
A small collection of SQL insert statements (formatted to fit this page) provides us with some data to play with:
mysql> insert into client_details value ( 0,
'Joe 810gg5', '25 Somewhere Street, Any town, USA', '00-1-415-555-3226', 'joe@bloggs.com' );

Name:  500948974.jpg
Views: 151
Size:  14.1 KB

mysql> insert into client_details value ( 0,
'Jane Doe', 'Apt. 2a, 16 Treatsville, Canada',
'00-1-416-555-1222', 'jane@idontknow.ca' ); mysql> insert into client_details value ( 0, 'Harry Smith', 'P.O. Box 46, Streetstown, USA', '00-1-581-555-9823', 'harry@harrysmith.com' ); mysql> insert into client_details value ( 0, 'Julie Jones', 'CharmsRus, BT Tower, London, UK',
With the database table ready, and some sample
data inserted, we next need to get the data into a format that OpenLaszlo can understand. It shouldn't surprise
you to learn that the best format for your data when communicating with OpenLaszlo is XML. OpenLaszlo has some rather neat, built-in functionality for working with XML data. To demonstrate this, we first have to arrange for MySQL to produce some XML output.
There are a number of ways to do this, and I'm going to write a simple CGI in Ruby that connects to the database, selects all the data from the required table and turns it into XML. My program, called get_data. rb, will execute from Apache's CGI directory, which is /usr/lib/cgi-bin on my system. Here's the Ruby code I wrote:
.' /usr/bin/ruby
require 'cgi I require 'dbi'
This code is straightforward. The key line is the call
to DBI::Utils::XMLFormatter, which takes the result of the SQL query and produces correctly formatted XML. To see the reSUlts, install geCdata. rb into Apache's cgi-bin directory (setting get_data. rb to be executable), and then type the following into a browser: http://localhostlcgi¬bin/get_data. rb. Figure 2 shows the XML produced by the get_data. rb CGI script.
To access this data from within an OpenLaszlo application, all that's required is the appropriate declaration using the LZX dataset tag. Here's another file, called clients. lzx, which displays the name of each of the store's clients in an OpenLaszlo window:
with something called 'simplelayout', an in-built Open Laszlo style that stacks text one line on top of another. The window, called top, also has a scrollbar associated with it. The dataset LZX tag informs the OpenLaszlo application where to get the data from (src), what to call the dataset (name) and instructs the application to go and get the data as soon as it is loaded (request). The datapath tag is a standard XML XPath specification pointing to the dataset that we want to use. In this case, we want to retrieve the text of the name tag, which is contained within the inner¬enclosing client tag, which is itself contained within the outer-enclosing clients tag. Referring back to Figure 2, it is easy to see the data that we are referring to within this XPath specifier.

To try out this application, copy the LZX file to the appropriate directory on the server (using the same destination directory as for the ljheLlo.lzx file), then start the application running within your browser using the following URL: http://localhost:8080/lps-4.0.10Iclients.lzx.

This produces an OpenLaszlo window with the names of the four clients displayed within it, as shown in Figure 3.