2017-07-28 60 views
1

我試圖顯示wishlist,woocommerce插件中的所有產品。另外,我希望讓願望清單能夠排序,因此我將它連接到數據庫,並且如果具有給定唯一ID的行不在那裏,它將按類別無組織顯示所有產品。如何結合foreach內部的數組結果

如果有一個唯一的ID,那麼它應該顯示所有項目。現在它顯示$行,如下:

Array ( 
    [0] => 237 
    [1] => 243 
    [2] => 266 
) 
Array ( 
    [0] => 237 
    [1] => 243 
    [2] => 266 
) 
Array ( 
    [0] => 237 
    [1] => 243 
    [2] => 266 
) 

由於3款產品在同一類別時,它一次顯示他們全部3。

 <?php 
     // db conn 
     $mysqli = new mysqli(
      "localhost", 
      "root", 
      "root", 
      "nest" 
     ); 
     // new wishist 
     $wishlist = new WC_Wishlists_Wishlist($_GET['wlid']); 
     // get products from wishlist 
     $wishlist_items = WC_Wishlists_Wishlist_Item_Collection::get_items($wishlist->id, true); 
     // counter - useless at the moment 
     $counter = 0; 
     // loop through all products 
     foreach ($wishlist_items as $wishlist_item_key => $item) { 
      $counter++; 
      // get product id 
      $product_id = apply_filters('woocommerce_cart_item_product_id', $item['product_id'], $item, $wishlist_item_key); 
      // get categories 
      $cats = get_the_terms($product_id, 'product_cat'); 

      // loop categories 
      foreach ($cats as $cat) { 
       // get slug 
       $product_cat = $cat->slug; 
      } 
      // wishlist id is set in the database as a primary key - unique. 
      $wlid_cats = $product_cat . $wishlist->id; 

      // query with results 
      $result = $mysqli->query(" 
       SELECT list 
       FROM sortable 
       WHERE wlid_cats = '$wlid_cats' " 
      ); 

      // results parsed into $row 
      $row = $result->fetch_array(MYSQLI_NUM); 

      if (isset($row)) { 
       // change string to array 
       $row = implode(' ', $row); 
       // split array item[0] into diff items. 
       $row = preg_split("/[\s,]+/", $row); 
      } 
      // get product data 
      $_product = wc_get_product($item['data']); 
      if ($_product->exists() && $item['quantity'] > 0) { 
       $product_permalink = apply_filters('woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink($item) : '', $item, $wishlist_item_key); 
       // $cate is a function parameter which asks for the category to be displayed 
       if ($product_cat == $cate && empty($row)) { 
       ?> 
         <!--Saved for Jquery reasons - POST to db--> 
         <p style="display: none" id="<?= $wishlist->id ?>" class="<?= $wishlist->id ?> "><?= $cate ?></p> 
         <li class="product-thumbnail" type="<?= $cate ?>" id="product-<?= $product_id ?>"> 
          <?php 
          // gets thumbnail and displays it 
          $thumbnail = apply_filters('woocommerce_cart_item_thumbnail', $_product->get_image(), $item, $wishlist_item_key); 
          echo $wlid_cats; 
          if (!$product_permalink) { 
           echo $thumbnail; 
          } else { 
           printf('<a href="%s">%s</a>', esc_url($product_permalink), $thumbnail); 
          } 
          ?> 
         </li> 
         <?php 
       } elseif (!empty($row)) { 

        // where I'm stuck 
        print_r($row); 

       } 

      } 
     } ?> 

我怎樣才能合併輸出到只有一個數組而不是3?

+0

use array_merge(); –

+0

它仍然會將它們全部顯示在一起。 –

+0

$ result = array_unique($ row); –

回答

1

讓新的全球陣列

$narray = array(); 

,之後在你的病情

elseif (!empty($row)) { 
      // where I'm stuck 
    $narray[] = $row; // add the array 
} 
$result = array_map("unserialize", array_unique(array_map("serialize", $narray))); 
print_r($result); 
+0

得到它的工作。但是,它不在foreach循環中,我無法獲得$ product_cat的結果。 –

+0

請再試一次,我已編輯最後一行 –

+0

出錯了。只能翻轉STRING和INTEGER的值 –

0

我想在這裏工作的問題。 應該不是這個查詢

$result = $mysqli->query(" 
       SELECT list 
       FROM sortable 
       WHERE wlid_cats = '$wlid_cats' " 
       ); 

有類別列表中不只是一個類別,然後如果有重複它會按出來。

只是重新閱讀你的問題,並試圖弄清楚這一切。

因此,在第12行中,您已經獲得了需要通過'可排序'表對它們進行排序的項目ID?

所以一個查詢(如果這只是正常的SQL)會是這樣的。

//爲訂單

$order = "itemId=3 DESC,itemId=1 DESC,itemId=5 DESC,itemId=6 DESC,category DESC"; 

    $query = "SELECT * FROM items WHERE itemID IN (myItems) ORDER BY ".$order; 

itemIds也許我在這裏忽略了一點,但SQL是健全的。

+0

不太清楚如何實現您的答案。 –

+0

所以列表的順序是在$ row [list]元素中? –

+0

使用它來創建訂單元素,通過爆炸或任何,然後你可以選擇正確的順序,沒有大驚小怪 –