组合两个查询的输出并在一个列表中显示信息

时间:2014-01-30 07:15:41

标签: php mysql sql

我目前有这个页面可以选择两个大量的信息并通过输出两个单独的列表显示一个列表,其中一个列表带有复选框以选择要添加到数据库的项目,另一个列表显示标题用户当前有但我需要简化输出,如果可能的话,只显示一个列表,其中包含一行上标题的信息,没有复选框或者用户订阅的那个旁边的勾号图像或十字架旁边的那些他们不是。

我添加了以下代码,并对查询和输出进行了评论

 // 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>

1 个答案:

答案 0 :(得分:0)

您可能需要外部加入:

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