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 3991978
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:42:43+00:00 2026-05-20T06:42:43+00:00

I have three tables and they look like this: users_table categories_table depart_table u_id cid

  • 0

I have three tables and they look like this:

users_table                      categories_table         depart_table

u_id   cid  name  d_array      |  c_id   d_array       |  d_id  d_name  u_array
1      3   Jam     1,3,4       |  1      2,4           |  1    James    1,2,5
2      1   Ham     1,4,6       |  2      1,2,3         |  2    Mark     3,4
3      2   Ink     2,6,7       |  3      1,2,5         |  3    Love     1,5
4      4   Kal     4,2         |  4      4,5,6,7       |  4    Phone    1,2,4
5      3   Pol     1,5,3       |  5      7             |  5    Bags     5

Basically, the cid (categories ID) is taken from a post. So $c_id = $_POST[“cid”];
So, we know what the cid is.

Now we want to display all departments of that category. And then… Display list of users within that department.
So, if $c_id = 3 The results should be:

   James
       Jam
       Pol

   Mark

   Bags
      Pol

Explanation:
Because c_id == 3, We loop through d_array which is department array – TO get the user array. Now, Every user that has a cid OF 3 should only be included in the list.
As you can see James (First value in the array of cid 3) has Jam AND Pol BUT NOT HAM; because HAM doesnt have a cid of 3.

This was my solution, but it doesn’t work.

     `$sql_myDetails = mysql_query("SELECT * FROM categories_table WHERE c_id = '$c_id'")`

or die ("<p>died 20: $sql_myDetails<br>" . mysql_error());
while($row = mysql_fetch_array($sql_myDetails)){
$c_id= $row["cid"];
$d_array = $row["d_array "];
}

     if ($d_array != ""){
    $sqlUsers = mysql_query("SELECT * FROM users_table WHERE c_id = '$c_id' AND d_array IN ($d_array)");
while($get_staff = mysql_fetch_array($sqlUsers )){
    $u_id = $get["u_id"];
    $name = $get["name"];
    }

$sqlDept = mysql_query("SELECT * FROM depart_table WHERE d_id IN ($d_array)")
or die ("<p>died 52: $sqlDept<br>" . mysql_error());
while($get = mysql_fetch_array($sqlDept)){
    $d_id = $get["d_id"];
    $d_name = $get["d_name"];
    $u_array = $get["u_array"];
    echo $d_name."<br />";
if ($u_array != ""){
$exloded_u_array = explode(",", $u_array); 
foreach($exloded_u_array as $key2 => $value2) { 
$sql_user = mysql_query("SELECT * FROM user_table WHERE u_id = '$value2'");
while($get_name = mysql_fetch_array($sql_user)){
    $user_id = $get["u_id"];
    $user_name = $get["name"];
    }
}
echo $user_name."<br />";
 }
}
}

I have tried, it displays the categories but displays ALL the users within that department. It should display users that have a c_id of the category only.

Please could you check what im doing wrong.

  • 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-20T06:42:44+00:00Added an answer on May 20, 2026 at 6:42 am

    I don’t use php much and I haven’t done much if any DB stuff with it. Looking at your code, the steps seem a bit confused, for instance unless I’m misunderstanding, you seem to assign some variables like $u_id multiple times for each matching row and then never use them. In any case, using your current database structure, I suggest you rewrite your code following this pseudocode:

    1. Query categories table for the desired category
    2. Query users table for users with that category id
    3. Query departments table for the departments in the category’s d_array

    From here you don’t access the database anymore, everything you need is in memory

    For each department in your result from 3
      print the d_name
      for each id in department's u_array
        if that u_id exists in the result from 2, print that user's name
    

    That said, I suggest you rethink your database structure. Keeping lists or ‘arrays’ in your columns is generally bad practice. Usually you either keep separate rows for each value, i.e

    users_table
    
    uid   cid  name    dept
    1     3    Jam     1
    1     3    Jam     3
    1     3    Jam     4
    

    Here the primary key for a row would be the whole row. Or you can keep a relations table, i.e

    users_table       |  users_depts_relation
                      |
    uid   cid   name  |  uid   dept
    1     3     Jam   |  1     1
                      |  1     3
                      |  1     4
    

    You also don’t need to keep two way relationships, like keeping a d_array for each user and a u_array for each department, since one is sufficient to figure out the other, especially if you store them in a kosher way as I just described, through a simple join query. Indeed if you store things better, you’ll be able to figure out what you need through good database query’s and you’ll save on the cycles and memory of using application logic to match together your data.

    Good luck!

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

Sidebar

Related Questions

I've got three related SQL tables, simplified they look like this: ShopTable [ShopID] ShelfTable
Feel like this should be something easy that I'm missing. I have a table
I have date range data in SQL DB table that has these three (only
If I have a set of small string values, and I want to fetch
Sorry for the vague topic but I'm having a hard time explaining this. What
Using MySQL Workbench I reverse engineered phpbb's database to see what the schema would
I'm creating a video ranking website. Each video goes up against another video in
I am wondering if there are any android device databases around. One of the
I work for a publishing house and we're discussing different ways to sell our
I'm trying to find a way to debug core files sent to me from

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.