0

以下の cgi スクリプトには、ファイル内の検索条件が含まれています。フォームには、Fname、Lname、Age、および Gender の 4 つのフィールドがあります。ユーザーが one フィールド、two フィールド、three フィールド、または four フィールドのいずれかにデータを入力すると、一致レコードが表示されます。どうすればそれを取得できますか? すべてのフィールドに。すべての条件についてどのように書くと思いますか

#!/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 ha 4フィールド:

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

ユーザーがいずれか 1 つまたは 2 つのフィールド、またはすべてのフィールドにデータを入力すると、データが一致し、レコードが表示されます。

たとえば、次のように入力した場合:

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

25歳の男性と男性の記録が表示されるはずです

ユーザーが次のように 3 つのフィールドにデータを入力した場合:

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

一致したレコードが表示されます。

同様に、ユーザーがすべてのフィールドにデータを入力すると、同様に賢明です

4

0 に答える 0