簡體   English   中英

使用while循環在同一表行中輸出特定id的數據

[英]Using while loop to output data of a specific id in same table row

我想我需要在 while 循環中使用 while 循環,但是當我這樣做時,我會收到奇怪的結果。

我有以下 PHP:

$sql = "SELECT c.id, c.title, c.image, u.profileImage, u.fname, u.lname, u.level AS ulevel, u.city, u.state, u.hs, u.ps, uc.id, uc.cid 
        FROM c, u, uc 
        WHERE uc.cid = c.id AND uc.userid = u.id 
        ORDER BY usercollege.createdate ASC";


        if ($result = mysqli_query($conn, $sql)) {
            while ($row = mysqli_fetch_assoc($result)) {


            if ($row['status'] == null) {
                 echo "";
            }

            if ($row['ulevel'] == "A Profile") {
                 echo '<tr>' .
                     '<td class="small">' . '<a href="http://www.example.com/aprofile.php?user="'.$row["username"] . '>' . '<img src="'.$row['profileImage'].'" />' . '</a>' . '</td>' .
                     '<td class="large">' . $row['fname']. ", " . $row['lname'] . '</td>' .
                     '<td class="large">' . $row['city'] . ", " . $row['state'] . '</td>' .
                     '<td class="large">' . $row['hs'] . '</td>' .
                     '<td class="largest">' .
                          '<div class="Limage">' . '<img src="images/c/'.$row['image'].'"/>' . '</div>' . 
                     '</td>' .
                  '</tr>';

<div class="Limage"> $row['image']目前只返回<div class="Limage">圖片,但數據庫每個用戶 ID 最多可以有 10 張圖片。 顯然,這個循環(如果每個用戶有多個圖像)將為具有不同圖像的用戶創建一個新行。

我試圖實現讓所有圖像(最多 10 個)出現在同一用戶的同一行中,而不在表中復制同一用戶。

如果每一行都包含相同的數據,除了頭像,您需要在查詢中連接行:


SELECT c.id, c.title, GROUP_CONCAT(c.image SEPARATOR ','), u.profileImage, u.fname, u.lname, u.level AS ulevel, u.city, u.state, u.hs, u.ps, uc.id, uc.cid 
        FROM c, u, uc 
        WHERE uc.cid = c.id AND uc.userid = u.id
        GROUP BY c.id
        ORDER BY MAX(usercollege.createdate) ASC

然后您的結果(圖像字段)包含如下內容:

avatar1.png,avatar2.png,avatar3.png,avatar4.png

現在您可以分解 sql-result 並在 foreach 循環中顯示。

雖然您的問題有點難以理解,但我可以告訴您您需要做什么,這不是循環中的循環(即使是)。 所以因為我不在乎決定你的架構,但我想我理解你的問題,我會用偽代碼概述它

首先你需要組織你的數據

if ($result = mysqli_query($conn, $sql)) {

     $users = []; //initialize variable to store data in. 

     while ($row = mysqli_fetch_assoc($result)) {
          if( !isset( $users['u'.$row['id']]){
                $users['u'.$row['uid']] = $row; //group users by id 
           }
           $users['u'.$row['uid']]['images'][] = $row['image'];
     }
      //*note you'll have to probably add  u.id as uid to you select.

     foreach( $users as $user ){
     //... code/html for displaying user (open).
        foreach( $user['images'] as $img){
        //... code/html for displaying image.
        }//end foreach image
    //... code/html for displaying user (close).
    }//end foreach user

當你用var_export()等輸出它時,你最終得到的$users數組是這樣的。

    [ 
       [ 'u10' => [
             'id' => 10,
             'images' => [
                  '{src for image1}',
                  '{src for image2}',
             ]

       ]

當您通過連接拉取記錄時,您會得到用戶與圖像之間的一對多關系的反映。 因此,每個圖像行都有相同的用戶行。 您需要做的就是按用戶 ID 對它們進行分組,這將使您的代碼下游更容易。 除非您與 1000 名用戶打交道,否則它對時間的影響可能很小,並且非常值得簡化 HTML 結構。 它們在 sql 中返回的順序可能會導致各種問題,您可以從用戶1開始有10其他用戶,然后以用戶1結束,這樣它們就可以在第一個循環中很好地分組。 要將它們分組,我們可以依賴 PHP 中的關聯鍵是唯一的這一事實。

我在那里添加u{id}是因為某些 PHP 函數有一種放置數字鍵的方法,這只是避免了這成為一個問題。 大多數情況下不需要它,但它是預防性的並且花費很少的精力,使數組更易於閱讀並且 IMO 成為一個好習慣。

最后,如果我在 CSS 中#10{ ..no dice.. }id="10" ( #10{ ..no dice.. } ) 的id="u10"是無效的,但id="u10"是。 可以在此處找到對此的一些解釋

https://css-tricks.com/ids-cannot-start-with-a-number/

您可以通過 uc.userid (和其他人)添加訂單,以便在結果數組中用戶將密切關注他們的所有原始數據。 之后,在您的 while 循環中,您可以檢查用戶 ID 是否更改 - 那么它是一個新用戶。

暫無
暫無

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

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