【问题标题】:Displaying a user profile page PHP显示用户个人资料页面 PHP
【发布时间】:2016-06-26 22:42:55
【问题描述】:

我目前正在创建一个社交网络。我正在使用以下代码显示所有注册用户。 有更好的方法吗?

<?php 
$query = $db->query("SELECT * from members");

while($r = $query->fetch()) {
    echo '<div class="col-sm-6 col-md-6 col-lg-3 membersGrid">';
    echo '<div class="profileThumb">';
    echo  '<img width="130" height="130" src="'.$r['profile_pic'].'"/><br>';
    echo $r['username'], '<br>';
    echo  $r['country'], '<br>';
    echo '<a class="viewProfile" href="profile.php"><button>View Profile</button></a>';
    echo '</div>';
    echo '</div>';

}
  ?>

我正在尝试为每个用户创建指向各个个人资料页面的链接。目前每个链接都在上面的while循环中,但是,我有点不确定如何链接到相关的个人资料页面。我猜我必须在 URL 中附加一个变量。任何指导将不胜感激。

    echo '<a class="viewProfile" href="profile.php"><button>View Profile</button></a>';

【问题讨论】:

  • 在您的 href=profile.php 添加成员 ID 作为参数,即 profile.php?memberid=1

标签: php mysql pdo user-profile


【解决方案1】:

这将取决于您的 profile.php 页面如何处理 GET 变量,该变量决定了您所展示的人的个人资料。假设变量名为id,并且您的members 表中有一行也称为id(充当唯一键),那么您的锚标记将如下所示:

echo '<a class="viewProfile" href="profile.php?id=' . $r['id']. '"><button>View Profile</button></a>';

【讨论】:

    【解决方案2】:

    在查询时,首先从数据库中检索用户的 user_id。然后将此 id 提供给配置文件链接:

    echo '<a href="profile.php?userid=' . $user_id . '>Linkname</a>';
    

    然后在profile.php中通过:

    获取这个变量
    $id = $_GET['userid'];
    

    这样您就可以在profile.php 中显示相关用户的个人资料。

    希望你能得到这个想法。

    【讨论】:

      【解决方案3】:

      如果您要生成指向用户配置文件的链接列表,那么有几个不同的选项:

      您可以将 $_GET 变量附加到链接的末尾,如 Lloyd 之前提到的那样

      echo '<a href="profile.php?if=' . $r['id'] . '">';
      

      或者你可以发送一个$_POST 变量;最简单的方法是创建一个表单列表:

      <?php 
      $query = $db->query("SELECT * from members");
      while($r = $query->fetch()) {
          echo '<div class="col-sm-6 col-md-6 col-lg-3 membersGrid">';
          echo '<div class="profileThumb">';
          echo '<img width="130" height="130" src="'.$r['profile_pic'].'"/><br>';
          echo $r['username'], '<br>';
          echo $r['country'], '<br>';
          echo '<form action="profile.php" method="post">';
          echo '<input type="hidden" value="' . $r['id'] . '" />';
          echo '<input type="submit" value="View Profile" />';
          echo '</form>';
          echo '</div>';
          echo '</div>';
      }
      ?>
      

      抱歉,如果我误读了您的问题,希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-08
        • 1970-01-01
        • 2020-01-23
        • 1970-01-01
        • 2016-11-26
        • 1970-01-01
        • 2014-01-29
        • 1970-01-01
        相关资源
        最近更新 更多