The Number Game - Start Me Up
(Page 3 of 7 )
With the database taken care of, it's time to get started with the user interface for our application. First things first - create a database connection as an instance of the Z MySQL Database Connection object and pass it the appropriate database name, user name and password. Remember that you can use the object's Test function to verify that the connection is working as it should.
Now, the first DTML method needed is "viewCurrentPoll"; this invokes a Z SQL Method that connects to the database to retrieve the latest poll, and displays it with a list of possible responses. Take a look:
<dtml-var standard_html_header>
<dtml-in getCurrentPollDetails>
<form
action="addUserResponse">
<b><dtml-var question></b>
<p>
<input
type="Radio" name="response" value="1"><dtml-var response1> <p>
<input
type="Radio" name="response" value="2"><dtml-var response2> <p>
<input
type="Radio" name="response" value="3"><dtml-var response3>
<input type="hidden"
name="qid" value="&dtml-qid;"> <p> <font size=-2><a
href="viewPollResults?id=&dtml-qid;">view
results</a></font> <font
size=-2><a href="viewPollResults">view past
polls</a></font> <p> <input
type=submit name=submit value="Vote">
</form> </dtml-in> <dtml-var
standard_html_footer>
This DTML Method merely contains some plain-vanilla code to display a simple form to the user. The values in the form are fetched via the "getCurrentPollDetails" Z SQL Method. Here's what it looks like:
SELECT qid, question, response1, response2, response3 from poll ORDER BY
qid DESC
Pay special attention to the SQL query that I've used here - I'm using the ORDER BY and DESC keywords to get all the records in descending order, so that the latest question appears first in the result set. Obviously, I also need to limit the result set to a single record - which can easily be done by hitting the advanced configuration for the Z SQL Method and setting the maximum number of rows to be retrieved to 1. In case you're wondering why I didn't just add a LIMIT clause to the SQL query above, there's a simple reason - Zope's MySQL database adapter doesn't support the use of LIMIT clauses in SQL statements (http://www.zope.org/Members/adustman/Tips/no_limits has more on this).
The <dtml-in> construct is used to iterate through the results of the Z SQL Method invoked, and the form, once submitted, invokes the "addUserResponse" method to add the user's response to the database. Note that the identifier for the poll question is also included in the form, as a hidden field; when the form is submitted; this identifier will be used to ensure that the correct record is updated with the user's choice.
Here's what it looks like:

Next: Rocking The Vote >>
More Zope Articles
More By Harish Kamath, (c) Melonfire