简体   繁体   中英

Filtering search results PHP

I'm trying to give a user the ability to filter search results by ascending or descending order. It isn't working.

I think it is an issue with my select or post method or something.

What could be the reason why it isn't working?

<?php
$search = "";
if(isset($_POST["search"])){
    $search = $_POST["search"];
    $Ascending= $_POST["Sort By"];
    $Descending= $_POST["Sort By"];
}
?>
    <form method="POST">
        <input type="text" name="search" placeholder="Search for Question"
               value="<?php echo $search;?>"/>
        <label for="Sort">SortBy:</label>
        <select id="SortBy" name="Sort By">
            <option value="Ascending">Ascending Order</option>
            <option value="Descending">Descending Order</option>
            <input type="submit"
        </select>
    </form>
<?php
if(isset($Ascending)) {
    if (isset($search)) {
        require("common.inc.php");
        $query = file_get_contents(__DIR__ . "/queries/SearchTableASC.sql");
        if (isset($query) && !empty($query)) {
            try {
                $stmt = getDB()->prepare($query);
                //Note: With a LIKE query, we must pass the % during the mapping
                $stmt->execute([":question" => $search]);
                //Note the fetchAll(), we need to use it over fetch() if we expect >1 record
                $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
    }
}
if(isset($Descending)){
    if (isset($search)) {
        require("common.inc.php");
        $query = file_get_contents(__DIR__ . "/queries/DescendingOrder.sql.sql");
        if (isset($query) && !empty($query)) {
            try {
                $stmt = getDB()->prepare($query);
                //Note: With a LIKE query, we must pass the % during the mapping
                $stmt->execute([":question" => $search]);
                //Note the fetchAll(), we need to use it over fetch() if we expect >1 record
                $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
    }

}
?>
    <!--This part will introduce us to PHP templating,
    note the structure and the ":" -->
    <!-- note how we must close each check we're doing as well-->
<?php if(isset($results) && count($results) > 0):?>
    <p>This shows when we have results</p>
    <ul>
        <!-- Here we'll loop over all our results and reuse a specific template for each iteration,
        we're also using our helper function to safely return a value based on our key/column name.-->
        <?php foreach($results as $row):?>
            <li>
                <?php echo get($row, "question")?>
                <a href="delete.php?QuestionId=<?php echo get($row, "id");?>">Delete</a>
            </li>
        <?php endforeach;?>
    </ul>
<?php else:?>
    <p>This shows when we don't have results</p>
<?php endif;?>

You are not using the incoming parameters correctly:

$Ascending= $_POST["Sort By"]; 
$Descending= $_POST["Sort By"];

Here you set ascending and descending to the same value.

You should do something like:

$ascOrDec = $_POST['Sort By'];

And than check the value of $ascOrDec before you choose your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM