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?
Sorry for my bad english
My 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);
what you need to do is create another table that will store friend ids, you should not do this in the users table as a comma seperated value. Example,
Table: Users
UserID, FirstName, LastName
1, John, Smith
2, Suzan, Carter
3, James, Blake
Table: Friends
ID, UserID, FriendID
1, 1, 2
2, 1, 3
3, 3, 2
Above Friends table contains 3 rows. It shows that User ID 1 (John) has two rows but the friendID for both rows is different. Now you need to join the two tables together. Example
SELECT * FROM Users u
INNER JOIN Friends f On f.UserID = u.UserID WHERE u.UserID =1
The above sql will return two rows, which is the two friends of John.