2014-10-07 64 views
0

我在將它們與已將項目放入其「Locker」中的用戶進行匹配時,按字母順序排序我的數據結果時遇到困難。多個查詢時按字母順序排序?

我有兩個疑問;第一個在數據庫中搜索用戶放置在他們「儲物櫃」中的所有物品,第二個查詢抽取物品的細節並將它們分類到物品所屬品牌的列表中。

我覺得有一個更好的方法來做到這一點,而不是強迫頁面爲每個項目運行一次查詢,但我不確定以最有效的方式寫出mySQL的正確方法。

我認爲解決方法是將所有ID作爲數組,然後以某種方式在第二個查詢中搜索並排序所有關聯品牌。

我目前有:

//$lockerid is pulled earlier in the code based on which locker number is associated with this user 

    // Pull all of the items and their ids that are in this users locker 
    $userlockerquery= mysql_query("SELECT DISTINCT item_id FROM lockers WHERE user_id = '$profile_userid' AND locker_id ='$lockerid' "); 

    while($lockeritems=mysql_fetch_array($userlockerquery)){ 
      $indi_item=$lockeritems[item_id]; 
      $lockeritemdetails = mysql_query("SELECT DISTINCT brand FROM inventory WHERE id = '$indi_item' "); 
      $brands=mysql_fetch_array($lockeritemdetails); 
      $brandname=$brands[brand]; 
      echo '<div>'.$brandname.'</div>'; 
    } 

雖然結果確實顯示了所有的品牌,我的問題似乎是,因爲查詢對每個項目的ID跑一次,就不能有列表結果互相交談,因此不能讓他們按字母順序排列,因爲查詢每個項目都運行一次。

也因此,DISTINCT標誌沒有任何作用,因爲它不匹配任何其他結果。

舉個例子,我的結果將返回的div的ID,而不是品牌的訂單,並重復:

耐克 彪馬 彪馬 匡威

不是

匡威 耐克 彪馬

將ORDER BY標誌添加到第二個查詢沒有幫助,所以我想我會試着在這裏問一些想法。請讓我知道是否需要其他細節!

回答

0

也許嘗試類似這個類。看看它是否會滿足您的需求。沒有嘗試sql查詢很難檢查它,但是如果我已經正確編寫它,它應該可以工作。

class MyLocker 
     { 
      // Protected means that you can't use this variable outside of the functions/class 
      // so you can not use $myLocker->_array; It will throw an error 
      protected $_array; 
      // Construct is basically used as an auto-function. It will execute automatically 
      // when you create a new instance of the class so as soon as you do this: 
      // $myLocker = new MyLocker($_locker); you initiate the __construct 
      // When you label as public, you allow it to be used outside of itself 
      public function __construct($_array) 
       { 
        // When you set this variable, it is now open to use in all 
        // other functions in this class. 
        $this->_array = $_array; 
       } 

      // This is the method that will do everything 
      public function LockerContents() 
       { 
        // Loop through query. Since the $_array was set in the __construct 
        // it is available in this function as $this->_array 
        while($lockeritems = mysql_fetch_array($this->_array)){ 
          // $brand is something we want to use in other functions but not 
          // outside the class so it is set here for use in the Fetch() function 
          $this->brand = $lockeritems['item_id']; 
          // We ant to use our Fetch() function to return our brand 
          $_brand   = $this->Fetch(); 
          // If brand available, set it to an array 
          if(!empty($_brand)) 
           $array[] = $_brand; 
         } 
        if(isset($array)) { 
          // Sort the array 
          asort($array); 
          // Finally, we use the Display() function for the final output 
          $this->Display($array); 
         } 
        else { ?> 
          <div>Locker is empty.</div><?php 
         } 
       } 
      // Establish this as an in-class variable 
      protected $brand; 
      // Establish this as a public function incase we want to use it by itself 
      // To do so you would write $myLocker->Fetch(); outside of the class. 
      // Since you need $brand for this function to work, you would need to turn 
      // $brand from "protected" to "public" and write $myLocker->brand = 'whatever'; 
      // before you run the $myLocker->Fetch(); 
      public function Fetch() 
       { 
        $query = mysql_query("SELECT DISTINCT brand FROM inventory WHERE id = '".$this->brand."'"); 
        $brands = mysql_fetch_array($query); 
        // Return brand 
        return (isset($brands['brand']))? $brands['brand']:""; 
       } 

      protected function Display($array) 
       { 
        if(is_array($array)) { 
          foreach($array as $object) { ?> 
           <div><?php echo $object; ?></div><?php 
           } 
         } 
       } 
     } 

    // You should be using mysqli_ or PDO for your db connections/functions. 
    $_locker = mysql_query("SELECT DISTINCT item_id FROM lockers WHERE user_id = '$profile_userid' AND locker_id ='$lockerid' "); 

    // If there are more than 0 rows, create locker. 
    if(mysql_num_rows($_locker) > 0) { 
      // Create new instance of the locker app 
      $myLocker = new MyLocker($_locker); 
      // Display the results 
      $myLocker->LockerContents(); 
     } 
+0

非常感謝你寫出來;我會嘗試今天實施並回復。 – vsdesign 2014-10-07 16:29:28

+0

你有沒有得到這個工作,或沒有爲你的需要工作? – Rasclatt 2014-10-09 20:35:36

+0

嗨Rasclatt,我還沒有機會實施它;我不熟悉PHP的類方面,所以我打算做一些閱讀,然後研究你的代碼,看看它是如何工作的,然後再與我的其他人一起實施。 – vsdesign 2014-10-10 21:27:06