By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,933 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,933 IT Pros & Developers. It's quick & easy.

Python,mysql question

P: n/a
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?
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 18 Mar 2004 05:09:00 -0800, james blair wrote:
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?


What have you tried? What were the outcomes of your attempts?

--
\ "Truth would quickly cease to become stranger than fiction, |
`\ once we got as used to it." -- Henry L. Mencken |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #2

P: n/a
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)+
'&amp;start='+str(start-itemsperpage)+'">Previous</a> ')
*(start-itemsperpage>=0)
+('<a href="/script.py/search?itemsperpage='+str(itemsperpage)
+'&amp;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)+
'&amp;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)
+'&amp;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:ca**@jabber.linux.it
Jul 18 '05 #3

P: n/a
james blair wrote:
Hi
I have a search page & the reults for the search are fetched from the
database. so you are doing web stuff?
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?

basically you re-run the mysql query at each page request.
check mysql's limit and offset keywords:
<http://www.mysql.com/documentation/mysql/bychapter/manual_SQL_Syntax.html#SELECT>

--
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#me> a foaf:Person ; foaf:nick "deelan" ;
foaf:weblog <http://www.deelan.com/> .
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.