Buki
Buki

Reputation: 127

Loop through array categories to get sub-categories

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.

Upvotes: 0

Views: 540

Answers (1)

B_.
B_.

Reputation: 2254

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!

Upvotes: 3

Related Questions