Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6038395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:12:49+00:00 2026-05-23T06:12:49+00:00

My idea: I have a database of users, each user has an autoincremented id

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:12:50+00:00Added an answer on May 23, 2026 at 6:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website that allows a functionality for any user but each user
Our app would be creating dynamics forms for each users. We are considering two
I've been struggling with this all day long! In the GemFile, I have: group
I have rails application that connects to multiple databases. I wrote custom rake task
I'm working on an import feature from one database to another and thought I'd
I am rebuilding a website that features a lot of content contributed by the
I'm working on a solo project, using the aforementioned technologies. The aim is to
Here's the story so far: I'm doing a C# winforms application to facilitate specifying
I'm after some guidance on a new project I'm working on that requires low
I'm trying to make my own framework for my own projects and I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.