输入字段单词到php数组
注册表单中带有自动完成脚本的好友文本框(支持从用户表的“全名”列中完成多个单词)。现在问题 1:如何从文本输入字段获取名称数组并使用 php 查找这些全名的 id?问题 2:如果新用户在注册过程中未知 ID,注册表如何将这些 ID 放入第二个(朋友)表? 例如,如果访问者编写“Tu”脚本,从用户表“Tural Teyyuboglu”中查找,完成它,添加逗号,用户可以添加任意数量的用户名。每次脚本工作时。我想获取这个用逗号分隔的用户名到 php 数组,一一搜索这些用户名的 id 并添加到朋友表中。但第二个问题发生了?但是,如果注册过程中新用户 ID 未知,注册表如何将这些 ID 放入第二个(朋友)表中?
抱歉我的英语不好。我的想法:我有一个用户数据库,每个用户都有一个自动递增的 ID 号字段,以及各种其他字段及其详细信息。我想在用户记录中存储一个包含一组朋友的字段。我想要最有效的方式来存储并能够搜索每个用户的“朋友 ID 数组”字段并列出每个用户搜索的朋友 ID。假设在某个时候可能有一百万个用户,其中每个用户的数组中有一百万个“朋友 ID”,我认为用逗号分隔它们不会有效,有什么解决方案吗?它可以存储在 blob 中并按位搜索吗? (例如,每人一点,因此 1k blob 最多可以存储 1024 个朋友组合)我认为另一个表是最好的解决方案。这是一个多对多的关系,我为用户朋友构建了一个单独的表: usrfrnd_tbl ,其中包含 UserFriendID (唯一键)、UserID、FriendID 列 在我将 Jquery 自动完成应用于注册表单的朋友字段中:当访客输入姓名时现有用户到此输入字段,首先,脚本搜索名称是否存在,完成它(如果存在),添加逗号。用户可以在此字段中输入第二个、第三个...现有用户名,每次脚本都会自动完成。现在我想创建 php 文件来搜索这些用户名的 id,创建 id 数组,将其添加到数据库表中的表“usrfrnd_tbl”新用户“FriendID”字段中。问题:1.怎么做?现在,如何获取书面姓名的 id 数组,并在提交注册表单后将其放入 usr_table 的“friends”字段中? 2.我只需要ID的书面姓名和全名。我不需要值。如何从 jquery 代码中删除它?
HTML
<form action="index.php" method="post">
<input class="std" type="text" name="friends" id="friends"/>
<input type="submit" name="submit">
</form>
Jquery
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#friends" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "search.php", {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
search.php
$db = new mysqli('localhost', 'user' ,'pass', 'db') or die(mysqli_errno());
$q = strtolower($_GET["term"]);
if (!$q) return;
$query = $db->query("select id, fullname from usr_table where fullname like '$q%'") or die(mysqli_errno());
$results = array();
while ($row = mysqli_fetch_array($query)) {$results[] = array ( "id" => $row[0] , "label" => $row[1], "value" => $row[1] );}
echo json_encode($results);
Friends text box with autocomplete script (which supports multiple word completion from userstable "fullname" column) in registration form. Now Question 1: How to get names array from text input field and find id's of theese fulnames with php? Question 2: How registration form will put theese id's to second (friends) table, if new users id unknown during registration?
For example, if visitor writes "Tu" script finds from users table "Tural Teyyuboglu", completes it, adds comma, user can add as much he wants user names. Every time script works. I want to get this usernames seperated by comma to php array, search theese usernames one by one for id's and add to friends table. But second wuestion occurs? But how registration form will put theese id's to second (friends) table, if new users id unknown during registration?
Sorry for my bad english. My idea: I have a database of users, each user has an autoincremented id number field, and various other fields with their details on. I would like to store within the user record a field with an array of friends. I would like the most efficient way to store and be able to search each users 'friend id array' field and list the friends ids per user search. assuming at some point there could be a million users of which each has a million 'friend ids' in their array, I dont think having them comma seperated would be efficient, any solutions? could it be stored in a blob and searched bitwise? (e.g a bit per person, so a 1k blob can store up to a possible 1024 friend combinations) I think another table is the best solution. It is a many-to-many relationship, I've built a separate table for user friends: usrfrnd_tbl with columns UserFriendID (unique key), UserID, FriendID In i applied Jquery autocomplete to friends field of registration form: When visitor types name of existing user to this input field, first of all, script searches for name existence, completes it (if exists), adds comma. User can type second, third... existing usernames to this field, and everytime script will auto complete. Now i want to create php file that will search for id's of these usernames, create array of id's, add it to table "usrfrnd_tbl" new users "FriendID" field in db table. Question: 1. How to do it? Now, how to fetch array of id's of written names, and put to "friends" field of usr_table after submission of registration form? 2. I need only id of written name,and fullname. I don't need value.How can i delete it from jquery code?
HTML
<form action="index.php" method="post">
<input class="std" type="text" name="friends" id="friends"/>
<input type="submit" name="submit">
</form>
Jquery
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#friends" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "search.php", {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
search.php
$db = new mysqli('localhost', 'user' ,'pass', 'db') or die(mysqli_errno());
$q = strtolower($_GET["term"]);
if (!$q) return;
$query = $db->query("select id, fullname from usr_table where fullname like '$q%'") or die(mysqli_errno());
$results = array();
while ($row = mysqli_fetch_array($query)) {$results[] = array ( "id" => $row[0] , "label" => $row[1], "value" => $row[1] );}
echo json_encode($results);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我更喜欢这样实现:
每次访问者接受自动完成时,都会创建一个隐藏的输入字段,就像在这个非常简单的模板中一样:
在一些自动完成之后,动态表单将如下所示:
您的
$_REQUEST
数据将最终以这个更加优雅的关联数组进行结构化:有了这些结构化数据,就没有理由进行棘手的 PHP 字符串操作。另外,我认为这会导致代码的可重用性更高。
希望有帮助;]
Well, I would prefer to implement things this way:
Every time the visitors accept an auto-complete, a hidden input field would be created, just like in this very simple template:
The dynamic form would look like this after some auto-completes:
Your
$_REQUEST
data would end structured in this much much more elegant associative array:With that structured data in hands, there is no excuse for tricky PHP string manipulations. Also, I think this would lead to a much more reusable code.
Hope it helps ;]
如果我正确评估您的申请,您的自动完成功能会用名称填充输入字段。由于它仅填充名称,因此您的表单不知道与每个名称对应的 ID。您需要的是一个解决方案,其中名称将保留其 ID。
这就是我使用的: Facebook 风格的 JQuery 自动完成插件
它是相同的 FB。您可以添加/删除名称并维护其 ID。
当您提交[POST]表单时,您填充的输入字段将输出如下:
使用PHP的explode函数,您可以获得每个ID并简单地处理每个ID的信息。可能,像这样:
If I'm assessing your application correctly, your autocomplete populates an input field with names. Because it is only populated with names, your form does not know the IDs that correspond to each name. What you need is a solution where the name will maintain it's ID.
This is what I used: Facebook style JQuery autocomplete plugin
It is identical FB. You are able to add/remove names, and maintain their IDs.
When your [POST] form is submitted, your populated input field will output as follows:
Using PHPs explode function, you can get each ID and simply process your information per ID. Possibly, like so: