Search code examples
pythonphphtmlcgi

How to display table with borders using python CGI script that is displayed in php page


I am trying to display results in a table format (with borders) in a cgi script written in python. How to display table boarders within python code.

  check_list = elements.getvalue('check_list[]')
mydata=db.get_data(check_list)
print("_____________________________________")

print("<table><th>",check_list[0],"</th> <th>",check_list[1],"</th>")
 i = 0 #here
for data in mydata:
    print("<tr><td>",data[0],"</td> <td>",data[1],"</td></tr>")
     i += 1 #here
     if i == 2:#here
         break#here 
print("</table>")

the checklist elements represent columns names from a sqlite table. and based on the selected columns the data will be shown. This code result is just showing two records result with no borders in a php page.


Solution

  • You need CSS styling to define the visuals of your table. Here is a quick introduction to it: https://www.w3schools.com/css/css_table.asp

    Also, your code tries to do alternating colors for rows, which is nice. CSS supports this, look at this article: https://www.w3.org/Style/Examples/007/evenodd.en.html This is the part you should look at:

    tr:nth-children