[Zope] Using lists in DTML

Christopher J. Kucera ckucera@globalcrossing.com
Wed, 05 Apr 2000 15:18:02 -0500


Ken Kinder wrote:
> lists. How would I eliminate duplicates created by querying the
> same table?
> 
> Any ideas on what syntax I would use for that?

What kind of SQL statements are your running?

If you have the SQL queries returning the same data types, and you're
using a database that supports it, you could actually combine the SQL
statements into one with a UNION:

  select first, second from sometable where second='blahblah'
  union select first, second from sometable where third='foo'

(Although, that statement could easily be reduced to
 "... where second='blahblah' or third='foo'")  Depending on the flavor of
SQL, you might have to add another keyword in there somehwere to remove
duplicates.

If you'd still like to build a list of keys though, you could do the
following (untested):

<dtml-let mylist="[]">
  <ul>
  <dtml-in ..>
    <dtml-call "mylist.append(fieldname)">
    <li>..
  </dtml-in>
  <dtml-in ..>
    <dtml-try>
      <dtml-if "mylist.index(fieldname) > -1">
        <li>..
      </dtml-if>
    <dtml-except>
      Already had this item . . .
    </dtml-try>
  </dtml-in>
  </ul>
</dtml-let>

The dtml-try in there is because the "index" function on a list gives an
error if the item is not found.  I don't know of any function that'll just
return -1 or something, but I'm sure you could write one in python.

Hope that helps some,
-CJ