【问题标题】:Retrieving variable from one foreach statement and displaying in another foreach statement从一个 foreach 语句中检索变量并在另一个 foreach 语句中显示
【发布时间】:2016-03-08 09:15:00
【问题描述】:

我正在尝试在另一个数组的 foreach 语句中使用一个数组中的变量。

我有一个网格视图,它显示用户的个人资料照片、他们的显示名称、当前位置和可用性,以及使用他们的用户名来完成应用于框的链接。

用户的表持有;我将来需要的用户名(和用户 ID)。

配置文件表保存;显示名称。

profilephotos 表保存;个人资料照片。

位置表保存;当前位置。

所有都通过与用户表匹配的 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);

无论我尝试什么 - 我都无法让 foreach 获取正确的图像。我已经设法提取了一个图像,但是 foreach 语句将相同的图像应用到每个用户,而不管我指示查询 $profilephotos 查找的 ID 是什么。

我做错了什么?我是否以正确的方式解决这个问题?

任何帮助将不胜感激 - 尽管我是 PHP 新手,但请注意。

【问题讨论】:

    标签: php mysql arrays foreach


    【解决方案1】:

    您需要一个查询而不是两个。 查询可能如下所示:

    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”——它有助于消除不同表中相同列名的歧义。

    【讨论】:

    • 谢谢 - 这似乎已经解决了 - 只有一个问题,如果用户更改了他们的个人资料图片,我将如何更改该查询以仅获取每个 user_id 的一个结果(理想情况下是最新的)?我出现了两个框,因为同一用户的不同行中有两个图像。
    • 一切皆有可能,但我认为您的数据库结构有问题(我已经更接近查询)。请描述您的表格用途。更改表架构可能会更好。
    【解决方案2】:

    如果您想查看其中一个查询变体(带有最新的个人资料图片),就是这样:

    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
    

    但我认为它太复杂了,可能根本就错了,因为我不知道你的表架构。

    【讨论】:

    • 谢谢 - 我决定更改照片上传文件中的 SQL 查询,以便我使用 REPLACE INTO 而不是 INSERT INTO,因为我真的不想要很多冗余行被存储。每个用户只需要一张图片。
    猜你喜欢
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    相关资源
    最近更新 更多