Python,mysql question
Stefano Cavallari
stefano at cavallari.cjb.net
Thu Mar 18 08:44:59 EST 2004
Il gio, 2004-03-18 alle 14:09, james blair ha scritto:
> Hi
> I have a search page & the reults for the search are fetched from the
> database.
> I want that on the results page only first 25 results are shown & a
> link "next 25 results" be shown.Same for the 2nd result page.
> How do I do this?
here is how I ( a python newbie :) ) do it, using mod_python for apache
and ... sorry for the last req.write that is quite hard to read
def search(req,start="0",itemsperpage="5"):
req.write("<html>" etc etc... )
start=int(start)
itemsperpage=int(itemsperpage)
curs1=db1.cursor()
curs1.execute("""SELECT ... FROM ... WHERE
ORDER BY ... LIMIT %s,%s
;""",(start,itemsperpage+1) )
for i in xrange(itemsperpage):
row=curs1.fetchone()
if row!=None:
req.write(repr(row)) # change to something more useful
row=curs1.fetchone() # any row left for next page?
req.write((
'<a href="/script.py/search?itemsperpage='+str(itemsperpage)+
'&start='+str(start-itemsperpage)+'">Previous</a> ')
*(start-itemsperpage>=0)
+('<a href="/script.py/search?itemsperpage='+str(itemsperpage)
+'&start='+str(start+itemsperpage)+'">Next</a>')*
(row!=None))
the last req.write(..) could be rewritten as
if (start-itemsperpage>=0):
req.write('<a href="/script.py/search?itemsperpage='+str(itemsperpage)+
'&start='+str(start-itemsperpage)+'">Previous</a> ')
if row!=None:
#first line of "next page" is empty, so no "next page" is needed
req.write('<a href="/script.py/search?itemsperpage='+str(itemsperpage)
+'&start='+str(start+itemsperpage)+'">Next</a>')
which is far more readable.
Any comment is appreciated, it's one of the first python script I wrote.
HTH, HAND
--
Linux User #153639 RoLug Member - http://rovigo.linux.it
GnuPG id:E88F9363 keyserver.linux.it
Jabber id:cava at jabber.linux.it
More information about the Python-list
mailing list