2017-07-28 63 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

使用它来创建订单元素,通过爆炸或任何,然后你可以选择正确的顺序,没有大惊小怪 –