簡體   English   中英

顯示具有一對多關系的php mysql查詢結果

[英]displaying php mysql query result with one to many relationship

這是一個示例表:

info table
info_id     name  
1           john  
2           peter
------------------------  
details table  
details_id      log                               date  
1            test log for john                  2013-08-01  
1            another log for john               2013-08-02  
2            test log for peter                 2013-08-02

這是我的示例查詢:

SELECT info.info_id, info.name, details.details_no, details.log, details.date 
FROM info JOIN details ON details.details_id = info.info_id 
GROUP BY info.info_id  

這是我想要實現的顯示:

john  
1     test log for john            2013-08-01  
1     another test log for john    2013-08-02  

peter  
2     test log for peter           213-08-02  

我嘗試使用 foreach 循環,然后在第一個循環內執行另一個 foreach 循環。

請幫助伙計們

嘗試將您的結果變成如下所示的多維數組

注意:我假設details.details_nodetails表的主鍵,並且您希望結果類似於

john  
1     test log for john            2013-08-01  
2     another test log for john    2013-08-02  

peter  
3     test log for peter           213-08-02

您可以使用以下方法檢索

...
$qry = "
    SELECT
        info.info_id,
        info.name,
        details.details_no,
        details.log,
        details.date 
    FROM
        info
        JOIN details ON (details.details_id = info.info_id)
";
$result = mysqli_query($your_db_link,$qry)
$data = array();
while($row = mysqli_fetch_array($result)){
    $data[$row["info_id"]]["name"] = $row["name"];
    $data[$row["info_id"]]["logs"][$row["details_no"]] = array(
        "log"=>$row["log"],
        "date"=>$row["date"],
    );
}

將導致一個數組,如:

$data = array(
    "1" => array(
        "name" => "john",
        "logs" => array(
            "1" => array(
                "log" => "test log for john",
                "date" => "2013-08-01",
            ),
            "2" => array(
                "log" => "another test log for john",
                "date" => "2013-08-02",
            ),
        ),
    ),
    "2" => array(
        "name" => "peter",
        "logs" => array(
            "3" => array(
                "log" => "test log for peter",
                "date" => "2013-08-02",
            ),
        ),
    ),
);

然后你可以顯示如下:

echo "<table>";
foreach($data as $value){
    echo "<tr><td colspan='3'>".$value["name"]."</td></tr>";
    foreach($value["logs"] as $subkey => $subvalue){
        echo "<tr>";
        echo "<td>".$subkey."</td>";
        echo "<td>".$subvalue["log"]."</td>";
        echo "<td>".$subvalue["date"]."</td>";
        echo "</tr>";
    }
}
echo "</table>";

這將導致類似於:

<table>
    <tr>
        <td colspan='3'>john</td>
    </tr>
    <tr>
        <td>1</td>
        <td>test log for john</td>
        <td>2013-08-01</td>
    </tr>
    <tr>
        <td>2</td>
        <td>another test log for john</td>
        <td>2013-08-02</td>
    </tr>
    <tr>
        <td colspan='3'>peter</td>
    </tr>
    <tr>
        <td>3</td>
        <td>test log for peter</td>
        <td>2013-08-02</td>
    </tr>
</table>

您可能將不得不獲取數據並制作您想要的數組。

$data = array();
foreach ($result as $item) {
    $key = $item['name']; // or $item['info_id']
    if (!isset($data[$key])) {
        $data[$key] = array();
    }

    $data[$key][] = $item;
}

// Build your table with the new $data array

編輯

這只是一個例子。 正如 amaster507 指出的那樣,如果您的name字段不是唯一的,您將需要在唯一鍵上構建您的數組。 與此沒有太大不同,因為您可能只需將$item['name']實例更改為$item['info_id']

好的,先解釋一下問題。

當您使用GROUP BY ,您不再將每個結果作為一行獲得,而是獲得 1 行包含分組內容,也就是說,您可以使用GROUP BY來獲得類似於來自 john 的兩個log單元的連接的內容,例如.

要執行您想要的操作,請刪除GROUP BY以獲取所有行,然后手動處理它們。

我建議您groupBy這項工作,使用underscore.php ,它有一個名為groupBy的函數,它將返回一個分組在子數組中的數組,基於這種情況, info_id列。

它將被使用如下:

$array_with_all_the_rows = $query->fetchAll(); // Or whatever you use to get all rows in 1 array.
$grouped_array = __::groupBy($array_with_all_the_rows, 'info_id');

該分組數組將如下所示:

$grouped_array = Array( 
    "1" => Array(
        "info_id"=> "1", "log"=>"test log for john", "date" => "2013-08-01",
        "info_id"=> "1", "log"=>"another test log for john", "date" => "2013-08-02"
    ), 
    "2" => Array(
        "info_id"=> "2", "log"=>"test log for peter", "date" => "2013-08-02"
    )
)

要完善 Ascherer 的答案,您無需在使用[]語法將數據推送到數組之前聲明數組。 如果您使用array_push()則需要先將數組聲明為變量。

使用 MySQLi,您可以直接使用foreach()迭代結果集對象,並無條件地使用info_id值作為分組依據。

$sql = "SELECT info.info_id,
               info.name,
               details.log,
               details.date
        FROM info
        JOIN details ON details.details_id = info.info_id
        ORDER BY info.info_id";

$result = [];
foreach ($mysqli->query($sql) as $row) {
    $data[$row['info_id']][] = $row;
}

使用 PDO,您可以使用->fetchAll(PDO::FETCH_GROUP)來創建相同的輸出,而無需手動循環。

生成 html 標記時...

<table>
    <?php foreach ($result as $groupId => $rows) { ?>
        <tr>
            <td colspan='3'><?php echo $rows[0]["name"]; ?></td>
        </tr>
        <?php foreach($rows as $row) { ?>
            <tr>
                <td><?php echo $groupId; ?></td>
                <td><?php echo $row['log']; ?></td>
                <td><?php echo $row['date']; ?></td>
            </tr>
        <?php } ?>
    <?php } ?>
</table>

您是否嘗試過帶有鍵/值對的 foreach? 你能告訴我們你目前的 for 循環嗎? 結果數組的 print_r/var_dump 將使某人更容易為您提供幫助。

foreach ($result as $key=>$value) {
  print "Name Row\n";
  for (each display row) {

那或者 Ascherer 建議創建一個新的多維數組,這將使嵌套的 for 循環變得不必要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM