【问题标题】:Php filter select optionPHP过滤器选择选项
【发布时间】:2020-07-21 23:05:10
【问题描述】:

我正在尝试使用选择下拉框创建过滤器选项,但我不知道我在哪里失败。我有一个完美的搜索栏,但我希望能够选择我想要搜索的位置。 例如,我可以输入我正在寻找的工作的名称,但我想过滤位置以仅在一个城市中查看 这是我的代码

编辑:感谢 ADyson,我对代码进行了一些编辑,我将在最后一个代码上发布新代码

        <?php
    require 'views/header.php';
    $connection = getDbConntection();

    // Search // 
    if (!empty($_GET['search'])) {

        $data = [
            'job_name' => '%' . $_GET['search'] . '%',
            'location_id' => $_GET['location_id']
        ];
        $searches = $connection->prepare("select jobs.id, jobs.name as job_name, salary as job_salary, description, location_id, domain_id , locations.name as location_name , domains.name as domain_name from jobs 
    LEFT JOIN locations ON jobs.location_id = locations.id
    LEFT JOIN domains on jobs.domain_id = domains.id "
                . "where jobs.name like :job_name"
                . 'AND location_id = :location_id');
        $searches->execute($data);
        $searches= $query->fetchAll();
    // List // 
    } else {
        $query = $connection->query("select jobs.id, jobs.name as job_name, salary as job_salary, description, location_id, domain_id , locations.name as location_name , domains.name as domain_name from jobs 
    LEFT JOIN locations ON jobs.location_id = locations.id
    LEFT JOIN domains on jobs.domain_id = domains.id ");
        $searches = $query->fetchAll();
    }
    ?>

    <div class="w3-row-padding w3-padding-64 w3-container">
        <div class="w3-content">

            <h1 class="center"> Jobs table </h1>
            <br>
            <form style="text-align:center" action="index.php" method="GET">
                <input type="text" name="search" value="Search jobs..." onfocus="this.value = ''" class="btn btn-danger">
                <select name="location_id"  class="btn btn-danger">
                    <?php foreach ($searches as $location): ?>
                        <?php $selectedText = ($location['id']) ?>
                        <option value= <?= $selectedText ?> > <?= $location['location_name'] ?></option>
                    <?php endforeach; ?>
                </select>
                <input type="submit" value="Search" class="btn btn-danger">
                <a href="index.php" class="btn btn-danger">Back to list </a>
            </form>
            <br>

            <div  class="center">
                <table>
                    <tr>
                        <th>ID</th>
                        <th> Job Name</th>
                        <th> Job Location</th>
                        <th> Job Domain</th>
                        <th> Job Description</th>
                        <th> Job Salary</th>

                        <th>Actions</th>
                    </tr>
                    <?php foreach ($searches as $key => $job_name) : ?>
                        <tr>
                            <th><?= $job_name['id'] ?></th>
                            <th style="background-color: lightskyblue"><?= $job_name['job_name'] ?></th>
                            <td><?= $job_name['location_name'] ?></td>
                            <td><?= $job_name['domain_name'] ?></td>
                            <td><?= $job_name['description'] ?></td>
                            <td><?= $job_name['job_salary'] ?></td>
                            <td> <a  class="btn btn-success" href="edit.php?id=<?= $job_name['id'] ?>">&nbsp; &nbsp; Edit &nbsp;</a> 
                                <a class="btn btn-danger" href="delete.php?id=<?= $job_name['id'] ?>">Delete</a> </td>
                        </tr>
                    <?php endforeach; ?>
                </table>  
            </div>

            <style>
                table td, table th {
                    padding: 15px;
                    text-align: center;

                }
                table th {
                    background:#3390FF;
                }
                table {
                    width: 100%;
                    border: 3px solid #ccc;
                    border-collapse: collapse;
                }

                .ce

nter {
                margin: auto;
                width: 100%;
                border: 3px solid red;
                padding: 10px;
                text-align: center;
            }
        </style>
    </div>
</div>

抱歉,代码混乱,我是一般编码新手

【问题讨论】:

  • 看来'location_id' =&gt; $_GET['search'] 应该改为'location_id' =&gt; $_GET['location_id']。现在,您正在使用“搜索”文本作为位置 ID!您稍后还会遇到语法错误 - AND location_id = ;location_id 需要将 ; 更改为 :
  • P.S.如果您进行了一些简单的调试并确保正确设置了错误日志记录,这些应该很容易发现。你有没有花时间学习如何调试?它几乎与编程本身一样重要。 phpknowhow.com/basics/basic-debugging 有一个使用 PHP 进行调试的简单指南。可能还有其他问题,通过快速阅读代码并不能立即发现。
  • 哦,$searchText = $_GET['search']; 是多余的,因为您从不在该行之后使用 $searchText 变量。它没有目的。您可以简单地删除这行代码。
  • 感谢帮助,我在此处发布代码并更改后注意到语法错误和逻辑错误。运行代码后,我得到了这个
  • Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter is not defined in C:\xampp\htdocs\TemaCarantina\index.php:17 Stack trace: #0 C:\xampp\htdocs \TemaCarantina\index.php(17): PDOStatement->execute(Array) #1 {main} 在 C:\xampp\htdocs\TemaCarantina\index.php 第 17 行抛出错误

标签: php mysql


【解决方案1】:

感谢 ADysom 我解决了问题,这是正确的代码

<?php
require 'views/header.php';
$connection = getDbConntection();

$locations = $connection->query("select * from locations");

// Search // 
if (!empty($_GET['search'])) {

    $data = [
        'job_name' => '%' . $_GET['search'] . '%',
        'location_id' => $_GET['location_id']
    ];
    $query = $connection->prepare("select jobs.id, jobs.name as job_name, salary as job_salary, description, location_id, domain_id , locations.name as location_name , domains.name as domain_name from jobs 
LEFT JOIN locations ON jobs.location_id = locations.id
LEFT JOIN domains on jobs.domain_id = domains.id "
            . "where jobs.name like :job_name "
            . "AND location_id = :location_id ");
    $query->execute($data);
    $query = $query->fetchAll();
//    print_r($_GET);
// List // 
} else {
    $query = $connection->query("select jobs.id, jobs.name as job_name, salary as job_salary, description, location_id, domain_id , locations.name as location_name , domains.name as domain_name from jobs 
LEFT JOIN locations ON jobs.location_id = locations.id
LEFT JOIN domains on jobs.domain_id = domains.id ");
    $query = $query->fetchAll();
}
?>

<div class="w3-row-padding w3-padding-64 w3-container">
    <div class="w3-content">

        <h1 class="center"> Jobs table </h1>
        <br>
        <form style="text-align:center" action="index.php" method="GET">
            <input type="text" placeholder='Search jobs..' name="search"  onfocus="this.value = ''" class="btn btn-danger">
            <select name="location_id"   class="btn btn-danger">
                <?php foreach ($locations as $location): ?>
                    <option value="<?= $location['id'] ?>">
                        <?= $location['name'] ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <input type="submit" value="Search" class="btn btn-danger">
            <a href="index.php" class="btn btn-danger">Back to list </a>
        </form>
        <br>

        <div  class="center">
            <table>
                <tr>
                    <th>ID</th>
                    <th> Job Name</th>
                    <th> Job Location</th>
                    <th> Job Domain</th>
                    <th> Job Description</th>
                    <th> Job Salary</th>

                    <th>Actions</th>
                </tr>
                <?php foreach ($query as $key => $job_name) : ?>
                    <tr>
                        <th><?= $job_name['id'] ?></th>
                        <th style="background-color: lightskyblue"><?= $job_name['job_name'] ?></th>
                        <td><?= $job_name['location_name'] ?></td>
                        <td><?= $job_name['domain_name'] ?></td>
                        <td><?= $job_name['description'] ?></td>
                        <td><?= $job_name['job_salary'] ?></td>
                        <td> <a  class="btn btn-success" href="edit.php?id=<?= $job_name['id'] ?>">&nbsp; &nbsp; Edit &nbsp;</a> 
                            <a class="btn btn-danger" href="delete.php?id=<?= $job_name['id'] ?>">Delete</a> </td>
                    </tr>
                <?php endforeach; ?>
            </table>  
        </div>

        <style>
            table td, table th {
                padding: 15px;
                text-align: center;

            }
            table th {
                background:#3390FF;
            }
            table {
                width: 100%;
                border: 3px solid #ccc;
                border-collapse: collapse;
            }

            .center {
                margin: auto;
                width: 100%;
                border: 3px solid red;
                padding: 10px;
                text-align: center;
            }
            ::placeholder { 
                color: white;
                opacity: 1; 
            }
        </style>
    </div>
</div>

【讨论】:

  • BTW 小事,但查询仍然可以全部变成一个字符串:select jobs.id, jobs.name as job_name, salary as job_salary, description, location_id, domain_id , locations.name as location_name , domains.name as domain_name from jobs LEFT JOIN locations ON jobs.location_id = locations.id LEFT JOIN domains on jobs.domain_id = domains.id where jobs.name like :job_name AND location_id = :location_id"。 where 子句不需要是单独的字符串。
猜你喜欢
  • 1970-01-01
  • 2016-10-31
  • 2011-10-27
  • 2015-08-26
  • 2017-07-06
  • 2019-02-07
  • 2015-10-26
  • 2016-07-22
  • 2021-08-30
相关资源
最近更新 更多