简体   繁体   中英

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; table_2: account_id, person_id, account_type, balance;

I want to display results like this:

(A)person_id - name: (a)account_type - balance, (a)account_type - balance, (a)account_type - balance

(B)person_id - name: (b)account_type - balance

(C)person_id - name: (c)account_type - balance, (c)account_type - balance


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. 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:

$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

(B)person_id - name:

(C)person_id - name:


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. 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. Then loop through your query and use the first array key to track the 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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