Home arrow Zope arrow Page 5 - DTML Basics (part 3)

Bringing In The Database - Zope

Last time out, DTML Basics demonstrated conditional statements. This time around, it's time to study loops...which, in the DTML world, aren't exactly what you're used to. Take a look.

TABLE OF CONTENTS:
  1. DTML Basics (part 3)
  2. When In Rome...
  3. A List In Time
  4. An Easy Replacement
  5. Bringing In The Database
  6. Digging Deeper
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 5
July 02, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Another common application of sequences involves using them in combination with database result sets to break up a large result set into smaller batches.

Consider the following example, which invokes a ZSQL Method (for the uninitiated, a ZSQL Method is a special Zope object that allows you to communicate with a database - read more about it at (http://www.devshed.com/Server_Side/Zope/ZopeWithApache). In the following example, the GetUsers() method retrieves a list of users from a database

select * from person
as a sequence and uses a DTML loop to iterate through it and print the data.

<table cellpadding="5" cellspacing="0" border="1" width="250"> <tr bgcolor="#CFCFCF"> <td align="left"><b>User Details</b></td> </tr> <dtml-in GetUsers sort=FirstName> <tr> <td align="left"><font size="2"><b><dtml-var sequence-number>. <dtml-var Title> <dtml-var FirstName> <dtml-var Surname></b></font><br> <font size="1"><i><dtml-var Email></i><br><dtml-var Tel><br><dtml-var Fax></font></td> </tr> </dtml-in> </table>
The output displays all the records in a single page, as shown below.



This example uses the <dtml-in> tag to loop over the result set returned by the GetUsers() ZSQL Method. The rest of the code is good ol' HTML to make the page look pretty.

Depending on the number of records in the table, you can rest assured that this is a definite no-no as far as the usability and performance of the site is concerned. A better idea would be to split this result set into multiple "pages", so as to reduce overhead and also to make the site more usable. Here's the code:

<table cellpadding="5" cellspacing="0" border="1" width="250"> <tr bgcolor="#CFCFCF"> <td align="left"><b>User Details</b></td> </tr> <dtml-in GetUsers size="6" start=start_here sort=FirstName> <dtml-if sequence-start> <dtml-if previous-sequence> <tr><td align="right"><font size="2">[ <a href="<dtml-var URL><dtml-var sequence-query>start_here=<dtml-var previous-sequence-start-number>"> Previous <dtml-var previous-sequence-size> users</a> ]</font></td></tr> <dtml-else> <tr><td align="right">&nbsp;</td></tr> </dtml-if> </dtml-if> <dtml-if sequence-even> <tr bgcolor="#DCDCDC"> <dtml-else> <tr bgcolor="#CECECE"> <tr> </dtml-if> <td align="left"><font size="2"><b><dtml-var sequence-number>. <dtml-var Title> <dtml-var FirstName> <dtml-var Surname></b></font><br> <font size="1"><i><dtml-var Email></i><br><dtml-var Tel><br><dtml-var Fax></font></td> </tr> <dtml-if sequence-end> <dtml-if next-sequence> <tr><td align="right"><font size="2">[ <a href="<dtml-var URL><dtml-var sequence-query>start_here=<dtml-var next-sequence-start-number>"> Next <dtml-var next-sequence-size> users</a> ]</font></td></tr> <dtml-else> <tr><td align="right">&nbsp;</td></tr> </dtml-if> </dtml-if> </dtml-in> </table>
Phew! That's a lot of code...but the result is worth it.

Take a close look at the output. No scroll bar. Neat little links to the next and previous "batch" of users. Just what I wanted.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

ZOPE ARTICLES

- 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: