Home arrow Zope arrow Page 5 - The Number Game

Down Memory Lane - Zope

Despite what you might think, it's fairly easy to write MySQL-based Web applications with Zope. In this case study, find out how to do just that by building a Zope-based online poll which uses MySQL to store poll questions and user responses, and DTML Methods to extract and present this information.

  1. The Number Game
  2. Reading The Funnies
  3. Start Me Up
  4. Rocking The Vote
  5. Down Memory Lane
  6. Cookie-Cutter Code
  7. Adding It All Up
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 7
April 16, 2002

print this article


So that takes care of the mechanics of displaying a question, registering votes, and displaying totals. The next item to address is the ability to view an archive of previous polls. The DTML Method to accomplish this is called "viewPollResults", and it looks like this:

<dtml-var standard_html_header> <dtml-in getPollResults> <table border=0 cellspacing=0 cellpadding=5> <tr><td colspan=3><b><dtml-var question></b></td></tr> <tr><td><dtml-var response1></td><td> <dtml-var votes1> </td></tr> <tr><td><dtml-var response2></td><td> <dtml-var votes2> </td></tr> <tr><td><dtml-var response3></td><td> <dtml-var votes3> </td></tr> <tr><td><font size=-2>Posted on <dtml-var date fmt="%d/%m/%Y"> </font></td><td><font size=-2><dtml-var expr="votes1 + votes2 + votes3"> total votes</font></td></tr> </table><p> </dtml-in> <p> <font size="-2"><a href="viewCurrentPoll">back to main page</a></font> <p> <dtml-var standard_html_footer>
This method is extremely simple - it merely calls the "getPollResults" Z SQL Method without any arguments, and iterates through the returned result set to display a list of previous polls.

At this point, you should be thinking to yourself, "...that doesn't compute". This is because a couple of paragraphs ago, I said that the "getPollResults" required the question ID as mandatory argument. And now I'm contradicting myself by saying that I'm going to call the same method without passing it any arguments. Huh?

Well, you're right - calling "getPollResults" without any arguments is sure to make Zope barf. Unless, that is, I alter the Z SQL Method and add a little more intelligence to it, so that it can make a decision about which query to execute based on the presence or absence of the question ID.

<dtml-if qid> SELECT question, response1, response2, response3, votes1, votes2,votes3, date from poll WHERE qid = <dtml-sqlvar qid type="int"><dtml-else> SELECT question, response1, response2, response3, votes1,votes2, votes3, date from poll </dtml-if>
Simple, isn't it?

Here's what it all looks like:

>>> More Zope Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Creating Zope Products
- Plone Content Types With Archetypes
- Flat User Management in Zope
- Creating Basic Zope Applications
- Getting started with Zope for Linux and Sola...
- ZPT Basics (part 4)
- ZPT Basics (part 3)
- ZPT Basics (part 2)
- ZPT Basics (part 1)
- Exception Handling In DTML
- DTML Basics (part 4)
- DTML Basics (part 3)
- DTML Basics (part 2)
- DTML Basics (part 1)
- Using Zope With Apache

Developer Shed Affiliates


Dev Shed Tutorial Topics: