简体   繁体   English

使用PHP while循环显示来自多个表的MySQL数据

[英]Displaying MySQL data from several tables using PHP while loops

After several weeks of research I have not been able to solve this. 经过几周的研究,我无法解决此问题。 I've simplified my table structure and code to hopefully make this easier. 我简化了表结构和代码,希望可以简化此过程。

I have two MySQL tables; 我有两个MySQL表; 'blocks' and 'files'. “块”和“文件”。

The 'blocks' table has columns bwblock_id, bwblock_year and bwblock_name. “块”表具有列bwblock_id,bwblock_year和bwblock_name。

The 'files' table has columns file_id, file_name and bwblock_id. “文件”表具有列file_id,file_name和bwblock_id。

A user creates a block first. 用户首先创建一个块。 Then they can create a file, and assign it to a block (bwblock_id being the index column). 然后他们可以创建一个文件,并将其分配给一个块(bwblock_id是索引列)。

I then want to display something like this: 然后,我想显示如下内容:

BLOCK NAME #1 (being data from column bwblock_name) 块名称1(来自bwblock_name列中的数据)

[No files assigned, display block name only] [未分配文件,仅显示块名称]

BLOCK NAME #2 块名#2

1st file assigned to Block #2 分配给块#2的第一个文件

2nd file assigned to Block #2 分配给块#2的第二个文件

BLOCK NAME #3 块名#3

1st file assigned to Block #3 分配给第3块的第一个文件

2nd file assigned to Block #3 分配给第3块的第二个文件

etc... 等等...

The code I have below displays everything I need it to, but lists the files under the first block, not the third where it actually belongs. 我下面的代码显示了我需要的所有内容,但列出了第一个块下的文件,而不是它实际所属的第三个块。 How do I assign the files to display under the correct block? 如何分配文件以在正确的块下显示? It obviously has something to do with lining up block.bwblock_id and files.bwblock_id but I just can't figure out what that is. 显然,它与将block.bwblock_id和files.bwblock_id排列在一起有关,但我只是不知道那是什么。

I've tried a LEFT JOIN statement but the below MySQL is the closest I've come to getting what I want. 我已经尝试了LEFT JOIN语句,但是下面的MySQL是我得到所需的最接近的MySQL。

I select the blocks data first: 我首先选择块数据:

$query_RS_Blocks = "SELECT * FROM blocks WHERE bwblock_year = $variable ORDER BY bwblock_id ASC"
$row_RS_Blocks = mysql_fetch_assoc($RS_Blocks);

Then I select the applicable files: 然后,选择适用的文件:

"SELECT * FROM files WHERE bwblock_id IN (SELECT bwblock_id FROM blocks WHERE bwblock_year = $variable) ORDER BY file_name ASC"
$row_RS_Files = mysql_fetch_assoc($RS_Files);

My stripped-down PHP display code is: 我精简的PHP显示代码是:

do {
echo $row_RS_Block['block_name']; 
do {
 echo $row_RS_Files['file_name'];
   } 
while ($row_RS_Files= mysql_fetch_assoc($RS_Files));
          }
while ($row_RS_Blocks = mysql_fetch_assoc($RS_Blocks));

I've also tried changing the last line to: 我也尝试将最后一行更改为:

while (($row_RS_Blocks = mysql_fetch_assoc($RS_Blocks)) && ($row_RS_Blocks['bwblock_id'] == $row_RS_Files['bwblock_id']));

Then I get the first file inserted under the first block, and the second file under the second block. 然后,我将第一个文件插入第一个块下,将第二个文件插入第二个块下。 (Note there are only two files returned in that particular file query. I imagine it would assign each block with a single file infinitely.) (请注意,在该特定文件查询中仅返回了两个文件。我想它将为每个块无限分配一个文件。)

My mind is mush right now but if I have omitted any necessary info please advise and I will amend asap. 我的头脑现在有些糊涂,但是如果我省略了任何必要的信息,请告知我,我将尽快进行修改。

Can you try this select? 您可以尝试这种选择吗? And let me know if it would work. 让我知道是否可行。

SELECT b.bwblock_id, b.bwblock_year, b.bwblock_name, f.file_id, f.file_name 
FROM blocks AS b
LEFT JOIN files as f
ON b.bwblock_id=f.bwblock_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM