在PHP中按组名回应用户

时间:2011-11-15 17:11:44

标签: php mysql database

我想要做的是点击一个组的名称(我创建的除poweruser和admin组之外的每个组),这将从数据库中回显该组中的所有用户。到目前为止,我已经找到了代码,但现在我的问题是如何在点击群组名称时将其全部打印出来?

到目前为止我的代码是:

<h3>Groups</h3>
<?php
include('db.php');
if (isset($_GET["groupID"])) {
$sql="SELECT `group`.*, `user`.* FROM `user` inner join `group` on group.groupID=user.groupID where group.groupID= " . mysql_real_escape_string($_GET["groupID"]) ;
} else {
$sql="SELECT * FROM `group` WHERE groupName <> 'admin' AND groupName <> 'poweruser'"  ;
}
$result=mysql_query($sql,$connection);
while($line=mysql_fetch_array($result)){
echo "<a href='index.php?page=groups&group=".$line['groupID']."'>".$line['groupName'].'</a><br />';
}
mysql_free_result($result);
mysql_close($connection);
?>

4 个答案:

答案 0 :(得分:1)

除了$ _GET [“group”]问题之外,它看起来不像是在任何地方打印出用户。

您应该将输出移动到if块,并为第一个条件创建新输出。

<?php
include('db.php');
if (isset($_GET["groupID"])) {
$sql="SELECT `group`.*, `user`.* FROM `user` inner join `group` on group.groupID=user.groupID where group.groupID= " . mysql_real_escape_string($_GET["groupID"]) ;

$result=mysql_query($sql,$connection);

//Output users after Group Selected
//Could be placed outside the if block to allow user to select a different group
echo("<h3>Users</h3>");
while($line=mysql_fetch_array($result)){
    //echo out the user data here
}

} else {
$sql="SELECT * FROM `group` WHERE groupName <> 'admin' AND groupName <> 'poweruser'"  ;
$result=mysql_query($sql,$connection);

//Output to let user select group.
echo("<h3>Groups</h3>");
while($line=mysql_fetch_array($result)){
echo "<a href='index.php?page=groups&group=".$line['groupID']."'>".$line['groupName'].'</a><br />';
}

}
mysql_free_result($result);
mysql_close($connection);
?>

答案 1 :(得分:0)

if (isset($_GET["groupID"])) {

应该不是

if (isset($_GET["group"])) {

还在$ sql查询中将$ _GET [“groupID”]更改为$ _GET [“group”]

答案 2 :(得分:0)

以下代码是您如何做到的示例。只需将它放在您已有的代码下面:

if ($_GET['page'] == 'groups'):
$groupID = $_GET['groupID'];
$sql = "SELECT * FROM users WHERE groupID = '$groupID'"; 
$res = mysql_query($sql); //MySQL query
$count = mysql_num_rows($res); //This counts our results
if($count != 0): //This makes sure we have at least 1 result
   while($user = mysql_fetch_assoc($res)):
     echo $user['name'];
   endwhile;
endif;
endif;

答案 3 :(得分:0)

将Peter Stuart的代码添加到您的文件中并进行更改 <a href='index.php?page ... ><a href='groups.php?page ... >