表单必填脚本_使用PHP脚本创建简单搜索表单的说明

表单必填脚本

创建数据库 ( Creating the Database )

Having a search feature on your site is handy for helping users find exactly what they are looking for. Search engines can range from simple to complicated.

在您的站点上具有搜索功能可帮助用户准确找到所需的内容。 搜索引擎的范围可以从简单到复杂。

This search engine tutorial assumes that all the data you want to be searchable is stored in your MySQL database. It does not have any fancy algorithms—just a simple like query, but it works for basic searching and gives you a jumping off point to make a more complex search system.

该搜索引擎教程假定您要搜索的所有数据都存储在MySQL数据库中 。 它没有任何花哨的算法, 就像查询一个简单的,但它的工作原理基本的搜索,给你一个起跳点进行更复杂的搜索系统。

This tutorial requires a database. The code below creates a testing database to use as you work through the tutorial.

本教程需要一个数据库。 下面的代码创建一个测试数据库 ,供您在学习本教程时使用。

HTML搜索表单 ( The HTML Search Form )

This HTML code creates the form your users will use to search. It provides a space to enter what they are looking for, and a drop-down menu where they can choose a field they are searching (first name, last name, or profile.) The form sends the data back to itself using the PHP_SELF () function. This code does not go inside the tags, but rather above or below them.

此HTML代码创建用户将用于搜索的表单。 它提供了一个输入所需内容的空间,以及一个下拉菜单,用户可以在其中选择要搜索的字段(名字,姓氏或配置文件。)该表单使用PHP_SELF( )功能。 此代码不在标签内,而是在标签上方或下方。

PHP搜索代码 ( The PHP Search Code )

This code can be placed either above or below the HTML form in the file depending on your preference. A breakdown of the code with explanations appears in the following sections.

可以根据您的喜好将此代码放在文件中HTML表单的上方或下方。 以下各节中将显示代码的细分及其说明。

分解PHP代码-第1部分 ( Breaking the PHP Code Down - Part 1 )

In the original HTML form, we had a hidden field that sets this variable to "yes" when submitted. This line checks for that. If the form has been submitted, then it runs the PHP code; if not, it just ignores the rest of the coding.

在原始HTML表单中,我们有一个隐藏字段,可在提交时将此变量设置为 yes 。 这行检查。 如果已提交表单,则它将运行PHP代码;否则,它将运行PHP代码。 如果没有,它将忽略其余的编码。

The next thing to check before running the query is that the user actually entered a search string. If they haven't, we prompt them to do so and don't process any more of the code. If we didn't have this code, and the user entered a blank result, it would return the entire database's contents.

运行查询之前要检查的下一件事情是用户实际上输入了搜索字符串。 如果还没有,我们会提示他们这样做,并且不再处理任何代码。 如果我们没有此代码,并且用户输入了空白结果,则它将返回整个数据库的内容。

After this check, we connect to the database, but before we can search, we need to filter.

进行此检查后,我们将连接到数据库,但是在进行搜索之前,需要进行过滤。

This changes all the characters of the search string to upper case.

这会将搜索字符串的所有字符更改为大写。

This takes out any code the user may have tried to enter in the search box.

这将删除用户可能试图在搜索框中输入的所有代码。

And this takes out all the white space—for example, if the user accidently put a few spaces at the end of their query.

这会占用所有空白,例如,如果用户不小心在查询末尾放置了一些空格。

分解PHP代码-第2部分 ( Breaking the PHP Code Down - Part 2 )

This code does the actual searching. We are choosing all the data from our table WHERE the field they choose is LIKE their search string. We use upper () here to search the uppercase version of the fields. Earlier we converted our search term to uppercase as well. These two things together basically ignore case. Without this, a search for "pizza" would not return a profile that had the word "Pizza" with a capital P. We also use the '%' percentage on either side of the $find variable to indicate that we are not looking solely for that term but rather that term possibly contained in a body of text.

此代码进行实际搜索。 我们从表中选择所有数据,而他们选择的字段就像他们的搜索字符串一样。 我们用 upper()在此处搜索字段的大写版本。 之前我们也将搜索词也转换为大写。 这两件事基本上一起忽略了大小写。 否则,搜索“ pizza”将不会返回带有单词“ Pizza”且大写字母P的配置文件。我们还使用$ find变量两侧的'%'百分比来表示我们并不是在寻找唯一的对象该术语,但该术语可能包含在正文中。

This line and the lines below it start a loop that will cycle through and return all the data. We then choose what information to ECHO back to the user and in what format.

这条线及其下的线开始一个循环,循环循环并返回所有数据。 然后,我们选择要以何种格式回传给用户的信息。

This code counts the number of rows of results. If the number is 0, no results were found. If this is the case, we let the user know that.

此代码计算结果的行数。 如果数字为0,则找不到结果。 如果是这种情况,我们会让用户知道。

Finally, in case the user forgot, we remind them of what they searched for.

最后,万一用户忘记了,我们会提醒他们搜索的内容。

If you anticipate a large number of query results, you may wish to use pagination to display your results.

如果您预计会有大量查询结果,则可能希望使用分页显示结果

翻译自: https://www.thoughtco.com/simple-site-search-2694116

表单必填脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值