2016-03-08 92 views
0

我想在另一个数组的foreach语句中使用一个数组中的变量。从一个foreach语句中检索变量并在另一个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新手。

回答

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

+0

感谢 - 似乎已经解决了它 - 只是一个问题,如果用户更改了他们的配置文件映像,我将如何更改该查询以获取每个user_id(理想状态下最新)的结果?我有两个框出现,因为对于同一个用户,在单独的行中有两个图像。 –

+0

一切都是可能的,但我认为它是你的数据库结构有问题(我更接近查询)。请描述你的表格的用途。可能是更改表格模式更好。 –

0

如果你想看到查询变种之一(与最新的配置文件图像),就是这样:

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 

但我认为它过于复杂,可能是错误的,在所有的,因为我不不了解你的表格模式。

+0

谢谢 - 我已决定更改照片上传文件中的SQL查询,以便我使用'REPLACE INTO'而不是'INSERT INTO',因为我真的不希望存储大量冗余行。每个用户只需要一个图像。 –