2017-08-30 35 views
0

我有2個數據庫表user_pmuser_pm_hidePHP/MySQL的多個嵌套的SELECT

user_pm

user_pm_hide

Basicly我想選擇user_pm_hide數據,然後在user_pm_hideuser_pm其中id != pm_id選擇數據。

我已編碼像我所需要的,但有問題..

我的代碼隱藏只有一個消息,有最新的ID,所有其他保持可見。

我的骨灰級功能:

public function inbox(){ 
    try{ 

     require_once("../System/class.user.php"); 
     $auth_user = new USER(); 

     $id = $_SESSION['user_session']; 
     $user_group = $_SESSION['user_session']; 

     $stmt = $auth_user->runQuery("SELECT * FROM users WHERE id=:id"); 
     $stmt->execute(array(":id"=>$id)); 

     $userRow=$stmt->fetch(PDO::FETCH_ASSOC); 
     $username = $userRow['id']; 

     $stmt = $this->conn->prepare("SELECT count(*) FROM user_pm WHERE sentto=('$username')"); 
     $stmt->execute(); 
     $inboxall = $stmt->fetchColumn(); 

     $stmt = $this->conn->prepare("SELECT count(*) FROM user_pm_hide WHERE user_id=('$username') AND sender != ('$username') "); 
     $stmt->execute(); 
     $inboxhidden = $stmt->fetchColumn(); 

     $inboxtotal = $inboxall - $inboxhidden; 

     $inboxperpage = 15; 
     $inboxpages = ceil($inboxtotal/$inboxperpage); 

     $inbox_get_pages = isset($_GET['inboxpage']) ? $_GET['inboxpage'] : 1; 

     $inboxdata = array(

      'options' => array(
       'default' => 1, 
       'min_range' => 1, 
       'max_range' => $inboxpages 
       ) 
     ); 

     $inboxnumber = trim($inbox_get_pages); 
     $inboxnumber = filter_var($inboxnumber, FILTER_VALIDATE_INT, $inboxdata); 
     $inboxrange = $inboxperpage * ($inboxnumber - 1); 

     $inboxprev = $inboxnumber - 1; 
     $inboxnext = $inboxnumber + 1; 

     $stmt = $this->conn->prepare("SELECT * FROM user_pm WHERE sentto=('$username') ORDER BY id DESC"); 
     $stmt->execute(); 

     $inboxresult = $stmt->fetchAll(); 

     foreach($inboxresult as $result){ 

      $checkid = $result['id']; 
      $stmt = $this->conn->prepare("SELECT pm_id FROM user_pm_hide WHERE pm_id=('$checkid') AND sender !=('$username') ORDER BY id "); 
      $stmt->execute(); 

      $checkidresult = $stmt->fetchAll(); 

      if($inboxhidden != 0){ 

      foreach($checkidresult as $check){ 
       $hiddenMessage = $check['pm_id']; 
       echo $hiddenMessage; 

       $stmt = $this->conn->prepare("SELECT * FROM user_pm WHERE sentto=('$username') AND id !=('$hiddenMessage') ORDER BY id DESC LIMIT :limit, :perpage"); 
       $stmt->bindParam(':perpage', $inboxperpage, PDO::PARAM_INT); 
       $stmt->bindParam(':limit', $inboxrange, PDO::PARAM_INT); 
       $stmt->execute(); 

       $inboxresult = $stmt->fetchAll(); 

       foreach($inboxresult as $message){ 

        $stmt = $auth_user->runQuery("SELECT * FROM users WHERE id=:sentby"); 
        $stmt->execute(array(":sentby"=>$message['sentby'])); 

        $userRow=$stmt->fetch(PDO::FETCH_ASSOC); 

        $date = $message['sent']; 
        $timestamp = strtotime($date); 

        $strTime = array("second", "minute", "hour", "day", "month", "year"); 
        $length = array("60","60","24","30","12","10"); 

        $currentTime = time(); 
        if($currentTime >= $timestamp) { 
         $diff  = time()- $timestamp; 
         for($i = 0; $diff >= $length[$i] && $i < count($length)-1; $i++) { 
          $diff = $diff/$length[$i]; 
         } 
         $diff = round($diff); 
        } 
        ?> 

        <a href="/Users/ViewMessages.php?inboxpage=<?php echo $inboxnumber?>?action=View&id=<?php echo $message['id']?>"> 
       <div class="mail_list"> 
       <div class="left"> 
           <img src="../Users/<?php echo $userRow['image']; ?>" style="width:50px; height:50px"> 
       </div> 
       <div class="right"> 
           <h3 style="margin-left:30px"><?php echo $userRow['username']?><small><?php echo $diff . " " . $strTime[$i] . "(s) ago "; ?></small></h3> 
           <p style="margin-left:30px"><?php echo $message['title']?><small style="float:right"><?php echo $message['readed']?></small></p> 
       </div> 
       </div> 
      </a> 

        <?php 
       } 

       if($inboxresult && count($inboxresult) > 0){ 
        echo "<center>"; 
      echo "<h4>Page $inboxnumber/$inboxpages</h4>"; 
        echo "<h4>Messages : $inboxtotal</h4>"; 

        # first page 
      if($inboxnumber <= 1) 
       echo "<span>&laquo; prev</span> | <a href=\"?inboxpage=$inboxnext\">next &raquo;</a>"; 
       # last page 
      elseif($inboxnumber >= $inboxpages) 
       echo "<a href=\"?inboxpage=$inboxprev\">&laquo; prev</a> | <span>next &raquo;</span>"; 
        # in range 
      else 
       echo "<a href=\"?inboxpage=$inboxprev\">&laquo; prev</a> | <a href=\"?inboxpage=$inboxnext\">next &raquo;</a>"; 
         echo "</center>"; 
      }else{ 
       echo "<p>No results found.</p>"; 
      } 

       return $inboxresult; 
      } 
      }else{ 
       $stmt = $this->conn->prepare("SELECT * FROM user_pm WHERE sentto=('$username') ORDER BY id DESC LIMIT :limit, :perpage"); 
       $stmt->bindParam(':perpage', $inboxperpage, PDO::PARAM_INT); 
       $stmt->bindParam(':limit', $inboxrange, PDO::PARAM_INT); 
       $stmt->execute(); 

       $inboxresult = $stmt->fetchAll(); 

       foreach($inboxresult as $message){ 

        $stmt = $auth_user->runQuery("SELECT * FROM users WHERE id=:sentby"); 
        $stmt->execute(array(":sentby"=>$message['sentby'])); 

        $userRow=$stmt->fetch(PDO::FETCH_ASSOC); 

        $date = $message['sent']; 
        $timestamp = strtotime($date); 

        $strTime = array("second", "minute", "hour", "day", "month", "year"); 
        $length = array("60","60","24","30","12","10"); 

        $currentTime = time(); 
        if($currentTime >= $timestamp) { 
         $diff  = time()- $timestamp; 
         for($i = 0; $diff >= $length[$i] && $i < count($length)-1; $i++) { 
          $diff = $diff/$length[$i]; 
         } 
         $diff = round($diff); 
        } 
        ?> 

        <a href="/Users/ViewMessages.php?inboxpage=<?php echo $inboxnumber?>?action=View&id=<?php echo $message['id']?>"> 
         <div class="mail_list"> 
          <div class="left"> 
           <img src="../Users/<?php echo $userRow['image']; ?>" style="width:50px; height:50px"> 
          </div> 
          <div class="right"> 
           <h3 style="margin-left:30px"><?php echo $userRow['username']?><small> <?php echo $diff . " " . $strTime[$i] . "(s) ago "; ?> </small></h3> 
           <p style="margin-left:30px"><?php echo $message['title']?><small style="float:right"><?php echo $message['readed']?></small></p> 
          </div> 
         </div> 
        </a> 

        <?php 
       } 

       if($inboxresult && count($inboxresult) > 0){ 
        echo "<center>"; 
        echo "<h4>Page $inboxnumber/$inboxpages</h4>"; 
        echo "<h4>Messages : $inboxtotal</h4>"; 

        # first page 
        if($inboxnumber <= 1) 
         echo "<span>&laquo; prev</span> | <a href=\"?inboxpage=$inboxnext\">next &raquo;</a>"; 
        # last page 
        elseif($inboxnumber >= $inboxpages) 
         echo "<a href=\"?inboxpage=$inboxprev\">&laquo; prev</a> | <span>next &raquo;</span>"; 
        # in range 
        else 
         echo "<a href=\"?inboxpage=$inboxprev\">&laquo; prev</a> | <a href=\"?inboxpage=$inboxnext\">next &raquo;</a>"; 
         echo "</center>"; 
        }else{ 
         echo "<p>No results found.</p>"; 
        } 

       return $inboxresult; 
      } 
     } 

    } 
    catch(PDOException $e) 
    { 
     echo $e->getMessage(); 
    } 
} 
+1

如果你無法解釋這個問題,你一定會永遠不會找到一個解決方案。你已經在你的問題上付出了一些努力,但目前還不清楚你想達到什麼目的。我偶然發現:「想從user_pm_hide中選擇數據」,但是你永遠不會說你想選擇什麼。請閱讀[如何創建最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)。 –

+0

@KIKOSoftware我想選擇隱藏的信息。 – Crelix

+0

嘗試創建一個'SELECT - JOIN'條件。這確實有好處,即數據庫將創建關於這兩個表的「視圖」,然後選擇數據。更多的效率和更少的編碼; P – SacrumDeus

回答

1

使用SQL NOT IN

SELECT * 
FROM table1 
WHERE id NOT IN 
    (
    SELECT id 
    FROM table2 
) 
+0

這種解決方案的工作原理,但它沒有那麼快作爲連接條件。連接條件在連接的表上創建一個視圖。嵌套選擇導致需要更多時間的數據庫訪問 – SacrumDeus

+0

嘗試使用:'SELECT * FROM table1 JOIN table2 ON條件JOIN table3 ON條件WHERE條件' – SacrumDeus

+0

@SacrumDeus我不太瞭解這些JOIN方法。嘗試過這樣的事情,但它不會隱藏任何內容並顯示所有記錄兩次。 – Crelix