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

  • SEARCH
  • Home
  • 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 6810985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:16:19+00:00 2026-05-26T20:16:19+00:00

Im making a codeigniter webapp where users can add each other in a contactslist.

  • 0

Im making a codeigniter webapp where users can add each other in a contactslist.

The table looks like this:

`id` int(11) NOT NULL AUTO_INCREMENT,
 `user_1` int(11) NOT NULL,
 `user_2` int(11) NOT NULL,
 `accepted` tinyint(2) NOT NULL,
  PRIMARY KEY (`id`)

The userid for the user that makes the request to add the contact is always stored in user_1 column. The other users userid is stored in user_2. user_2 then has to accept the request and the ‘accepted’ column gets updated to 1.

I want to list all contacts that are accepted (WHERE accepted = 1) in a html table, and the contact requests (accepted = 0) in another.
My question is: How can i make a mysql query that selects all the rows and just get the userid from the contact? Its a problem since they can be in either user_1 or user_2 (Depending on if they requested or accepted).

Should i change the db table in some way to achieve this. Or could i make a query (active rcords preferably) that accomplish this?

Any help is appreciated

Thanks in advance

George

Update:

So the final query looks like this:

SELECT DISTINCT users.id, users.username, contacts.accepted 
FROM users
LEFT JOIN contacts ON users.id = contacts.user_1
WHERE contacts.user_2 = ' . $this->session->userdata('user_id') . '
UNION DISTINCT
SELECT DISTINCT users.id, users.username, contacts.accepted 
FROM users 
LEFT JOIN contacts ON users.id = contacts.user_2
WHERE user_1 = ' . $this->session->userdata('user_id')

And works exactly as i described 🙂

  • 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-26T20:16:19+00:00Added an answer on May 26, 2026 at 8:16 pm

    Use a UNION query. See the documentation.

    SELECT DISTINCT user_1 userid FROM user WHERE accepted = 1
    UNION DISTINCT
    SELECT DISTINCT user_2 userid FROM user WHERE accepted = 1
    

    About the join, you’d use something like below for each part of the UNION

    SELECT DISTINCT users.userid, users.username, contacts.accepted 
    FROM users
    LEFT JOIN contacts ON users.userid = contacts.user_1
    WHERE contacts.user_2 = ?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making use of Codeigniter and I currently have this URL format setup:
I have a problem making a url in Codeigniter. The url is mywebsite.com/users/mike where
I'm making the switch from CodeIgniter to Symfony 2. Can someone please give me
I have a weird problem. I am making kinda my own framework like codeigniter
I'm making a website, using codeigniter, that will enable users to download files a
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
When making software, specially games, those resources like graphics and sounds are something freaky
I am having trouble simply making multiple pages with CodeIgniter. For example, I am
I am developing an app using Codeigniter. In this app I got a form
I have create a web application using CodeIgniter making at first a login interface.

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.