Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我正在绞尽脑汁使用MySQL数据创建PHP数组_Php_Mysql - Fatal编程技术网

我正在绞尽脑汁使用MySQL数据创建PHP数组

我正在绞尽脑汁使用MySQL数据创建PHP数组,php,mysql,Php,Mysql,这对很多人来说似乎很基本,但我正努力想办法解决这个问题 我有一个MySQL表,看起来像这样: | email | name | mod1 | mod2 | mod3 | mod4 | mod1r | mod2r | mod3r | mod4r | | jb@test.com | Joe Bloggs | 401 | 402 | 405 | 410 | yes | no | no | no | | ab@test.com | Alex Ba

这对很多人来说似乎很基本,但我正努力想办法解决这个问题

我有一个MySQL表,看起来像这样:

| email       | name        | mod1 | mod2 | mod3 | mod4 | mod1r | mod2r | mod3r | mod4r |
| jb@test.com | Joe Bloggs  | 401  | 402  | 405  | 410  | yes   | no    | no    | no    |
| ab@test.com | Alex Baines | 401  | 404  | 407  | 409  | no    | yes   | yes   | no    |
| rs@test.com | Rick Summer | 403  | 406  | 408  | 409  | no    | no    | no    | no    |
最后四列与前四列相关(因此mod1r对应mod1,mod2r对应mod2等)

我需要做两件事:

  • 1:确定在最后四列中的一列或多列中有“是”的记录

  • 2:识别前面四列中与“是”数据相对应的数据

  • 3:在表格中显示结果

所以在上面的例子中,Rick Summer应该被完全抛弃,因为他没有“是”的数据。Joe Bloggs应显示一次,值为“401”,因为他在“mod1r”列中有一个“是”,而“mod1”中的相应值为“401”。Alex Baines应显示两次,一次显示值为“404”,另一次显示值为“407”

我希望最终的输出像这样(在HTML表中):

有人能帮忙吗?我假设我需要使用多维数组来提取数据,但我很难想出如何做到这一点


谢谢

大致实施思路:

For all query results as row:
   For all modules as currentModule:
      if (row[currentModule + 'r']):
         outputLine(row['name'], row[currentModule]);

尽管您确实应该考虑从表中删除这些模块列,并创建具有“代码> USER ID ”和“代码>模块> ID <代码>的表。他们可以这样做,直接从数据库中获取结果:

SELECT    u.name, m.module_id module
FROM      users u
JOIN      users2modules m ON u.id = m.user_id
ORDER BY  u.name ASC
试试这个

   select name ,mod1 as module from Table1 where mod1r = 'yes'
  union all
   select name ,mod2 from Table1 where mod2r = 'yes'
  union all
   select name ,mod3 from Table1 where mod3r = 'yes'
   union all
   select name ,mod4  from Table1 where mod4r = 'yes'
输出:

  NAME         MODULE
 Joe Bloggs     401
 Alex Baines    404
 Alex Baines    407


但是要编写整个代码,您应该自己编写,或者看看这里如何使用mysqli构建html表,正如其他人提到的那样,我肯定会规范数据库结构,因此您有三个表而不是一个表

用户:

user_id  email        name
1        jb@test.com  Joe Bloggs
2        ab@test.com  Alex Baines
3        rs@test.com  Rick Summer
模块:

module_id  modules
1          401
2          402
3          403
4          404
5          405
6          406
7          407
8          408
9          409
10         410
用户/模块:

user_id  module_id
1        1
2        4
2        7
这使得获得所需阵列变得更加容易,而且如果需要添加更多模块,也可以避免将来的麻烦。然后,以下查询将以您想要的方式返回数据

SELECT u.name, m.module 
FROM users u 
INNER JOIN users_modules um ON u.user_id = um.user_id 
INNER JOIN modules m ON um.module_id = m.module_id

小注释:
modXr
似乎是一个布尔值。在这种情况下,最好将其编码为
0/1
,这样会更快更容易处理。如果您的数据库,注意到并将进行更改,直到-我会这样做,但在提取和显示相关值方面仍然需要帮助。是的,我会选择不同的db结构,因为这样的结构会使事情变得非常复杂。我会说使用一个单独的表来表示模块,使用一个单独的表来表示模块订阅。然后
加入
你的方式通过他们。谢谢,直到,所以一旦我对数据库进行了更改,我该怎么办?
SELECT u.name, m.module 
FROM users u 
INNER JOIN users_modules um ON u.user_id = um.user_id 
INNER JOIN modules m ON um.module_id = m.module_id