Using VFP commands to generate HTML

HTML is just text and FoxPro has always been very good at manipulating text. Generating the HTML on your own PC gives you a static document on the web but that's sometimes all that the user needs. There are four easy of generating HTML from FoxPro:

  • The Web Page Wizard
  • The GenHTML utility
  • TextMerge commands
  • The StrToFile() function
  • The FoxPro Editor

Web Page Wizard

This is more useful than most wizards and creates a table of data on a web page from a VFP table or cursor. The wizard generates an HTML file but can also generate a prg file that will recreate that web page from a later version of the table. This generated program uses GenHTML and creates and uses a new entry in the GenHTML table.

GenHTML

The 'Save as HTML' option on the File menu calls the generator program specified by system variable _GENHTML. This is usually GenHTML.Prg but you can write your own utility if you want to improve it. GenHTML.Prg can be added to your project and distributed in an executable.

The simple syntax is:

DO GenHTML With lcHTMLFileName, lcTableName

You can extend the power of GenHTML by referring to two FoxPro Foundation Classes and a table of styles:

  • _HTML.vcx
  • _HTMLSty.vcx
  • GenHTML.dbf

These too can be distributed in your application.

TextMerge

TextMerge is a tool that dates from FoxPro version 2.00 where we used it to create reports. TextMerge produces a pure ASCII output to a text file and the only way to get special effects in these reports was to embed printer control codes in the text. The Report Writer made life so much easier that TextMerge is little used now.

The output from TextMerge can be a mixture of text literals, the contents of fields or memory variables, and the result of VFP functions. This flexibility means that the TextMerge commands have become useful again as powerful tools for generating a web page from FoxPro data.

More details on TextMerge here.

StrToFile

StrToFile is a new function that was introduced with VFP 5. As the name suggests, it takes a string variable and writes it into a text file. You have the option of creating a new file, overwriting an existing file, or adding text to the end of an existing file.

The StrToFile function returns the number of bytes written so you can check this value to ensure that your file has actually been created. The VFP 7 version of StrToFile (and its partner FileToStr) opens the file in Shared mode to reduce the risk of conflict

Use Customer

*-- Start the string with a heading for the page
lcHTML = '<h1>Customer List</h1>'

*-- Now append 'Company' and the company name for each record
SCAN
   lcHTML = lcHTML + '<br>Company  '
   lcHTML = lcHTML + ALLTRIM (Company)
ENDSCAN

*-- And send it to a file
=StrToFile (lcHTML, 'Customer.htm')

FoxPro Editor

Don't forget that FoxPro has a good text editor built into it that's much better than Notepad as a basic tool for writing HTML. Think of all these advantages:

  • Multiple levels of Undo.
  • Drag and Drop.
  • Automatic indenting.
  • Intellisense which can be extended so that typing PP gives you the opening and closing paragraph tags and puts the cursor between them:

    <P>
       |
    </P>
  • Global search and replace across all pages using Code References on the Tools menu.
  • You can write simple FoxPro programs to write and manipulate your HTML files.

MS Access technical tips

Visual FoxPro technical tips

General Tips

 

More software development tips from Alvechurch Data

FoxPro TextMerge

Using TextMerge to export text files, html or xml from FoxPro

Read More

IntelliSense in Visual Foxpro

Using and extending FoxPro Intellisense

Read More

Copying data to plain text files from FoxPro

FoxPro commands to copy data from tables into comma separated and other types of delimited text files

Read More

NotePad++

Using NotePad++ to create a simple HTML page

Read More

NotePad++ and FireFox

Using NotePad++ and FireFox together

Read More