由数据库填充的自动完成表单?
我目前正在开发一个项目,其中我需要一个自动完成表单从数据库文件中调用其信息。我看过很多关于 jquery 自动完成表单的教程,但我不知道如何调用 db 文件来填充列表。
我正在使用 PHP 工作。目前,该代码表示一个简单的下拉框,它调用数据库文件进行填充。
<?php
global $wpdb;
$depts = $wpdb->get_results( "SELECT * FROM departments ORDER BY department_name ASC" );
echo '<select>';
foreach($depts as $row) {
echo '<option name="select_dept" value="'.$row->department_id.'">'.$row->department_name.'</option>';
}
echo '</select>';
?>
任何帮助都会很棒!
I am currently working on a project in which I need to have an autocomplete form call its information from a db file. I have seen many tutorials on jquery autocomplete forms, but I do not know how to call a db file to populate the list.
I am working in PHP. Currently the code represents a simple drop down box that is calling on the db file for population.
<?php
global $wpdb;
$depts = $wpdb->get_results( "SELECT * FROM departments ORDER BY department_name ASC" );
echo '<select>';
foreach($depts as $row) {
echo '<option name="select_dept" value="'.$row->department_id.'">'.$row->department_name.'</option>';
}
echo '</select>';
?>
Any help would be awesome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近使用这个库进行自动完成 - http://www.devbridge.com/projects/autocomplete /jquery/
所以这里是基于您的简短脚本:
I recently have used this library for autocompletion - http://www.devbridge.com/projects/autocomplete/jquery/
So here is brief script based on yours:
请遵循这个写得很好的教程
http://www.nodstrum.com/2007/09 /19/自动完成器/
Please follow this very well written tutorial
http://www.nodstrum.com/2007/09/19/autocompleter/
JQuery UI 包含自动完成功能,但您仍然需要编写 PHP 脚本来返回要添加到控件的信息,如下所示它是通过 AJAX 完成的。如果您知道在 PHP 中如何连接到数据库、查询数据库并返回结果列表 - 那么您将不会遇到任何问题。 JQuery 使 AJAX 变得极其简单。
根据您的字段/数据集的复杂程度 - 并假设它不是数以百万计的未索引记录,我会满足于自动完成:
因此,如果您正在搜索,比如说,食物......查询“CA”将拔出胡萝卜、卷心菜和花椰菜。如果您在 LIKE 的开头添加了 %,则可以获得包含您的查询的结果,而不是仅以它开头。
用户点击的页面将包含 JQuery 部分,该部分既发送请求,又根据结果创建自动完成效果,以及一个非常简单、单独的 PHP 脚本,AJAX 请求点击该脚本将返回潜在的“匹配项”。
查看 JQuery UI 上的自动完成演示
JQuery UI includes an autocomplete, although you still need to write a PHP script to return the information to be added to the control as its done via AJAX. If you know in PHP how to connect to a database, query it, and return a list of the results - then you will have no problems with this. JQuery makes AJAX extremely simple.
Depending on how complicated your field/data set is - and assuming its not millions upon millions of unindexed records, I would be content to autocomplete from a:
So if you were searching for, say, food... the query "CA" would pull out CArrot and CAbbage and CAuliflower. If you added a % to the beginning of the LIKE, you could get results that contained your query, as opposed to just beginning with it.
The page your user hits would contain the JQuery part which both sends the request and creates the autocomplete effect from the results, and a very simple, separate PHP script which the AJAX request hits would return the potential 'matches'.
Take a look at the Autocomplete demos on JQuery UI