0

これらのコードを Web から取得し、思いどおりに機能するようにまとめました。簡単なphp検索から始めて...

検索スクリプト - http://php.about.com/od/phpwithmysql/s/php_search.htm

google… mysql の照合

自分の仕事 - 大文字と小文字を区別するフィルターの試行に成功

検索語を強調表示する (ここから stackoverflow) - mysql & php 検索の強調表示

これらのコードをオンラインで共有し、stackoverflow で膨大な知識を提供してくれた人々に感謝します。

コードを使用して、結果を取得した後、ワインの在庫を更新するオプションが必要です。どこから始めればいいのかわからず、勉強する例が見つかりません。私を正しい道へと導いてください。ありがとうございました。

次にコードを必要とする人のための完全なコードを次に示します。

<p align="center">Search Red Wines</p></h1>
//search from - http://php.about.com/od/phpwithmysql/ss/php_search.htm
<form name="search" method="post" action="<?= $PHP_SELF ?>">
    <table width="720" border="0" align="center"
           style="margin-right:auto; margin-left:auto;">
        <tr>
            <td valign="middle"><label>Search for:
                <input type="text" name="find"/></label>
            </td>
            <!-- <select name="field">
            <option VALUE="name">Name</option>
            <option VALUE="location">Location</option>
            </select> -->

            <td valign="middle">&nbsp;&nbsp;&nbsp;&nbsp;<label>Name<input
                        type="checkbox" name="field" value="name"></label></td>
            <td valign="middle">&nbsp;&nbsp;&nbsp;&nbsp;<label>Location<input
                        type="checkbox" name="field" value="location"></label>
            <td valign="middle">&nbsp;&nbsp;&nbsp;&nbsp;<label>Tasting
                    note<input type="checkbox" name="field" value="tastingnote"></label>
            </td>
            <td>&nbsp;&nbsp;&nbsp;
                <input type="hidden" name="searching" value="yes"/>
                <input type="submit" name="search" value="Search"/>
            </td>
        </tr>
    </table>
</form>

<table width="600" border="0" align="center"
       style="margin-right:auto; margin-left:auto;">
    <tr>
        <td>
<?
//This is only displayed if they have submitted the form
$find = $_POST['find'];
$field = $_POST['field'];
$searching = $_POST['searching'];

if ($searching == "yes") {
    //echo "<h2>Results</h2><p>";

    //If they did not enter a search term we give them an error
    if ($find == "") {
        echo "<p>You forgot to enter a search term or choose checkboxs";
        exit;
    }

    // Otherwise we connect to our Database
    mysql_connect("localhost", "chadaveg_bon", "TingTong12") or die(mysql_error());
    mysql_select_db("chadaveg_wine") or die(mysql_error());

    // We preform a bit of filtering
    //$find = strtoupper($find); //make all upper case
    //$find = ucfirst($find);
    $find = strip_tags($find);
    $find = trim($find);

    //And we remind them what they searched for
    $find = mysql_real_escape_string($find);
    echo "<br><font size=5>Searched results for:</b> " . $find;
    echo "</font><br><br>";

    //Now we search for our search term, in the field the user specified
    $data = mysql_query("
        SELECT * from
        (select * FROM reserveredwine WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
        union
        select * from pinotnoir WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
        union
        select * from redwines1 WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
        union
        select * from redwines2 WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
        union
        select * from redwines3 WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
        union
        select * from redwines4 WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
        union
        select * from fortifiedandsweetwine WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE))
        a order by grape, name
    ");

    while ($result = mysql_fetch_array($data)) {    
        //And we display the results
        //highlight code - http://php.about.com/od/phpwithmysql/ss/php_search.htm

        $explode_criteria = explode(" ", $find);
        $highlight        = $result['name'] . "&nbsp;&nbsp;&nbsp;" . $result['grape'] . "&nbsp;&nbsp;&nbsp;" . $result['year'] . "&nbsp;&nbsp;" . "-&nbsp;&nbsp;" . "\$" . $result['price'] . "<br>" . "&nbsp;&nbsp;&nbsp;&nbsp;" . $result['location'] . "&nbsp;-&nbsp;" . $result['instock'] . "&nbsp;in stocks"; // capture $result['name'] here
        foreach ($explode_criteria as $key) {    
            //filter case sensitive search words
            if ($field == 'location' || $field == 'tastingnote') {
                $key = strtolower($key);
            } else {
                $key = ucfirst($key);
            }

            // escape the user input
            $key2 = preg_quote($key, '/');

            // keep affecting $highlight
            $highlight = preg_replace("/" . $key2 . "/", "<span style='box-shadow: 0px 0px 8px 3px #888888; background-color: #FF9900; font-weight: bold; padding: 2px' >" . $key . "</span>", $highlight);
            //another way to add more result
            //echo "<br>";
            //echo '<td>' . $result['name'] . '</td>';
            //echo '<td>' . $result['price'] . '</td>';
        }
        echo $highlight;
        //add label picture echo "<img src='../" . $result['label image'] ."'>";
        echo "<br>";
        echo "<br>";
    }
    //This counts the number or results - and if there wasn't any it gives them a little message explaining that
    $anymatches = mysql_num_rows($data);
    if ($anymatches == 0) {
        echo "Sorry, but we can not find an entry to match your query<br><br>";
    }
}
?>
4

1 に答える 1