Can't get advanced search dropdown values to fill via PHP

时间:2016-03-02 10:54:16

标签: php pdo get form-submit advanced-search

I have an advanced form that looks like this:

enter image description here

I am using a template from Bootsnipp which contains my code, and am trying to adjust it - available here.

My method of doing search is to append values to specific URL parameters based on the search, and then further down in the PHP code, check if those parameters are blank or contain values, and then execute code which fetches auctions from a database.

Since I started playing around to do this, the "Filter By State" buttons are not filling in the states filters from the database (the only thing that appears is the blank option), and also my search buttons (with the eyeglass icons) have disappeared. No idea why as this was working before. Note I am using PDO. Here is HTML and PHP code for the filter section:

<!-- 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>

2 个答案:

答案 0 :(得分:0)

Your while loop fills a $row variable

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

But you are using $r to read it:

$r['state']

答案 1 :(得分: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>