我正在尝试允许用户通过提交按钮接受/拒绝对事件的请求。信息循环并连续显示(用户名,位置,接受,拒绝)。
现在正在显示2个用户;用户1和用户2(当前用于测试)。无论如何,我正在尝试使用正确的用户名来获取正确的用户ID。目前,无论我接受或拒绝哪个用户,都会显示用户2。我试图根据隐藏的输入设置userid的值,但它无法正常工作。
这是我的代码。
for($i=0;$i<count($userExplode)-1;$i++){
$user = mysql_query("select userid,username from users where userid = ".$userExplode[$i]." ");
$user = mysql_fetch_array($user);
$userLoc = mysql_query("select userLocation from userinfo where userid = ".$userExplode[$i]." ");
$location = mysql_fetch_array($userLoc);
$locationExplode = explode("~",$location['userLocation']);
//the displayed output is working correctly so I know it's setting $user['userid'] properly
echo '<form method="post"><table><tr><td><a href="profile.php?userid=' . $user['userid'] . '">' . $user['username'] . '</a></td>
<td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td>
<td><input type="hidden" name="userReq" value='.$user['userid'].'></td>
<td><input type="submit" name="accept" value="Accept Request" ></td>
<td><input type="submit" name="decline" value="Decline Request" ></td></tr>';
}
echo '</table></form>';
}
if(isset($_POST['accept'])){
echo $_POST['userReq']; //displays user 2 even if I click user 1
}
if(isset($_POST['decline'])){
echo $_POST['userReq']; //also displays user 2 even if i click user 1
}
}
答案 0 :(得分:2)
您的代码存在很多问题:
for
用锚点替换提交按钮,并在那里添加参数。然后你可以将锚设置为看起来像一个按钮。
<input type="submit" name="decline" value="Decline Request" >
变为
echo "<a href="yourfile.php?userid=".$user['userid'].">Decline request</a>";
删除所有<form>
并将所有内容加载到1个表中。
<强>结果:强>
<?php
if(isset($_GET['action']) AND isset($_GET['userid'])){
switch($_GET['action']){
case "accept":
// do whatever
break;
case "decline":
// do whatever
break;
default:
die('something wrong');
break;
}
}
echo '<table width="100%">';
for($i=0; $i <= count($userExplode); $i++){
$q = "
SELECT u.userid,u.username, userLocation
FROM users u
INNER JOIN userLocation ul ON u.userid = ul.userid
WHERE u.userid = ".$userExplode[$i]."
";
$rs = mysql_query($q) or die(mysql_error());
$user = mysql_fetch_array($rs);
$locationExplode = explode("~",$user['userLocation']);
//the displayed output is working correctly so I know it's setting $user['userid'] properly
echo '<tr><td><a href="profile.php?userid=' . $user['userid'] . '">' . $user['username'] . '</a></td>'.
'<td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td>'.
'<td><a href="yourfile.php?userid=' . $user['userid'] . '&action=accept">Accept Request</a></td>'.
'<td><a href="yourfile.php?userid=' . $user['userid'] . '&action=decline">Decline Request</a></td></tr>';
}
echo '</table>';
我认为这只是冰山一角。例如,你如何获得$userExplode
?这是非常奇怪和不合逻辑的。我假设你首先运行一个查询来获取所有用户,然后循环使用它?
答案 1 :(得分:1)
在您的示例中,您似乎遇到了一些代码问题。在这里清理一下:
for($i=0;$i<count($userExplode)-1;$i++) {
$user = mysql_query("select userid,username from users where userid = ".$userExplode[$i]." LIMIT 1");
$user = mysql_fetch_array($user);
$userLoc = mysql_query("select userLocation from userinfo where userid = ".$userExplode[$i]." LIMIT 1");
$location = mysql_fetch_array($userLoc);
$locationExplode = explode("~",$location['userLocation']);
echo '<form method="post" action=""><table><tr><td><a href="profile.php?userid=' . $user['userid'] . '">' . $user['username'] . '</a></td>
<td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td>
<td><input type="hidden" name="userReq" value='.$user['userid'].'></td>
<td><input type="submit" name="accept" value="Accept Request" ></td>
<td><input type="submit" name="decline" value="Decline Request" ></td></tr>';
echo '</table></form>';
}
if(isset($_POST['accept'])){
echo $_POST['userReq']; //displays user 2 even if I click user 1
}
if(isset($_POST['decline'])){
echo $_POST['userReq']; //also displays user 2 even if i click user 1
}
根据您的代码,传递的数组应为:
$users = "1|2";
$userExplode=explode("|",$users) // or whatever your delimiter is
要进行测试,请在开始循环之前执行var_dump($userExplode);
,以确保您已将所有条目分解。
原始页面的源转储也很有用。如果您可以发布,那么我可以看到您的代码如何呈现html表单。