我目前有这个页面来选择两批信息并将其显示到它的工作页面,方法是输出两个单独的列表,一个列表带有复选框,用于选择要添加到数据库的项目,另一个列表用于显示以下标题用户当前有,但如果可能的话,我需要简化输出,仅显示一个列表,该列表在一行上包含标题信息,并且没有复选框或用户订阅的列表旁边的勾号图像或十字在那些不是的旁边。
我添加了下面的代码并评论了查询和输出
// Check to see if the form has been submitted
if(isset($_POST['submit'])){
// Declare shorthand for the id value if there is $_POST data
$id = $_POST['userId'];
// Connect to the database
$objects->connect();
// Create a variable full of the posted array sub
$list = $_POST['sub'];
// for each loop to insert each value into the database with the selected users informtion
foreach ($list as $value) {
// The query to run
$listQuery='INSERT IGNORE INTO tbl_list (`userId`, `subId`) VALUES (\'' . $id . '\', \'' . $value . '\')';
// Run the query
$objects->query($listQuery);
}
}
else{
// Filter all of the $_GET data
$objects->filterEverything($_GET);
// Declare shorthand for the id value if there is $_GET data
$id = $objects->clean['userId'];
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
这是检查用户是否订阅了某个标题的第一个查询
////////////////////////////////////////////////////////////////////////////////////////////////////
// This section will select any existing titles that the selected client is subbed to
// Connect to the database
$objects->connect();
// The query to select the info for the clients current subb titles
$exist = 'SELECT a.`subId`, a.`userId`, b.`subTitle`, b.`subId` FROM `tbl_list`a, `tbl_subs`b WHERE a.`subId` = b.`subId` AND a.`userId` =' . $id;
// Create a variable and set it to the query
$result = $objects->query($exist);
// Consctuct the output
$existOutput = '';
// Loop through the results and create the output with the count
while($result = $objects->result->fetch_array(MYSQLI_BOTH)){
$existOutput .= '<p>' . $result['subId'] . '' . $result['subTitle'] . '</p>';
}
// Select the chosen users information from the database
// Connect to the database
$objects->connect();
// query the database
$query = 'SELECT `userId`,
`firstName`,
`lastName`
FROM `tbl_user`
WHERE `userId` =' . $id;
// Run the query
$objects->query($query);
//Store the results returned from the database
$row = $objects->result->fetch_array(MYSQLI_BOTH);
// Create the users output
$output = '' . $row['firstName'] . ' ' . $row['lastName'] . '';
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
这是第二个查询,用于创建带有复选框的标题列表以添加到用户列表请注意,该列表仍然显示用户订阅的标题
////////////////////////////////////////////////////////////////////////////////////////////////////
// This section selects the selected users name
// Connect to the database
$objects->connect();
// Query for the database
$selectionQuery = 'SELECT `subId`, `subTitle` FROM `tbl_subs`';
// Run the query
$objects->query($selectionQuery);
// Create the output
$subOutput = '';
$subOutput .= $objects->openForm();
while($row = $objects->result->fetch_array(MYSQLI_BOTH)){
$subOutput .= '<input type="checkbox" " name="sub[]" value="' . $row['subId'] . '" />' . $row['subTitle'] . '<br />';
}
$subOutput .= $objects->makeInput('userId','hidden','none');
$subOutput .= $objects->makeSubmitButton('submit','Edit','submit');
$subOutput .= $objects->closeForm();
}
else{
// If the user is not logged in then redirect to the login page
header("Location:index.php");
}
//Include the header for the page
include_once '../includes/header.php'
?>
<!-- The Body section starts here -->
<div id="body">
<div class="bodyBox">
<!-- The left side of the body -->
<div class="bodyLeft left">
<div class="cmsContainer">
<h2>Manage Subs</h2>
<div class="cmsMargin">
<p>Manage the subscriptions for <?php echo $output; ?></p>
<?php echo $subOutput; ?>
<?php echo $listTest; ?>
<p><a class="link left" href="index.php">Control Panel</a></p>
<p><?php echo $existOutput; ?></p>
最佳答案
您可能需要一个外部联接:
SELECT a.`subId`, a.`userId`, b.`subTitle`, b.`subId`,
CASE -- check if there was a matching b.`subId`
WHEN b.`subId` IS NOT NULL THEN 'x' ELSE ' ' END
END
FROM `tbl_list`a LEFT JOIN `tbl_subs`b
ON a.`subId` = b.`subId`
WHERE a.`userId` =' . $id
关于php - 合并两个查询的输出并在一个列表中显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21450054/