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 回答