PHP - 如何向现有搜索结果添加过滤器

时间:2015-08-28 00:40:10

标签: php sql

背景: 我目前有一个设置,人们被放在页面,他们有他们没有的BB卡。我希望他们能够过滤掉现有列表以缩小范围......

如何将PHP过滤器添加到该EXISTING搜索结果中,以便将其分解?这是我现有的相关代码:

<div id="col2"><br><br><center>Your Baseball Cards<center><br><br><br>
<?php
$servername = "********";
$username = "******";
$password = "*******";
$dbname = "*******";
$username1=$_SESSION['activeusername'];
    $userid = $_SESSION['activeid'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM useritems JOIN (users, iteminfo) on (users.id=useritems.UserID AND iteminfo.ID=useritems.ItemID) AND userid!='$userid' LIMIT 100";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "<a href='iteminfo.php?id=". $row['id']."'><div id='frame'>
<div id='splash-image' style='background: url(".$row['imagename']."); background-size:cover;'></div>
<div id='text'>
<table >
<tr>
    <td>Player:</td>
    <td>".$row["player"]."</td>
</tr>
<tr>
    <td>Brand:</td>
    <td>".$row["brand"]."</td>
</tr>
<tr>
    <td>Year:</td>
    <td>".$row["year"]."</td>
</tr>
<tr>
    <td>Price:</td>
    <td>".$row['price']."</td>
</tr>
</table>
</div>
</div>";
    }
} else {
    echo "You Have No Baseball Cards!!!";
}

mysqli_close($conn);
?>
</div>

<div id="col3">column 3</div>

<div id="footer">footer</div>

</body>
</head>
<html> 

1 个答案:

答案 0 :(得分:0)

我不完全确定您想要通过过滤,但我认为您的意思是用户可以根据播放器,品牌,年份等进行过滤。如果是这样,那么您可以添加SQL语句的WHERE子句。

以下是使用播放器过滤的示例(假设您有要提交的元素和表单。

$where_clause = "AND player='" . filter_var($_POST['player'], FILTER_SANITIZE_STRING) . "'";

$sql = "SELECT * FROM useritems JOIN 
(users, iteminfo) ON (users.id=useritems.UserID AND iteminfo.ID=useritems.ItemID)
WHERE userid != '$userid' $where_clause LIMIT 100";

如果需要,可以在具有多个输入的循环中执行此操作,并在WHERE子句中附加用户输入的任何其他过滤器。