0

My code allows a person to enter keyword(s) in a provided space and clicking the search button, searches a database for those keywords and displays url(s) that have that keyword in it's row. The code preforms a union search automatically.

I have added two radio buttons to my page named union and intersect. How do I change my code to allow the user to choose what search they do by selecting a radio button?

#!/usr/bin/env python2.7

import sys
import sqlite3
from string import Template
from cgi import FieldStorage
from dateutil import parser
from textwrap import dedent
from eicpy.www import CGIHandler
from eicpy.sql import ezsql

from eicpy import www
from eicpy import cgierror
cgierror.enable(display=True)

DB = ezsql().connect("/usr/HTTPServer/dev7/data/asq1.db")

class Page(CGIHandler):

def handle1(self):
   search_terms = self.fs.getfirst("searchinp", None)
   if search_terms:
      fields = search_terms.split(" ")
      steps = []
      apps = []

      for field in fields:
        recs = DB.execute("select issue, url, steps from help where k1='%s' or
        k2='%s' or k3='%s' or k4='%s' or k5='%s' or k6='%s' or k7='%s' or
        k8='%s' or k9='%s' or k10='%s' or k11='%s' or k12='%s' or k13='%s'" %
        (field, field, field, field, field, field, field, field, field, field,
        field, field, field)).fetchall()

#self.debug(recs)

for r in recs:
  if r.steps == "S":
    steps.append({
    "issue" : r.issue,
    "size" : "600x460",
    "location" : "/" + r.url
    })

if r.steps =="A":
    apps.append({
    "issue" : r.issue,
    "size" : "600x460",
    "location" : "/" + r.url
    })

#self.debug(steps)
#self.debug(apps)

self.show({"steps": steps, "apps": apps})

else :
   self.show({})

if __name__ == '__main__':
    p = Page('Agent Support Query'
        ,'pt/asq.xml'
        , ['/styles/asq.css'])
    p.handle1()

edit: HTML/XML code:

<html>
  <head>
    <title>Agent support Query</title>
    <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
    <link rel="stylesheet" type="text/css" href="/styles/asq.css" />
    <script type="text/javascript" src="/yui/yahoo-dom-event/yahoo-dom-event.js">
       </script>
    <script type="text/javascript" src="/yui/connection/connection-min.js"></script>
    <script type="text/javascript" src="/scripts/eiLib.js"></script>
    <script type="text/javascript" src="/scripts/win7.js"></script>
  </head>

  <body>
    <div id="container">
      <div id="header">
        <div id="body">
          <div id="hd">
            <a href="/"><img src="/images/eic_small.jpg" width="145" height="45" /></a>
          </div><!-- hd -->
          <div id="title">
            <div><h1>Agent Support Query</h1></div>
          </div><!-- title -->
        </div><!-- body -->
      </div><!-- header -->
      <form action="#">
        <div id="searchspace">
          <div id="searchform">
            <div id="radio">
              Union<input type="radio" name="accept" value="iaccept">
                <br>
              Intersect<input type="radio" name="accept" value="notaccept">
            </div><!-- radio -->
            <div id="dropbox">
              <select>
                <option>keyword</option>
                <option></option>
                <option></option>
              </select>
            </div> <!-- dropbox -->            
            <div id="keyword">keyword(s) -</div>
              <input type="text" id="searchinp" name="searchinp" />
              <input type="submit" id="srch" name="srch" value="?" />
          </div><!-- searchform -->
        </div><!-- searchspace -->

    <!-- This is the important bits for creating the help links -->

        <div id="help">
          <div id="steps">
            <div tal:condition="exists:vars/steps">
              <div tal:repeat="item vars/steps">
                <h1 tal:attributes="onclick string:window.open('${item/location}',
                   'popup',
                      '${item/size},scrollbars=yes')"
                      tal:content="item/issue"></h1>
              </div><!-- tal:repeat -->
            </div><!-- tal:condition -->
         </div><!-- steps -->
         <div id="append">
           <div tal:condition="exists:vars/apps">
             <div tal:repeat="item vars/apps">
               <h1 tal:attributes="onclick string:window.open('${item/location}',
                   'popup',
                    '${item/size},scrollbars=yes')"
                     tal:content="item/issue"></h1>
             </div><!-- tal:repeat -->
           </div><!-- tal:condition -->
      </div><!-- append -->
    </div><!-- help -->
   </form>
  </div><!-- container -->
 </body>
</html>
Justin Gould
  • 23
  • 1
  • 1
  • 5
  • 1
    Wouldn't that involve just changing all of the `OR` SQL operators to `AND`? – cdhowie Jan 04 '13 at 15:59
  • can you provide some HTML so that we can see the form? – jdotjdot Jan 04 '13 at 16:43
  • @cdhowie: yes if I wanted to make the changes permanent but I don't. I want the user to be able to choose (via 2 radio buttons on the webpage) what type of search they want to do. They can either do a union(default) search or choose to do a intersection search. – Justin Gould Jan 04 '13 at 19:05
  • @jdotjdot: I added the html. – Justin Gould Jan 04 '13 at 20:32
  • @JustinGould So accept a true/false argument, and construct the query using `AND` or `OR` depending on the argument? – cdhowie Jan 04 '13 at 22:03

0 Answers0