2012-01-17 11 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

通常を表示するページ、私がGET変数として渡された友人のユーザIDとのリンクとしてそれを行います。あなたの現在の設計では、この

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']; ?>' /> 

第一の方法は、第2の方法では、あなたが$_GET['userid']変数から取得する一方で、あなたは$_POST['userid']から取得することができ、POST変数として渡しています。

+0

おかげで、完全に働きました。私が$ _GET ...が渡されているページで使われなければならなかったことを認識していなかったので、それを理解するのに少し時間がかかりました。 – user1104854

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']; 
関連する問題