Returning database results similar to google search
I'm fairly new to Zope, I have been searching for a number of hours on my most recent dilemma. I have a database that's full of information I'd like to return. I created a Z SQL method to select * from table, and using <dtml-in ...> I can return all the results I need, but there are too many. I'd like to only show 5 at a time, then have a links at the bottom so you can jump to database results 1-5, 6-10, 11-15, etc (much like at the bottom of a google results page). I need these links to be generated dynamically (the database grows weekly), dependent on how many results I have from the database. I have come up with a number of ideas, but they are primarily "unsuccessful hacks" ... is there a good "Zopeish way" I can acomplish this, or at least point me in the right direction? Thanks. -- Elizabeth K. // Lyz@PrincessLeia.com http://www.princessleia.com http://www.wallaceandgromit.net
Get the PloneBatch module from Plone. It runs also outside Zope and it does exactly what you want. PloneBatch is well documented (check plone.org). -aj --On Freitag, 5. Dezember 2003 15:12 Uhr -0500 Lyz@PrincessLeia.com wrote:
I'm fairly new to Zope, I have been searching for a number of hours on my most recent dilemma.
I have a database that's full of information I'd like to return. I created a Z SQL method to select * from table, and using <dtml-in ...> I can return all the results I need, but there are too many.
I'd like to only show 5 at a time, then have a links at the bottom so you can jump to database results 1-5, 6-10, 11-15, etc (much like at the bottom of a google results page). I need these links to be generated dynamically (the database grows weekly), dependent on how many results I have from the database. I have come up with a number of ideas, but they are primarily "unsuccessful hacks" ... is there a good "Zopeish way" I can accomplish this, or at least point me in the right direction?
Thanks.
On Fri, 2003-12-05 at 12:12, Lyz@PrincessLeia.com wrote:
I'm fairly new to Zope,
Welcome.
I have a database that's full of information I'd like to return. I created a Z SQL method to select * from table, and using <dtml-in ...> I can return all the results I need, but there are too many.
I'd like to only show 5 at a time, then have a links at the bottom so you can jump to database results 1-5, 6-10, 11-15, etc (much like at the bottom of a google results page).
Here's an untested, top-of-head Python Script that assumes you pass in a variable called offset that defaults to 0. ---- records = context.your_zsql_method(args) batch_size = 5 last_record = min([offset+batch_size,len(records)]) for record_num in range(offset, last_record): print records[record_num]['some_info'] for page in range(0,len(results),batch_size): print '<a href=your_url?offset=%s>%s</a>' % (page,page/batch_size) return printed ----- It's incomplete and ugly, but should get you well on your way. HTH, Dylan
On Fri, 2003-12-05 at 13:08, Dylan Reinhardt wrote:
---- records = context.your_zsql_method(args) batch_size = 5
last_record = min([offset+batch_size,len(records)])
for record_num in range(offset, last_record): print records[record_num]['some_info'] for page in range(0,len(results),batch_size):
should be records---------^^^^^^^
print '<a href=your_url?offset=%s>%s</a>' % (page,page/batch_size) return printed -----
On 05 Dec 2003 13:08:01 -0800 GMT Dylan Reinhardt asked the Zope mailinglist about the following:
On Fri, 2003-12-05 at 12:12, Lyz@PrincessLeia.com wrote:
I'm fairly new to Zope,
Welcome.
I have a database that's full of information I'd like to return. I created a Z SQL method to select * from table, and using <dtml-in ...> I can return all the results I need, but there are too many.
I'd like to only show 5 at a time, then have a links at the bottom so you can jump to database results 1-5, 6-10, 11-15, etc (much like at the bottom of a google results page).
Here's an untested, top-of-head Python Script that assumes you pass in a variable called offset that defaults to 0.
records = context.your_zsql_method(args) batch_size = 5
last_record = min([offset+batch_size,len(records)])
for record_num in range(offset, last_record): print records[record_num]['some_info']
for page in range(0,len(results),batch_size): print '<a href=your_url?offset=%s>%s</a>' % (page,page/batch_size) return printed
It's incomplete and ugly, but should get you well on your way.
It also fetches the entire table of data from the external database one every call, so unless you do caching, it is quite inefficient. Either make sure these database results are cached, or check the syntax for returning segments of results from your RMDBS and apply Dylan's offset-logic to the ZSQL-method, instead of to the entire resultset. :) -- __________________________________________________________________ Geir Bækholt · Interaction Architect · Plone Solutions Development · Training · Support · http://www.plonesolutions.com __________________________________________________________________
On Fri, 2003-12-05 at 13:53, Geir Bækholt wrote:
On 05 Dec 2003 13:08:01 -0800 GMT Dylan Reinhardt asked the Zope mailinglist about the following:
It's incomplete and ugly, but should get you well on your way.
It also fetches the entire table of data from the external database one every call, so unless you do caching, it is quite inefficient.
Good catch... that's an assumption I should have made explicit. The OP said the database would be updated "weekly" so caching should be easy. The suggestion to retrieve only those records you want displayed is probably a better all-around solution, however. Thanks, Dylan
On Fri, Dec 05, 2003 at 01:08:01PM -0800, Dylan Reinhardt wrote:
Here's an untested, top-of-head Python Script that assumes you pass in a variable called offset that defaults to 0.
---- records = context.your_zsql_method(args) batch_size = 5
last_record = min([offset+batch_size,len(records)])
for record_num in range(offset, last_record): print records[record_num]['some_info']
for page in range(0,len(results),batch_size): print '<a href=your_url?offset=%s>%s</a>' % (page,page/batch_size) return printed -----
It's incomplete and ugly, but should get you well on your way.
HTH,
Dylan
This is exactly what I needed to get me on my way. Thanks so much! -- Elizabeth K. // Lyz@PrincessLeia.com http://www.princessleia.com http://www.wallaceandgromit.net
Lyz@PrincessLeia.com wrote:
On Fri, Dec 05, 2003 at 01:08:01PM -0800, Dylan Reinhardt wrote:
Here's an untested, top-of-head Python Script that assumes you pass in a variable called offset that defaults to 0.
---- records = context.your_zsql_method(args) batch_size = 5
last_record = min([offset+batch_size,len(records)])
for record_num in range(offset, last_record): print records[record_num]['some_info']
for page in range(0,len(results),batch_size): print '<a href=your_url?offset=%s>%s</a>' % (page,page/batch_size) return printed -----
It's incomplete and ugly, but should get you well on your way.
This is exactly what I needed to get me on my way. Thanks so much!
See also http://zope.org/Members/peterbe/DTML2ZPT/#example4 http://www.zopelabs.com/cookbook/1015785843 http://www.zopelabs.com/cookbook/1019946889 and ZTUtils.Batch in http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/AppendixB.stx --jcc -- "My point and period will be throughly wrought, Or well or ill, as this day's battle's fought."
participants (5)
-
Andreas Jung -
Dylan Reinhardt -
Geir Bækholt -
J. Cameron Cooper -
Lyz@PrincessLeia.com