我有一个数据库保存有关我们提供的课程的数据。所以我有2个表,一个用于保存课程详细信息,2个用于保存参与者详细信息,在每个记录中我保存了他们所属的课程。
表1(CourseInfo): ID,CourseName,CourseStartDay等
表2(UserInfo): ID,FirstName等,CourseID
现在,我希望在我的页面上显示所有可用课程,并在子列表中显示该特定课程的所有参与者。
像这样:
我的代码:
<?php require_once('Connections/JPT.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "1";
$MM_donotCheckaccess = "true";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && true) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "index.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_UserDetails = "-1";
if (isset($_SESSION['MM_Username'])) {
$colname_UserDetails = $_SESSION['MM_Username'];
}
mysql_select_db($database_JPT, $JPT);
$query_UserDetails = sprintf("SELECT * FROM UserInfo WHERE Username = %s", GetSQLValueString($colname_UserDetails, "text"));
$UserDetails = mysql_query($query_UserDetails, $JPT) or die(mysql_error());
$row_UserDetails = mysql_fetch_assoc($UserDetails);
$totalRows_UserDetails = mysql_num_rows($UserDetails);
mysql_select_db($database_JPT, $JPT);
$query_GroupList = "SELECT * FROM GroupList WHERE Status = 'A' ORDER BY ID DESC";
$GroupList = mysql_query($query_GroupList, $JPT) or die(mysql_error());
$row_GroupList = mysql_fetch_assoc($GroupList);
$totalRows_GroupList = mysql_num_rows($GroupList);
// $group_id = $row_GroupList['ID'];
//mysql_select_db($database_JPT, $JPT);
//$query_PartcipantsList = sprintf("SELECT FirstName, Surname, Username, EmailAddress, HouseNumber, Address1, Address2, City, PostCode, Country, Phone, Mobile, ReferedBy, PaymentMethod, ChargeAmount, PaidAmount, GroupID, Status, UserType FROM UserInfo WHERE Status='A' AND UserType=2 AND GroupID=%s", GetSQLValueString($group_id, "int"));
//$PartcipantsList = mysql_query($query_PartcipantsList, $JPT) or die(mysql_error());
//$row_PartcipantsList = mysql_fetch_assoc($PartcipantsList);
//$totalRows_PartcipantsList = mysql_num_rows($PartcipantsList);
//$particpant_FirstName = $row_PartcipantsList['FirstName'];
$colname_UserType = "-1";
if (isset($row_UserDetails['UserType'])) {
$colname_UserType = $row_UserDetails['UserType'];
}
if ($colname_UserType != 1) {
header('location: linklist.php');
exit;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>List of Partcipants</title>
</head>
<body>
<?php do {
?>
<table border="1">
<tr>
<td>Group ID</td>
<td>Group Name</td>
<td>Start Date</td>
<td>Notes</td>
</tr>
<tr>
<td><?php echo $row_GroupList['ID']; ?></td>
<td><?php echo $row_GroupList['GroupName']; ?></td>
<td><?php echo $row_GroupList['StartDate']; ?></td>
<td><?php echo $row_GroupList['Notes']; ?></td>
</tr>
</table>
<p/>
<table border="1">
<tr>
<td width="175px">Name</td>
<td>Email Address</td>
<td>Address</td>
<td>Country</td>
<td>Phone</td>
<td>Refered By</td>
<td>Payment Method</td>
<td>Charge Amount</td>
<td width="50px">Paid Amount</td>
<td width="50px">Group</td>
</tr>
<?php do {
$group_id_new = $row_GroupList['ID'];
mysql_select_db($database_JPT, $JPT);
$query_PartcipantsList = sprintf("SELECT FirstName, Surname, Username, EmailAddress, HouseNumber, Address1, Address2, City, PostCode, Country, Phone, Mobile, ReferedBy, PaymentMethod, ChargeAmount, PaidAmount, GroupID, Status, UserType FROM UserInfo WHERE Status='A' AND UserType=2 AND GroupID=%s", GetSQLValueString($group_id_new, "int"));
$PartcipantsList = mysql_query($query_PartcipantsList, $JPT) or die(mysql_error());
$row_PartcipantsList = mysql_fetch_assoc($PartcipantsList);
$totalRows_PartcipantsList = mysql_num_rows($PartcipantsList);
//take in all fields in a variable with a short name
$pt_Add1 = $row_PartcipantsList['HouseNumber'];
$pt_Add2 = $row_PartcipantsList['Address1'];
$pt_Add3 = $row_PartcipantsList['Address2'];
$pt_Add4 = $row_PartcipantsList['City'];
$pt_Add5 = $row_PartcipantsList['PostCode'];
//create variables with commas to concatenate the address
$pt_result_Add1 = ($pt_Add1 != "") ? $pt_Add1 . " " : "";
$pt_result_Add2 = ($pt_Add2 != "") ? $pt_Add2 . ", " : "";
$pt_result_Add3 = ($pt_Add3 != "") ? $pt_Add3 . ", " : "";
$pt_result_Add4 = ($pt_Add4 != "") ? $pt_Add4 . ", " : "";
$pt_result_Add5 = ($pt_Add5 != "") ? $pt_Add5 : "";
//concatenate the address
$pt_Add_Conc = $pt_result_Add1 . $pt_result_Add2 . $pt_result_Add3 . $pt_result_Add4. $pt_result_Add5 ;
?>
<tr>
<td><?php echo $row_PartcipantsList['FirstName'] . " " . $row_PartcipantsList['Surname']; ?></td>
<td><?php echo $row_PartcipantsList['EmailAddress']; ?></td>
<td><?php echo $pt_Add_Conc; ?></td>
<td><?php echo $row_PartcipantsList['Country']; ?></td>
<td><?php echo $row_PartcipantsList['Phone']; ?></td>
<td><?php echo $row_PartcipantsList['ReferedBy']; ?></td>
<td><?php echo $row_PartcipantsList['PaymentMethod']; ?></td>
<td><?php echo $row_PartcipantsList['ChargeAmount']; ?></td>
<td><?php echo $row_PartcipantsList['PaidAmount']; ?></td>
<td><?php echo $row_PartcipantsList['GroupID']; ?></td>
</tr>
<?php } while ($row_PartcipantsList = mysql_fetch_assoc($PartcipantsList)); ?>
<?php } while ($row_GroupList = mysql_fetch_assoc($GroupList)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($UserDetails);
mysql_free_result($GroupList);
mysql_free_result($PartcipantsList);
?>
答案 0 :(得分:0)
取决于您使用的是mysqli还是mysql,有几种方法可以处理这个
<强>的foreach(mysqli的)强>
$query = $db->query("SELECT * FROM.... WHERE ....");
$data = $db->get($query);
foreach($data as $key => $value) {
echo '<li>'.$value["courseid"].'</li><br/><ul>';
$courseid = $value["courseid"];
$query2 = $db->query("SELECT * FROM `userinfo` WHERE `courseid` = '$courseid'");
$data2 = $db->get($query2);
if($data2 != null) {
foreach($data2 as $key => $value) {
echo '<li>'.$value["username"].'</li>';
}
echo '</ul>';
}
}
答案 1 :(得分:0)
您可以通过加入以下两个表来完成此操作
$q=mysql_query("SELECT C.CourseName,C.CourseStartDay,U.FirstName FROM CourseInfo AS C LEFT JOIN UserInfo AS U ON C.ID = U.CourseID");
while($row = mysql_fetch_assoc($q)) {
/*echo the results*/
echo $row["CourseName"]."</br>";
echo $row["FirtsName"]."</br>";
}
答案 2 :(得分:0)
直截了当的方式非常简单。对于每个课程,使用WHERE
上的UserInfo.CourseId
子句查询数据库,以使所有参与者都能参加此课程并显示它。
(顺便说一句,我觉得很奇怪,参与者只能按照一门课程。)
此方法的主要缺点是您需要为显示的每个课程提供额外的查询。有10个左右的条目,这不会有问题,但如果你发现自己有数百个,这可能会成为问题。
优化的解决方案是获取所有课程和所有参与者,并使用小算法对其进行排序:
<?php
$link = // Connect to mysql using mysqli_connect()
// Get courses
$coursesResult = mysqli_query($link, 'SELECT * FROM CourseInfo');
// Get participants
$usersResult = mysqli_query($link, 'SELECT * FROM USerInfo');
$users = array();
// Sort users by course
while ($row = mysqli_fetch_array($link, $usersResult)) {
// Create the entry
if (!isset($courses[$row['courseID']]))
$users[$row['courseID']] = array();
// Add user to the list
$users[$row['courseID']][] = array (
'id' => $row['id'],
'name' => $row['name'],
// ...
);
}
// Display user list along the courses
while ($row = mysqli_fetch_array($link, $coursesResult)) {
echo 'This is course ' . $row['name'];
echo 'Users are :'
if (isset($users[$row['id']])) {
foreach ($users[$row['id']] as $ulist) {
echo '* ' . $users['name']
}
}
}