2012-01-17 48 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'];