2014-01-30 16 views
0

我目前有這個頁面來選擇兩個批次的信息,並通過輸出兩個單獨的列表顯示一個列表與複選框選擇一個項目添加到它的工作頁面數據庫和另一個列表來顯示用戶當前擁有的標題,但是如果可能的話,我需要簡化輸出,只顯示一個包含標題信息的列表,其中的一行沒有任何複選框或旁邊有一個打勾圖像那些用戶訂閱的或者他們不在的那些旁邊的十字架。結合了兩個查詢的輸出並在一個列表中顯示信息

我已經添加下面的代碼和註釋查詢並輸出

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

回答

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 
+0

感謝您的答覆我我不熟悉使用情況,我試圖使用這個,但一定不能得到的東西,我只得到T_CONSTANT_ENCAPSED_STRING這裏是th e查詢 –

+0

$ exist ='SELECT a.'subId',a.'userId',b.'subTitle',b.'subId' CASE when b.'subId' = a.'subId' THEN'。$ x。 「 ELSE''END END FROM'tbl_list'a LEFT JOIN'tbl_subs'b ON a.'subId' = b.'subId' WHERE a.'userId' ='。 $ ID; –

+0

CASE之前沒有昏迷。你可能需要逃避空的字符串? ELSE''''結束? – dnoeth

相關問題