开发者

Array within an array - Getting subcategories from a category

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开发者_如何学Python "]; }
   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_staff["u_id"];
    $name = $get_staff["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_name["u_id"];
       $user_name = $get_name["name"];
      }
   }
   echo $user_name."<br />";
      }
     }
    }

I have tried almost everything; it displays the categories but displays ALL the users within that department. It should ONLY display users that have the same c_id of the $_POST['c_id'].

Please help!! I have been on this for hours. I might just be missing something small. Please could you check what im doing wrong


I would also suggest you to use inner joins and move d_array and u_array to "middle table". Anyway, if it is not possible to do now here is one SQL script which could help you out with this. And before you try it, add for each d_array, u_array and c_array ending ',' char. So instead of "1,2" it should be "1,2,"

select  d.*, u.*
from    depart_table d,
        categories_table c,
        users_table u
where   c.d_array like '%' + cast(d.d_id as varchar) + ',%'
        and c.c_id = @c_id
        and u.d_array like '%' + cast(d.d_id as varchar) + ',%'
        and u.cid = @c_id

which should produce something like this

d_id        d_name    u_array   u_id  cid   name   d_array 
----------- --------- --------- ----- ----- ------ --------
1           James     1,2,5,    1     3     Jam    1,3,4,
1           James     1,2,5,    5     3     Pol    1,5,3,
5           Bags      5,        5     3     Pol    1,5,3,

I'm sure you will figure out how to group data further in PHP

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜