Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当链接来自$\u GET方法时,如何实现搜索功能_Php_Search - Fatal编程技术网

Php 当链接来自$\u GET方法时,如何实现搜索功能

Php 当链接来自$\u GET方法时,如何实现搜索功能,php,search,Php,Search,你们可能不明白我的问题,我会进一步解释 我有一个网站,显示的产品信息,其名称,id,价格,数量等。 现在,我已经使用$\u GET变量显示了它 例如,如果我想单击产品页面,那么链接将是 http://localhost/InventorySystem/Employee/Tools/?Item=ProductList 我不必再做一个php页面,因为我只需要一个get方法,它将显示产品信息 现在我的问题是,如何用它实现搜索功能?有没有一种方法可以让我创建一个像 ?Item=ProductList&

你们可能不明白我的问题,我会进一步解释

我有一个网站,显示的产品信息,其名称,id,价格,数量等。 现在,我已经使用$\u GET变量显示了它

例如,如果我想单击产品页面,那么链接将是

http://localhost/InventorySystem/Employee/Tools/?Item=ProductList
我不必再做一个php页面,因为我只需要一个get方法,它将显示产品信息

现在我的问题是,如何用它实现搜索功能?有没有一种方法可以让我创建一个像

?Item=ProductList&Search=SearchTerm

我很难知道怎么做。请提供任何建议。

我假设您只是在使用SQL语句查询此页面的产品

我会检查
isset($\u GET['Search'])
以了解
Search
参数是否存在。如果是这样的话,我会在SQL语句中附加一些条件(
where…=…
)。但要记住,要对查询进行清理,以避免出现错误

示例代码:

$query = "select * from products";
if (isset($_GET['Search']))
  $query .= " where somefield=?";

$mysqli->prepare($query....);

这可能是这方面的一些变化:

<!DOCTYPE html>
<html>
  <body>
<?php
if (isset($_GET['search'])) {
  // Perform the search, probably against a back-end database.
  // For purposes of this pseudocode, I'll assume you store the results in a
  // variable named $search_result that is an array of associative arrays
  // with fields 'id' and 'name'.
?>
    <p style="font-weight: bold;">Search Results</p>
    <table>
    <?php foreach ($search_result as $result): ?>
      <tr>
        <td><a href=$base_url."/Item="<?= urlencode($result['id']) ?>">
          <?= htmlentities($result['name']) ?></a></td>
      </tr>
    </php endforeach; ?>
    </table>
<?php } ?>

  <!-- stuff you display whether page is searched or not goes here -->
  </body>
</html>


基本上,在查询字符串中添加另一个变量,就像您刚才在示例中所示:
?Item=ProductList&Search=SearchTerm
。只需执行
$\u GET['Search']
即可获得该值,将其传递给mysql,并使用
WHERE
子句与
LIKE
LIKE组合实现一个过滤器:

$searchkey = $_GET['Search'];
$result = mysql_query("SELECT * FROM table WHERE column LIKE '%".$searchkey."%'");
这将返回包含您在
$searchkey
中传递的术语的结果,例如,如果
$searchkey=“ball”
排球、篮球、芭蕾、篮球排球都是有效的比赛

编辑:

html表单应如下所示,以便能够在查询字符串中传递所需的参数:

<form method="GET">
<input type="hidden" name="Item" value="ProductList" />
Search: <input type="text" name="Search" />
<input type="submit" value="submit" />
</form>

搜索:

页面显示的是一种产品还是多种产品?它显示数据库中的所有产品。基本上,我正在创建一个搜索功能,以便缩小正在搜索的项目范围。基本上,在查询字符串中添加另一个变量,如您刚才在示例中所示:
?Item=ProductList&search=SearchTerm
。只需执行
$\u GET['Search']
即可获取值,将其传递给mysql并使用
WHERE
子句实现一个过滤器。我已经知道需要查询数据库的部分,我需要知道的是,当您的页面依赖于$\u Get方法时,如何实现搜索功能。您的意思是什么依赖于$\u Get方法?我的意思是,我没有独立的页面或类似于productlist.php的页面。我的productlist是从Get method index.php?Item=productlist显示的。您不需要单独的页面。当前如何在index.php上获取产品列表的数据?我使用了if($_GET[Item])==ProductList){ProductList();}ProductList()执行数据库查询并将其直接显示在页面中。Item表示我正在显示的页面,即ProductList。我不知道这是否解决了问题,因为我的问题在URL端。我不明白你当时在问什么。听起来您只需要解析$\u GET参数,并为每个参数采取所需的适当操作。如果设置为($\u GET['Search']),则编写一个函数来生成搜索结果。如果没有,则用户不在搜索,如果没有搜索,您应该生成页面所做的任何操作。我知道我的问题是我将其发送到的URL,因为当我创建一个“我将在操作中放置什么=”“,它将显示为完全相同的位置?Item=ProductList&search=SearchTerm。因为当我放置index.php时,它返回的URL不仅仅是?Item=ProductList&Search=SearchTerm@anoldgangstah如果您正在引用当前页面,则无需执行操作。如果您需要
Item=ProductList
,请在表单中包含
。好的,当我在操作中输入空白时,url仍然没有显示我要在url中显示的内容。呃,我真的很抱歉,老兄,如果我给你的信息不太详细,我的做事方式可能是错误的,我想我只是要重写所有这些。你实际上是对的!你说的东西非常好用
?Item=ProductList&Search=Keyboard
我现在可以执行搜索功能了!谢天谢地,你理解了我的问题。@anoldgangstah没问题,程序员应该在需要的时候帮助其他程序员。