Data Centers

An introduction to Miva scripting

If you're building an e-commerce presence via sites like Yahoo, you should learn Miva's scripting language. We'll show you the basics of this powerful Web application server.


Miva is a popular e-commerce application server used by companies such as Yahoo! and Dell, among others. At it's heart is Miva Merchant, which powers thousands of online stores.

In 1995, a company called Volant created HTMLScript. Revolutionary at the time because it was one of the only ways to create server side code without resorting to CGI/Perl, HTMLScript simplified the task of creating server code by offering an API that had a syntax similar to HTML. As a result, hundreds of ISP's started offering .HTS functionality, and that product quickly evolved into Miva.

Hello, world
Miva Script is the language inside Miva Merchant. Rather than .hts, Miva files end with a .mv extension. The tags contain both elements and attributes, like HTML or XML tags. In fact, the tags are designed to co-exist with your HTML code. Here is a basic "Hello World" example written in Miva:
<MvASSIGN NAME="l.displayHello" VALUE="Hello World!">
<MvEVAL EXPR="{l.displayHello}">

First, you create a local variable called displayHello and assign a value of "Hello World!" Then, on the second line you evaluate the local variable, which will be displayed in the user's browser.

Now let's see how to create and manipulate Miva databases using Miva Script. Along the way I'll also give you a brief overview of some software and resources to help you develop Miva-driven Web applications.

Tools of the trade
To set up your Miva development environment on a shoestring budget, you have a choice of two server components: The stand-alone MivaMia personal server or Miva Empressa, a Miva Script plug-in which is compatible with Windows NT/2000 and UNIX Servers. You can download both products for free.

Figure A
MivaMia in action


The server has basic configuration options that allow you to change the port, the default Web site directory, the database directory, the default filename, and the extension and timeout settings. The setup is quite similar to the Microsoft Personal Web Server component in IIS. The server usually sits in your system tray and can easily be enabled or disabled with the click of a mouse.

You don't need costly tools to develop Miva content. Miva Script can be written simply using a text editor like Notepad or WordPad. If you prefer using Macromedia Dreamweaver MX, you can download a set of Miva tag definitions.

The API allows you to perform a whole range of functions on par with other mature server scripting languages, such as file uploading, sending and receiving e-mail, communicating with remote HTTP servers, creating macros and more. It's all documented in the MivaScript Reference Guide.

If you are really serious about developing Miva code, you might want to invest in the Miva Script Compiler. Compiled Miva code runs faster than interpreted code and it is obfuscated during the conversion process into an executable. This means that you can develop Miva software that you can resell without worrying about someone stealing your source code. There is one downside however: The price of the compiler is a tad steep—roughly $700 USD.

Creating your first database
Miva supports two varieties of databases: Windows-based ODBC databases and xbase3 databases (which are compatible with dBaseIII). Currently, only the MivaMia product can pull data from an ODBC source. I will focus on creating the native xbase3 databases.

Let's walk through the source code for dbcreate.mv, a sample from an actual online poll application which will illustrate how to create and retrieve data from a Miva database:
<MIVA MvCREATE_Error = "nonfatal, nodisplay">

First you want to create an error handler just in case something goes wrong when Miva attempts to build the database. The nonfatal and nodisplay attributes assures that no error messages are displayed onscreen to the user.
<MvCREATE NAME = "polldb"
      DATABASE = "polldb.dbf"
        FIELDS = "uid NUMBER,
             question CHAR(254),
            answerone CHAR(254),
            answertwo CHAR(254),
            answerthr CHAR(254),
              graphic CHAR(254),
            widthsize CHAR(254),
              bgcolor CHAR(254),
             altcolor CHAR(254),
             desctext MEMO,
            emailreq CHAR(3)">

The next step involves defining the database. The NAME attribute refers to an alias of your database. All Miva database files end with the .dbf extension.

Then you define the database fields. Miva allows four different field types: BOOL, NUMBER, CHAR and MEMO. Optionally, you can also create an index for your database, which is a good idea if your database will contain many records.
<MvIF EXPR = "{ MvCREATE_Error }">
Error - Could not create Poll database
<MvEVAL EXPR = "{ MvCREATE_Error }">

If an error code does occur, then you can display your own custom message by checking the state of the error handler.
<MvELSE>
<MvASSIGN NAME = "polldb.d.uid" VALUE="1">
<MvASSIGN NAME = "polldb.d.question" VALUE = "Who is your favorite r&b/rap artist?">
<MvASSIGN NAME = "polldb.d.answerone" VALUE = "Jay-Z">
<MvASSIGN NAME = "polldb.d.answertwo" VALUE = "P Diddy">
<MvASSIGN NAME = "polldb.d.answerthr" VALUE = "Jennifer Lopez">
<MvASSIGN NAME = "polldb.d.graphic"   VALUE = "img/poll-faces.gif">
<MvASSIGN NAME = "polldb.d.bgcolor"   VALUE = "7998A9">
<MvASSIGN NAME = "polldb.d.altcolor"  VALUE = "5887A2">
<MvASSIGN NAME = "polldb.d.desctext"  VALUE = "Vote in this poll!">
<MvASSIGN NAME = "polldb.d.widthsize" VALUE = "200">
<MvASSIGN NAME = "polldb.d.emailreq"  VALUE = "yes">
<MvADD NAME = "polldb">

Then you add information into your database. All you have to do is assign a value to every field. Each field is declared using the alias, a "d" which indicates that the information originates from a database, and finally, the field name. Once the process is complete, theMvADDcommand will import the data into the database.
Poll Database Created!
</MvIF>
<MvCLOSE NAME="polldb">

Once the database has been created, you can retrieve the information using simple Miva code:
<MvOPEN NAME = "polldb" DATABASE = "polldb.dbf">
<MvFILTER NAME = "polldb" FILTER = "{'1' EQ polldb.d.uid}">

MvOPENwill open a connection to the database with the aliaspolldb.

TheMvFILTERcommand will send a query to the database and filter the results accordingly. The above command will specifically search for the value of "1" in the uid field.
<MvWHILE EXPR = "{ NOT polldb.d.EOF }">
&<br>
<ol>
<li>&</li>
<li>&</li>
<li>&</li>
</ol>

If the value "1" is not found in the uid, then the database will move to the end of file. If the value of "1" is found, then the above code displays the possible answers from the poll.
<MvSKIP NAME = "polldb" ROWS = "1"> 
</MvWHILE>
<MvCLOSE NAME = "polldb">

The application will then iterate through the database, one row at a time, searching for instances of "1" until it reaches the end of the database file.

That's your basic Miva database script. To learn more, one of the best resources for Miva related topics and troubleshooting is the discussion forums available on the Miva Web site.

Editor's Picks