簡體   English   中英

從一個foreach語句中檢索變量並在另一個foreach語句中顯示

[英]Retrieving variable from one foreach statement and displaying in another foreach statement

我試圖在另一個數組的foreach語句中使用一個數組中的變量。

我有一個網格視圖,其中顯示用戶的個人資料照片,他們的顯示名稱,當前位置和可用性,以及使用他們的用戶名來完成應用於該框的鏈接。

用戶表保存; 用戶名(和用戶ID)將來是否應該使用。

配置文件表保存; 顯示名稱。

profilephotos表保存; 個人資料照片。

位置表保存; 當前位置。

在表中,所有這些都通過與users表匹配的user_id和username列進行鏈接。

我為用戶框提供的代碼是;

<?php foreach($rows as $row): ?> 
<div class="box">
    <div class="boxInner">
      <a href="profile.php?username=<?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8'); ?>&uid=<?php echo htmlentities($row['id'], ENT_QUOTES, 'UTF-8'); ?>">


      <img src="uploads/profile-photos/<?php echo htmlentities($pphoto['profilephoto_file'], ENT_QUOTES, 'UTF-8'); ?>" />


      <div class="titleBox" style="text-align:left; line-height:20px;">
      <span style="font-size:18px; font-weight:bold;"><i class="fa fa-user"></i> <?php echo htmlentities($row['profile_displayname'], ENT_QUOTES, 'UTF-8'); ?>,
      <?php echo htmlentities($row['profile_displayage'], ENT_QUOTES, 'UTF-8'); ?></span>
      <br />
      <span style="font-size:14px;"><i class="fa fa-map-marker"></i> City Name &nbsp;|&nbsp; <i class="fa fa-clock-o"></i> Now</span>
      </div></a>
    </div>
  </div>
<?php endforeach; ?> 

我的SQL查詢和數組代碼是;

$query = " 
        SELECT 
            users.id, 
            users.username,
            users.email,
            profiles.profile_displayname,
            profiles.profile_displayage,
            profiles.profile_photo
        FROM users, profiles
        WHERE users.id = profiles.user_id;
    "; 

    try 
    { 
        // These two statements run the query against your database table. 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $rows = $stmt->fetchAll(); 

    $query = " 
        SELECT 
            users.id, 
            users.username,
            profilephotos.user_id,
            profilephotos.profilephoto_file
        FROM users, profilephotos
        WHERE users.id = profilephotos.user_id;
    "; 

    try 
    { 
        // These two statements run the query against your database table. 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $profilephotos = $stmt->fetchAll(PDO::FETCH_ASSOC);

無論我嘗試什么,我都無法獲得正確的印象。 我設法插入圖像,但是無論我指示查詢$profilephotos查找的ID如何,foreach語句都將同一圖像應用於每個用戶。

我究竟做錯了什么? 我什至會以正確的方式這樣做嗎?

任何幫助將不勝感激-請注意,盡管我是PHP新手。

您需要一個查詢而不是兩個查詢。 該查詢可能看起來像這樣:

SELECT 
            users.id AS id, 
            users.username AS username,
            users.email as email,
            profilephotos.profilephoto_file AS file_photo,
            profiles.profile_displayname AS file_displayname,
            profiles.profile_displayage AS displaypage,
            profiles.profile_photo AS photo
        FROM users
        JOIN profilephotos ON users.id = profilephotos.user_id
        JOIN profiles ON users.id = profiles.user_id;

您需要兩次使用聯接-更好的實踐。 並注意關鍵字“ AS”-有助於消除不同表中相同的列名稱的歧義。

如果要查看查詢變體之一(帶有最新的個人資料圖片),就是這樣:

SELECT 
            users.id AS id, 
            users.username AS username,
            users.email as email,
            profilephotos.profilephoto_file AS file_photo,
            profiles.profile_displayname AS file_displayname,
            profiles.profile_displayage AS displaypage,
            profiles.profile_photo AS photo
        FROM users
        LEFT JOIN profiles ON users.id = profiles.user_id
        LEFT JOIN profilephotos ON users.id = profilephotos.user_id
        WHERE profilephotos.id in (select max(id) from profilephotos group by user_id)
        ORDER BY id

但是我認為它太復雜了,可能根本是錯誤的,因為我不知道您的表架構。

暫無
暫無

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

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