0

次のような高度なフォームがあります。

ここに画像の説明を入力

私のコードを含む Bootsnipp のテンプレートを使用しており、それを調整しようとしています -こちらから入手できます。

検索を行う私の方法は、検索に基づいて特定の URL パラメーターに値を追加し、PHP コードのさらに下で、それらのパラメーターが空白であるか値が含まれているかを確認し、データベースからオークションを取得するコードを実行することです。

これをいじり始めて以来、「州ごとにフィルター」ボタンがデータベースからの州フィルターを埋めていません (表示されるのは空白のオプションだけです)。また、私の検索ボタン (眼鏡のアイコン) には消えた。これが以前に機能していた理由はわかりません。私はPDOを使用していることに注意してください。フィルタ セクションの HTML および PHP コードは次のとおりです。

<!-- Page Content -->
<div class="container">
    <!--This is the search bar-->
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <!--                http://stackoverflow.com/questions/8476602/appending-get-parameters-to-url-from-form-action-->
<!--If this is a form it messes up the styling-->
                <div method="get" class="input-group" id="adv-search" action="listings.php">
                    <input type="text" name="q" class="form-control"
                           placeholder="Search for auctions by name or description" value=""/>
                    <div class="input-group-btn">
                        <div class="btn-group" role="group">
                            <div class="dropdown dropdown-lg">
                                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                        aria-expanded="false"><span class="caret"></span></button>
                                <div class="dropdown-menu dropdown-menu-right" role="menu">
                                    <form class="form-horizontal" role="form">
                                        <div class="form-group">
                                            <label for="filter">Sort By</label>
                                            <select class="form-control" name="sort" value="by_lowest_time">
                                                <!--                                                By default by lowest time remaining-->
                                                <option value="by_lowest_time" selected>Lowest Time Remaining</option>
                                                <option value="by_highest_time">Highest Time Remaining</option>
                                                <option value="by_lowest_price">Lowest Price</option>
                                                <option value="by_highest_price">Highest Price</option>
                                            </select>
                                        </div>
                                        <div class="form-group">
                                            <label for="filter">Filter by Category</label>
                                            <?php
                                            try {
                                                $catsql = 'SELECT * FROM ebay_clone.Category';
                                                $catq = $db->query($catsql);
                                                $catq->setFetchMode(PDO::FETCH_ASSOC);
                                            } catch (PDOException $e) {
                                                echo 'ERROR: ' . $e->getMessage();
                                            }
                                            ?>
                                            <select class="form-control" name="cat">
                                                <option value=""></option>
                                                <!--                                                http://stackoverflow.com/questions/3078192/how-to-set-html-value-attribute-with-spaces-using-php-->
                                                <?php while ($r = $catq->fetch()): ?>
                                                    <option
                                                        value="<?php echo str_replace(' ', '_', strtolower(htmlspecialchars($r['item_category']))); ?>"> <?php echo htmlspecialchars($r['item_category']) ?></option>
                                                <?php endwhile; ?>
                                            </select>
                                        </div>

                                        <div class="form-group">
                                            <label for="filter">Filter by State</label>
                                            <?php
                                            try {
                                                //         error_reporting(E_ERROR | E_PARSE);
                                                $statsql = 'SELECT * FROM ebay_clone.State';
                                                $statq = $db->query($statsql);
                                                $statq->setFetchMode(PDO::FETCH_ASSOC);
                                            } catch (PDOException $e) {
                                                echo 'ERROR: ' . $e->getMessage();
                                            }
                                            ?>
                                            <select class="form-control" name="state">
<!--                                                Blank option-->
                                                <option value=""></option>
                                                <?php while ($r = $statq->fetch()): ?>
                                                    <option value="<?php echo trimstr_replace(' ', '_', strtolower(htmlspecialchars($r['state']))); ?>"> <?php echo htmlspecialchars($r['state']) ?> </option>
                                                <?php endwhile; ?>
                                            </select>
                                        </div>
                                        <button type="button" value="search" class="btn btn-primary"><span
                                                class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
                                    </form>

                                </div>

                            </div>
                            <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-search"
                                                                                aria-hidden="true"></span></button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
4

2 に答える 2

0

私はこれを理解しました。それは、SQL ステートメントを変更し、新しい SQL ステートメントを新しいクエリに再割り当てすることに関係していました。また、データベース名を参照する必要はなく、データベース内のテーブルのみを参照する必要がありました。

これは今動作します:

<!--This is the search bar-->
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <!--                http://stackoverflow.com/questions/8476602/appending-get-parameters-to-url-from-form-action-->
                <!--If this is a form it messes up the styling-->
                <div class="input-group" id="adv-search">
                    <form method='get' action='listings.php'>
                        <input type="text" name="q" class="form-control"
                               placeholder="Search for auctions by name or description" value=""/>
                    </form>

                    <div class="input-group-btn">

                            <div class="btn-group" role="group">
                                <div class="dropdown dropdown-lg">
                                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                            aria-expanded="false"><span class="caret"></span></button>
                                    <div class="dropdown-menu dropdown-menu-right" role="menu">
                                        <form class="form-horizontal" role="form">
                                            <div class="form-group">
                                                <label for="filter">Sort By</label>
                                                <select class="form-control" name="sort" value="by_lowest_time">
                                                    <!--                                                By default by lowest time remaining-->
                                                    <option value="by_lowest_time" selected>Lowest Time Remaining
                                                    </option>
                                                    <option value="by_highest_time">Highest Time Remaining</option>
                                                    <option value="by_lowest_price">Lowest Price</option>
                                                    <option value="by_highest_price">Highest Price</option>
                                                </select>
                                            </div>
                                            <!-- Item Category -->
                                            <div class="form-group">
                                                <label for="filter">Filter by Category</label>
                                                <select class="form-control" id="item-category" name="cat"
                                                        class="form-control">
                                                    <option value="" selected disabled hidden>Please Select a Category
                                                    </option>
                                                    <?php $sql = 'SELECT * FROM Category';
                                                    foreach ($db->query($sql) as $row) { ?>
                                                        <option
                                                            value="<?php echo $row['item_category']; ?>"><?php echo htmlspecialchars($row['item_category']); ?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <!-- Item State -->
                                            <div class="form-group">
                                                <label for="filter">Filter by State</label>
                                                <select class="form-control" id="item-state" name="state"
                                                        class="form-control">
                                                    <option value="" selected disabled hidden>Please Select a
                                                        Condition
                                                    </option>
                                                    <?php $sql = 'SELECT * FROM State';
                                                    foreach ($db->query($sql) as $row) { ?>
                                                        <option
                                                            value="<?php echo $row['state']; ?>"><?php echo htmlspecialchars($row['state']); ?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <button type="submit" action="listings.php" method="get" value="search"
                                                    class="btn btn-primary"><span
                                                    class="glyphicon glyphicon-search" aria-hidden="true"></span>
                                            </button>
                                        </form>

                                    </div>

                                </div>

                                <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"
                                                                                    aria-hidden="true"></span></button>


                            </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
于 2016-03-02T11:46:48.903 に答える
0

あなたの while ループは$row変数を埋めます

<?php while ($row = $statq->fetch()): ?>

しかし、あなたは$rそれを読むために使用しています:

$r['state']
于 2016-03-02T10:58:33.490 に答える