Connecting Tech Pros Worldwide Forums | Help | Site Map

HTML Tables

Amie
Guest
 
Posts: n/a
#1: Aug 18 '08
Afternoon all.

Just want to know how to create html tables using a for loop.
I need to display 34 html tables, so I figured a for loop will do.
Please show me an example of how to do that.
Also how do I display the results of an sql query onto the html
tables?

Thanks in advance

Adrian Smith
Guest
 
Posts: n/a
#2: Aug 18 '08

re: HTML Tables


On Aug 18, 7:16*pm, Amie <sthembileng...@gmail.comwrote:
Quote:
Afternoon all.
>
Just want to know how to create html tables using a for loop.
I need to display 34 html tables, so I figured a for loop will do.
Please show me an example of how to do that.
for i in range(33):
print "<table border>"
for j in range(numberofrows-1):
print "<tr><td>stuff</td><td>stuff</td><td>stuff</td></tr>"
print "</table>"

Or something like that. You could have for loops for the individual
rows, too.
Gabriel Genellina
Guest
 
Posts: n/a
#3: Aug 18 '08

re: HTML Tables


En Mon, 18 Aug 2008 09:05:30 -0300, Adrian Smith <adrian_p_smith@yahoo.comescribió:
Quote:
On Aug 18, 7:16*pm, Amie <sthembileng...@gmail.comwrote:
Quote:
>Afternoon all.
>>
>Just want to know how to create html tables using a for loop.
>I need to display 34 html tables, so I figured a for loop will do.
>Please show me an example of how to do that.
>
for i in range(33):
print "<table border>"
for j in range(numberofrows-1):
print "<tr><td>stuff</td><td>stuff</td><td>stuff</td></tr>"
print "</table>"
>
Or something like that. You could have for loops for the individual
rows, too.
This can be done, but easily becomes unmaintainable.
I'd use a template engine - or at least a library for generating HTML. See this recent thread:
http://groups.google.com/group/comp....0811516b29fcc/

--
Gabriel Genellina

Fredrik Lundh
Guest
 
Posts: n/a
#4: Aug 18 '08

re: HTML Tables


Adrian Smith wrote:
Quote:
Quote:
>Just want to know how to create html tables using a for loop.
>I need to display 34 html tables, so I figured a for loop will do.
>Please show me an example of how to do that.
>
for i in range(33):
range(33) returns 33 numbers, not 34 (the upper bound is not inclusive).
Quote:
print "<table border>"
for j in range(numberofrows-1):
This is also off by one.
Quote:
print "<tr><td>stuff</td><td>stuff</td><td>stuff</td></tr>"
print "</table>"
One extra tip: if you're printing arbitrary strings, you need to encode
them (that is, map &<to HTML entities, and perhaps also deal with
non-ASCII characters). simple ascii-only version:

from cgi import escape

print "<tr><td>", escape("stuff"), </td><td>" # etc

(the above inserts extra spaces around the cell contents, but browsers
don't care about those)

The "escape" function from the cgi module only works for (ascii)
strings; to be able to escape/encode arbitrary data, you'll need a
better escape function, e.g. something like:

import cgi

def escape(text):
if isinstance(text, unicode):
return cgi.escape(text).encode("utf-8")
elif not isinstance(text, str):
text = str(text)
return cgi.escape(text)

The above escapes Unicode strings and encodes them as UTF-8; 8-bit
strings are escaped as is, and other objects are converted to strings
before being escaped.

Tweak as necessary.

</F>

Adrian Smith
Guest
 
Posts: n/a
#5: Aug 19 '08

re: HTML Tables


On Aug 19, 6:49*am, Fredrik Lundh <fred...@pythonware.comwrote:
Quote:
Adrian Smith wrote:
Quote:
Quote:
Just want to know how to create html tables using a for loop.
I need to display 34 html tables, so I figured a for loop will do.
Please show me an example of how to do that.
>
Quote:
for i in range(33):
>
range(33) returns 33 numbers, not 34 (the upper bound is not inclusive).
Oops! Thought it went over 0-33, my bad.
Closed Thread