2012-01-17 103 views
0

我正在尝试创建一个用户配置文件页面。用户基于搜索选择他想要查看的用户简档。通过点击“查看个人资料”按钮,该页面应该转到profile.php,并显示用户的个人资料。php循环值 - mysql查找

现在,我只是想测试它,只显示用户的名字。这是我的代码。

我的问题是,我不知道如何将“$ userID”传递给profile.php,然后它将用于查找该用户的信息。由于该值处于while循环中,因此我不确定如何选择此循环的一次实例。

function findUsers($friend){ 
    $search = mysql_query("Select * from users where username='$friend'"); 
      $userLocation = mysql_query("select * from userinfo where username='$friend'"); 
      $locationResult = mysql_fetch_array($userLocation); 
      $locationResultArray = $locationResult['userlocation']; 
      $locationExplode = explode("~","$locationResultArray"); 

      //table column names 
      echo "<table>"; 
      echo "<tr><td>"; 
      echo "Username"; 
      echo "</td><td>"; 
      echo "Location"; 
      echo "</td></td><tr><td>"; 
       while($result = mysql_fetch_array($search)) //loop to display search 
       { 
         $userID = $result['userid']; //can I pass this value to the function since it's possible that there is more than 1 userID from the while loop? 
        echo $result['username']; 
        echo "</td><td>"; 
        echo $locationExplode['0']; 
        echo ", "; 
        echo $locationExplode['1']; 
        echo "</td><td>"; 
       ?> 
        <form method="post" action="profile.php"> 
       <? 
        echo "<input type='submit' name='profile' value='View User's Info'"; 
        echo "</td><td>"; 
       ?> 
        </form> 
        <form method="post" action="profile.php"> 
       <? 
        echo "<input type='submit' name='addfriend' value='Add Friend' />"; //code still needs to be written for this input. 
        echo "</td></tr>"; 
      } 
        echo "</table>"; 
      if(isset($_POST['profile'])){ 
       $viewProfile->displayProfile($userID); //This is where I'm not sure if it's taking the correct userID. 
      } 
     } 
} 
?> 

...和页面来显示轮廓

<? 
include_once 'infoprocesses.php'; 
$user = new dbProcessing(); 

Class viewProfile{ 
function displayProfile($username){ //display profile pulls the user's name from the databse 
    echo $username; //used to test if value is being sent...nothing is being displayed 
    ?> 
    <h2><?php $user->username($username);?>'s Information</h2> 
    <? 
    } 
} 
?> 

回答

0

通常情况下,我将它作为与朋友的用户ID作为GET变量传递的链接。 像这样

echo "<a href='profile.php?userid=" . $result['userid'] . "'>". $result['username'] ."</a>"; 

对于当前的设计,你的选择是:
设定的动作

<form method="post" action="profile.php?userid=<?php echo $result['userid']; ?>"> 

使与朋友的用户ID的隐藏输入字段。

<input type='hidden' name='userid' value='<?php echo $result['userid']; ?>' /> 

第一种方法是通过如POST变量,则可以从$_POST['userid']检索,而在第二种方法中,你会从$_GET['userid']变量检索。

+0

谢谢,非常完美。我花了一点时间才弄明白,因为我没有意识到$ _GET ...必须在传递给它的页面上使用。 – user1104854 2012-01-18 00:55:26

0
 while($result = mysql_fetch_array($search)) //loop to display search 
     { 
      echo $result['username']; 
      echo "</td><td>"; 
      echo $locationExplode['0']; 
      echo ", "; 
      echo $locationExplode['1']; 
      echo "</td><td>"; 
      echo '<a href="profile.php?id='.$result['userid'].'">View User's Info</a>'; 
     } 
     echo "</table>"; 

}

,并在profile.php只是

$userid = $_GET['id'];