Python Script/ZSQL – Alternate data over 2 columns 1


Here’s a quick post based on a Python Script I made in Zope to display the data from a ZSQL method in a 2 column table layout. Not that big of a deal, but I wanted to save this because it took a little bit of thought and some learning.
(My original code is all mess up on this, I’ll try to find and repost it.)

## Script (Python) "genlodinfo" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=itemid ##title= ## # Example code:  ################################### """  Author: Greg Fischer  1st Byte Solutions - greg@1stbyte.com   Date: 9/24/05   License: You are free to reengineer  rework, recode, redistribute, resell  or alter this  code in any way you see fit, but you   must give credit to my original work  and you must provide this same license  to those that may receive your distribution  if you do. (just leave my name on it,  and you must offer the same freedom  in your work, that's all)   Purpose: This script will gather  from a zsql method, cat the records  into address records (with exra info),  then generate a table with alternating  rows. In other words, it will fill the table  from left to right, then down a row, 2 columns  wide. """ ###################################  def iseven(n):    """Return true if n is even."""    return n%2==0  def isodd(n):    """Return true if n is odd."""       return not iseven(n)  # Import a standard function, and get the HTML request and response objects. from Products.PythonScripts.standard import html_quote request = container.REQUEST RESPONSE =  request.RESPONSE  rs = context.sql.ap_lodging(itemid) rownum = 1 x = [] for r in rs: 	ritemid = str(r[0]) 	rtqstart = str(r[4]) 	rtqend = str(r[5]) 	rstreet1 = r[12] 	rstreet2 = r[13] 	rcity = r[14] 	rstate = r[15] 	rzip = r[16] 	rnotes = r[18] 	 	lodstr = rtqstart + ' - ' + rtqend + '
' 	lodstr = lodstr + rstreet1 + '
' + rstreet2 + '
' 	lodstr = lodstr + rcity + ', ' + rstate + ' ' + rzip + '
' 	if rnotes <> '': 		lodstr = lodstr + rnotes + '
' 	 	x.append(lodstr)  table = '' tablee = '

' tr = '' tre = '' td = '' tde = '' 	 if len(x) >= 1: 	c = len(x) 	listing = '' 	cur = 0 	for addy in x: 		listing = listing + '' 		#first build the addy with starting table elements 		#first record only 		if cur == 0: 			listing = listing + table + tr + td + addy + tde 		#Now check if this is an odd seq item, 		#just add a new cell and end the row 		if isodd(cur): 			listing = listing + td + addy + tde + tre 		#if this is an even item, it should be on a new row 		#and NOT the first item 		if iseven(cur) and cur <> 0: 			listing = listing + tr + td + addy + tde 			#all good, but if last record, then end row 			#else skip and loop to previous isodd and  			#add a new cell(which ends the row as well) 			if cur == (c - 1): 				listing = listing + tre 		#if this is the last item, end the table 		if cur == (c - 1): 			listing = listing + tablee 		cur = cur + 1 else: 	listing = 'No records'   return listing

And you return the results simply by calling the script in your dtml. something like: dtml-var “path.to.script(itemid=itemid)”

As always, I hope this help someone else out there, not just myself! Good luck!