0

下面的 cgi 脚本包含文件中的搜索条件。一个表单有四个字段 Fname、Lname、Age 和 Gender。如果用户在任一字段、二字段、三字段或四字段中输入数据,则应显示匹配记录。我如何获得它?对于所有领域。我想如何为所有条件写

#!/usr/bin/python
import os
import cgi


found = False

form = cgi.FieldStorage()
Fname = form.getvalue('firstname', '')
Lname = form.getvalue('lastname', '')
Age = form.getvalue('age', 0)
Gender = form.getvalue('gender', '')
fname = "/tmp/myfile.txt"

print "Content-type:text/html\n"
print "<html>"
print "<head>"
print "</head>"
print "<body>"
print "<h2><b><i>Search Data</i></b></h2>"
print "<table cellpadding='2' cellspacing='2' border='1'>"
print "<br>"
print "<td>First Name</td>"
print "<td>Last Name</td>"
print "<td>Age</td>"
print "<td>Gender</td>"
print "</tr>"


if os.path.exists(fname):

    f = open(fname, "r")

    b = []
    for line in f:
        temp = line.split()
        Fsearch = temp[0]
        Lsearch = temp[1]
        Asearch = temp[2]
        Gsearch = temp[3]



        if Fname and Fname.lower() == Fsearch.lower():
            found = True

        if Lname and Lname.lower() == Lsearch.lower():
            found = True

        if Age and Age == Asearch:
            found = True

        if Gender and Gender == Gsearch :
            found = True

        if found:
            b.append(line)


        found = False

    f.close()

    if len(b) == 0:
        print "<h2>", "No records found","</h2>"

    else:
        for each in b:
            store = each.split()

            print "<tr>"
            print "<td>", store[0], "</td>"
            print "<td>", store[1], "</td>"
            print "<td>", store[2], "</td>"
            print "<td>", store[3], "</td>"
            print "</tr>"


else:
    print "Search Other File"
                                                                                           print "</table>"
print "<br><a href='/~tom/index.html'>Home</a>"
print "</body>"
print "</html>"    

我有一个这样的文件:

Ram Charan 25 male
John Mckay 28 Male
Ashley Lobo 25 Female
Sara jane 28 Female

现在我的forn有四个字段:

FirsrName:
Last Name:
Age:
Gender(male Female Radio button)

如果用户在任一字段或两个字段或所有字段中输入数据,则应匹配数据并显示记录

例如,如果我输入:

Firstname: 
lastName:
Age: 25
Gender: Male (radio button)

它应该显示25岁的男性和男性的记录

如果用户在任何三个字段中输入数据:

FirstName: Ashley
Lastname: 
Age: 25
Gender: Female (radio button)

它应该显示匹配的记录。

同样,如果用户在所有字段中输入数据并且同样明智

4

0 回答 0