gpt4 book ai didi

php - 在php中添加搜索过滤器

转载 作者:行者123 更新时间:2023-11-29 03:01:01 25 4
gpt4 key购买 nike

这是一个搜索引擎和分页,带有一个过滤器,根据“搜索”中的一个词,我想再添加两个搜索过滤器:角色和两个日期之间的搜索。

<?php

$button = $_GET ['submit'];
$search = $_GET ['search'];

echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","");
mysql_select_db("page");

$search_exploded = explode (" ", $search);

foreach($search_exploded as $search_each)
{$x=0;
$construct="";
$x++;
if($x==1)
$construct .="name LIKE '%$search_each%'";

}

$constructs ="SELECT * FROM info WHERE $construct";
$run = mysql_query($constructs);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "Sorry";
else
{

echo "$foundnum results found !<p>";
$per_page = 1;
$start = $_GET['start'];
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$getquery = mysql_query("SELECT * FROM info WHERE $construct LIMIT $start, $per_page");

while($runrows = mysql_fetch_assoc($getquery))
{
$name = $runrows ['name'];
}

//Pagination Starts
echo "<center>";

$prev = $start - $per_page;
$next = $start + $per_page;

$adjacents = 3;
$last = $max_pages - 1;

if($max_pages > 1)
{
//previous button
if (!($start<=0))
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";

//pages
if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}

}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='index.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}

}
//close to end; only hide early pages
else
{
echo " <a href='index.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
}

//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
?>

最佳答案

你应该像这样重写你的循环

//by name
$construct="true";
foreach($search_exploded as $search_each) {
$construct .= " AND name LIKE '%$search_each%'";
}
//by role
if($role) {
$construct .= " AND role = '$role'";
}
//by date
if($start) {
$construct .= " AND the_date >= '$start'";
}
if($end) {
$construct .= " AND the_date <= '$end'";
}

否则,您会不断地重置 $x(无论如何都没有必要)和 $construct 的值。

当然,您必须GET 日期/角色并正确格式化它们。

关于php - 在php中添加搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23660502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com