2017-07-28 9 views
1

私はウィッシュリストからすべての製品を表示しようとしています、woocommerceプラグイン。また、ウィッシュリストをソート可能にしてDBに接続したいので、指定された一意の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つではなく1つの配列にする方法はありますか?

+0

使用array_merge(); –

+0

それでもそれらはすべて一緒に表示されます。 –

+0

$結果= array_unique($行); –

答えて

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

うまくいきました。しかし、$ product_catの結果を得ることができないforeachループの外にあります。 –

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

ため

//アイテムIDは、たぶん私は、ここでのポイントを逃しましたが、SQLは音です。

+0

あなたの答えを実装する方法がわかりません。 –

+0

リストの順序は$ row [list]要素ですか? –

+0

これを使用して注文要素を作成するには、爆発などの方法を使用してください。正しい順序で項目を選択できます。歓声はありません –

関連する問題