#!/usr/bin/env ruby # coding: utf-8 Encoding.default_external = 'utf-8' # UTF-8のCSVファイルを読むため require 'cgi' require 'csv' c = CGI.new(:accept_charset => "UTF-8") #データベース読み取り処理は全く同じ csv = CSV.read("bunbetsu3.csv", {:headers => true}) db = csv # あとで全データを使う場合にそなえcsv変数は温存 # 検索パターンはHTMLフォームへの入力値を取得する trash = c["gomi"] if trash > "" # 開講時期指定に何か文字列を入れたなら ptn = Regexp.new(trash) # 文字列を正規表現に変換 db = db.select {|row| ptn =~ row["品名"]} end # 23行の空白行必須。その行にスペースは一つでも入っていけない print("Content-type: text/html; charset=utf-8 <!DOCTYPE html> <html> <head><title>検索結果</title> <style type=\"text/css\"> <!-- h1 {color: black ; text-align: left} tr {color : black ; text-align: center} th {background:#e0ffff; color:inherit; text-align: center; padding:7px 5px 5px 5px; } td {color : inherit; ; text-align: center;padding:10px 0 5px 5px;} --> </style> </head> <body> ") puts("<h1>検索結果</h1>") puts("<table border=\"1\">") db.each{|row| printf("<tr><th>品名</th><th>ごみ区分</th><th>備考</th></tr><tr><td>%s</td><td>%s</td><td>%s</td></tr>", row["品名"], row["区分"], row["備考"])} puts(" </body> </html>")