php循环值-mysql查找
我正在尝试创建一个用户配置文件页面。用户根据搜索选择要查看的个人资料。单击“查看配置文件”按钮,页面应转到profile.php,在那里显示用户的配置文件 现在,我只是想测试一下,只显示用户名。这是我的密码 我的问题是,我不知道如何将“$userID”传递给profile.php,然后profile.php将用于查找该用户的信息。由于该值位于while循环中,因此我不确定如何选择该循环的一次实例php循环值-mysql查找,php,mysql,Php,Mysql,我正在尝试创建一个用户配置文件页面。用户根据搜索选择要查看的个人资料。单击“查看配置文件”按钮,页面应转到profile.php,在那里显示用户的配置文件 现在,我只是想测试一下,只显示用户名。这是我的密码 我的问题是,我不知道如何将“$userID”传递给profile.php,然后profile.php将用于查找该用户的信息。由于该值位于while循环中,因此我不确定如何选择该循环的一次实例 function findUsers($friend){ $search = mysql_
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.
}
}
}
?>
函数查找器($friend){
$search=mysql_查询(“从用户名为“$friend”的用户中选择*);
$userLocation=mysql_查询(“从userinfo中选择*,其中username='$friend');
$locationResult=mysql\u fetch\u数组($userLocation);
$locationResultArray=$locationResult['userlocation'];
$locationExplode=explode(“~”,“$locationResultArray”);
//表列名称
回声“;
回声“;
回显“用户名”;
回声“;
回声“定位”;
回声“;
while($result=mysql\u fetch\u array($search))//循环以显示搜索
{
$userID=$result['userID'];//由于while循环中可能有多个userID,我可以将此值传递给函数吗?
echo$result['username'];
回声“;
echo$locationExplode['0'];
回声“,”;
echo$locationExplode['1'];
回声“;
?>
通常,我会将其作为一个链接,并将朋友的userid作为GET变量传递。
像这样的
echo "<a href='profile.php?userid=" . $result['userid'] . "'>". $result['username'] ."</a>";
echo”“;
对于您当前的设计,您可以选择:
将操作设置为
<form method="post" action="profile.php?userid=<?php echo $result['userid']; ?>">
”/>
第一个方法是作为POST变量传递,然后您可以从$\u POST['userid']
中检索,而在第二个方法中,您将从$\u GET['userid']中检索
variable.谢谢,工作得很好。我花了一点时间才弄明白,因为我没有意识到$u GET…必须在传递给它的页面上使用。
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>";
<input type='hidden' name='userid' value='<?php echo $result['userid']; ?>' />
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>";
$userid = $_GET['id'];