[Express] Interfacing with MySQL

I am new to Softpress. I need to read data from MySQL and dynamically display the information on oue web site. What is the easiest way to do that in Softpress


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Hi,
Here are some examples I did a few years ago:
http://easibase.com/freeway/phpmysql.php

This does not teach you MySQL or PHP but it does show one of the ways to use dynamic data with Freeway.

Hopefully this will be of help.

On Jan 7, 2012, at 12:11 AM, Ashok Iyer wrote:

I am new to Softpress. I need to read data from MySQL and dynamically display the information on oue web site. What is the easiest way to do that in Softpress


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

If your data is already in the database, and you are looking for a truly read-only option, then the first thing you need to do is figure out the data format in the database. You will need to know the names of all the columns, the name of the table(s) and you’ll need a username and password pair (set within MySQL) that is permitted to access this particular table or set of tables. (All of this is case-sensitive, by the way.)

Next, you need to know if your server supports PHP (most do). There are lots of other ways to integrate MySQL with a Web server, but PHP is far and away the most popular. It’s also good to find out what version of PHP and what version of MySQL you’re working with. It’s very rare to find a host anymore that is running anything less than PHP 5.1.something, and likewise for MySQL. (If yours is stuck back in the end of the previous century, then it’s really not a good idea to try to back-port your code to work. It’s much easier to find a better host.)

Freeway Pro supports the use of third-party Actions that make the next step much easier, but if you are handy with an FTP application and have a plain-text editor like CodeWrangler, you can use Express in much the same way. The following instructions assume Express, so they’re essentially the same thing you would do if all you had was a text editor and you were hand-coding the whole thing.

On the page where you want to connect with the database, open the Page / HTML Markup dialog and choose the Before HTML option in the picker. Type the following into the dialog:

<?php
require_once('connect.php');
?>

We’ll make that file later, and upload it to your server, but for the moment, this is the first step in Freeway. What will happen when you publish is that Freeway will inject this code into the HTML before the opening <!DOCTYPE…> tag. Because it’s PHP code, at the moment it won’t be interpreted by your server, because your page is still set to a filename that ends in .html. Your next change will be to use the Page Inspector to set the page filename to whatever.php (whatever it used to be, just change the file-type extension). From this moment forward, Freeway will not permit you to preview this page – you’ll just see the raw HTML/PHP code instead. You’ll need to upload to your server and browse it at the actual address to see what you’ve made.

Next, you need to consider what form you want your variable data to take on your page. Is it paragraphs, table rows, unordered lists (bullets), some other layout mode? This will largely be driven by your data model – what do the rows in your database represent, in other words.

For purposes of explanation here, I’ll assume the following data (it’s an address book):

people (database table)
	id (primary key, auto-incrementing, int[11])
	first_name (varchar 255)
	last_name (varchar 255
	address_1 (varchar 255)
	address_2 (varchar 255)
	city (varchar 255)
	state (varchar 255)
	zip (varchar 12)
	phone_1 (varchar 255)
	phone_2 (varchar 255)
	email_1 (varchar 255)
	email_2 (varchar 255)

This would fit nicely in a table for display purposes. Draw a table on your page using the Table tool, and make it two rows by 7 columns. Style it up how you like, setting borders and padding to give it some structure. The top row will be your table header, so go ahead and put some text in there as titles for each column. Note that we’re only using one column for first and last name combined, and likewise for address, phone and e-mail.

Now, click on a cell in the second row, and look in the Inspector. Find the Row section (left-most tab, top segment) and click the Markup button. This opens up a dialog similar to the Page / HTML Markup dialog you used already, except it targets the table structure rather than the entire page. In the Before TR section, add the following:

<?php foreach($people as $person){ ?>

and in the After TR section, add this:

<?php } ?>

What this does is make that second table row into a template for as many rows as you have people, creating a new table row for each one. Now the only thing left is to format the data. You do that in the second row of the table directly. Click into the first cell, so you have a flashing text cursor. Type a space, then from the Insert menu, choose Markup Item. You’ll see a dialog in the center of the screen. In it, add the following code:

<?php echo implode(' ', 
	array($person->first_name, 
	$person->last_name)); ?>

A bit of explanation about what this is doing. You have a first and a last name, but they may not both be there in your data. This function (implode) takes an array (structured group) of strings (text) and turns them into a sandwich with whatever you’ve passed to it (in this case, a space) in between elements. If there’s only one element in the array, you don’t get anything besides that one element back. This doesn’t seem valuable right now, but the next example will make it plain, since we’re not just adding a space.

Okay the dialog, and you’ll see a little box that looks like [< H >] in the middle of your text box. Type another space after it. Now move to the next cell and repeat:

<?php echo implode(', ', 
	array($person->address_1, 
	$person->address_2)); ?>

Be sure to add the space before and after the markup item, that’s how you’ll eventually set text style on these dynamic code bits. Now repeat the above for the phone and e-mail addresses. I leave the actual code as an exercise.

Finally, the other elements are much simpler (city, state, and zip). They look like this:

<?php echo $person->city; ?>

Now, if you highlight all of the text in each cell (selecting the entire “space, markup item, space” sandwich) you can apply your desired text style to it using the normal Freeway Inspector controls.

At this point, you are done with the Freeway part of the program. You’ve created a reference to a script called connect.php, so let’s go make that now. It’s going to create the connection to your database and select all the rows and put them in a variable called $people. That’s how come your page in Freeway doesn’t have any database-specific code in it – all that work has been done elsewhere.

Create the following text file in your editor (TextWrangler, free from http://barebones.com is my recommendation if you don’t already own a programmer’s editor). Name the file connect.php.

<?php
// un-comment the following two lines to show errors while debugging
// ini_set('display_errors',true);
// error_reporting(E_ALL);
// edit the following line to match YOUR database server
define('MYACTIVERECORD_CONNECTION_STR', 'mysql://username:password@servername/databasename');
require_once('MyActiveRecord.php');
class People extends MyActiveRecord {
	//var $_primary_key = 'customers_id';
}
$people = MyActiveRecord::FindAll('people', null, 'last_name ASC, first_name ASC');
?>

Download MyActiveRecord from here: http://scripty.walterdavisstudio.com/mar/mar.zip

Back in Freeway, upload your document to your server. Next, using an FTP application, upload connect.php and MyActiveRecord.php to the same folder as your dynamic page. Now visit your page in a browser and see what you see. If all has gone well, you should see your entire database laid out in a table. If something has gone wrong, then you should see either a plain white page or a bunch of cryptic error messages. If all you see is the white page, then you can turn on error reporting (see the top of the connect.php script for instructions) and upload that file again and reload to see what’s gone wrong. It’s very rare for everything to work perfectly on the first try, and learning how to interpret error messages is the first lesson of any programmer.

Hope this helps, and let me know how it goes for you.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thank you walter for such a detailed explanation. I think I can do this now. Having said that, would it be lot easier if we upgrade to Freeway PRO? Is there a way for me read the user guide for Freeway PRO realted to this feature before upgrading it?

Thank you so much for your help Walter.

Ashok


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

On Jan 13, 2012, at 12:46 PM, Ashok Iyer wrote:

Thank you walter for such a detailed explanation. I think I can do this now.

That’s great!

Having said that, would it be lot easier if we upgrade to Freeway PRO? Is there a way for me read the user guide for Freeway PRO realted to this feature before upgrading it?

What you get with Pro (as it relates to this) is access to the whole world of custom Actions (Express ships with a set of “bundled” Actions, but you cannot install any others). The demo code I posted here would work in both Pro and Express, because it doesn’t rely on Actions to work at all.

There are a set of PHP/MySQL Actions available at ActionsForge. They allow you to connect to a MySQL database, issue requests for records, and display the results in your page without needing to write much or any code. Note that these were written quite a long while ago, and I haven’t used them for many years, so I may be misremembering their reach and scope.

There are also some more basic Actions that would allow you to link a library of PHP code to the top of your page, rather than needing to manage that upload separately or hand-code the <?php require… statement in the Page / HTML Markup dialog. If you were following these instructions here, that would chop out a lot of steps.

I’ve never used Express for any length of time, so there’s probably quite a lot more that you would find useful and revolutionary should you choose to upgrade. My guess is that it would be a good investment for you.

Walter

Thank you so much for your help Walter.

Ashok


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options