简体   繁体   English

php / mysql递归显示相关表中的关联记录

[英]php/mysql display associated records from related table recursively

Assuming I have 2 related tables set up similar to this: table_1: person_id, name; 假设我有2个与此相似的相关表:table_1:person_id,name; table_2: account_id, person_id, account_type, balance; table_2:account_id,person_id,account_type,余额;

I want to display results like this: 我想显示如下结果:

(A)person_id - name: (a)account_type - balance, (a)account_type - balance, (a)account_type - balance (A)person_id-名称:(a)account_type-余额,(a)account_type-余额,(a)account_type-余额

(B)person_id - name: (b)account_type - balance (B)person_id-名称:(b)account_type-余额

(C)person_id - name: (c)account_type - balance, (c)account_type - balance (C)person_id-名称:(c)account_type-余额,(c)account_type-余额


Below is what I have so far (I wasn't sure if I should do it in 2 queries or just 1 joined query) also $where1 and $search are user input search fields that are currently already working on the page. 以下是到目前为止的内容(我不确定是应该在2个查询中还是仅在1个联合查询中进行),其中$ where1和$ search是当前已在页面上运行的用户输入搜索字段。 And $y was my dumb attempt at passing a variable but I don't think this is a correct solution to what I'm trying to do: $ y是我愚蠢的尝试传递变量,但是我认为这不是我要执行的操作的正确解决方案:

$q = "SELECT person_id, 
FROM table_1 WHERE $where1 LIKE '$search%'"; 
$r = @mysqli_query ($dbc, $q);

$q2 = "SELECT person_id, balance, account_type 
FROM accounts WHERE person_id LIKE '$y%'"; 
$r2 = @mysqli_query ($dbc, $q2);

while($row = mysqli_fetch_array($r)) {
    echo $row['person_id']. " - " .$row['name']. ":";
    $y = $row['person_id'];
    echo "</br>";
        while($row = mysqli_fetch_array($r2)) {
        echo $row['account_type']. " - " .$row['balance'] ."</br>";
        }       
    echo "</br>";
    }

Currently this displays: 当前显示:

(A)person_id - name: (a)account_type - balance (a)account_type - balance (a)account_type - balance (b)account_type - balance (c)account_type - balance (c)account_type - balance (A)person_id-名称:(a)帐户类型-余额(a)帐户类型-余额(a)帐户类型-余额(b)帐户类型-余额(c)帐户类型-余额(c)帐户类型-余额

(B)person_id - name: (B)person_id-名称:

(C)person_id - name: (C)person_id-名称:


I assume that the while within the while is the way this would work, but I'm not sure exactly how to go about it. 我以为while内的一会儿是可行的方式,但是我不确定该怎么做。 Also, there is obviously code above this stuff but that stuff is all working and is mostly just related to opening the database connection and the search form. 同样,显然在这些内容之上还有代码,但是这些内容都可以正常工作,并且大多与打开数据库连接和搜索表单有关。

Also, the code I've included here is sample code and may be wrong in another way but I'm mostly just looking for an overall idea of how to go about building the query and the recursion to display the data the way I want it displayed. 另外,我在此处包含的代码是示例代码,可能在另一种方式上是错误的,但是我主要是在寻找有关如何构建查询和递归以我想要的方式显示数据的总体思路显示。

Thanks in advance for any help you all can offer. 预先感谢大家提供的任何帮助。

What I like to do is take data like that and put it into a multidimensional array that fits the display I want to output. 我喜欢做的是将这样的数据放入一个适合我要输出的显示的多维数组 It would depend on how much data you have though as huge arrays can cause problems. 这将取决于您拥有多少数据,因为巨大的阵列会导致问题。 I would do one query, order by person_id , then whatever other data you want. 我会进行一个查询, order by person_id ,然后再查询其他任何数据。 Then loop through your query and use the first array key to track the person_id . 然后循环查询,并使用第一个数组键跟踪person_id

$output_ary = array();
while($row = mysqli_fetch_array($r)) {
   if ( !array_key_exists($output_ary, $r['person_id'] ) {
      $output_ary[$r['person_id']] = array();
   }
   $output_ary[$r['person_id']][] = $r; // or a new array with the data elements you want
}

Then you can use nested foreach loops to output the data or whatever. 然后,您可以使用嵌套的foreach循环来输出数据或其他内容。 Probably not the most elegant or efficient solution, but for smaller data results, I find that putting the data into a structure that matches the way I want to display it works for me. 可能不是最优雅或最有效的解决方案,但对于较小的数据结果,我发现将数据放入与我要显示的方式相匹配的结构中对我来说很有效。

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

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