2013-08-27 33 views
0

在我的wp小部件中,$instance具有一個名爲order的屬性,它將列出庫/服務的列表順序。我想存儲一個多維關聯數組,它將保存每個位置/服務的id,其類型(即,如果它是位置或服務),並且它是名稱。我想它是這個樣子:

array(
    [0] => ('id' => 1, 'type' => 'location', 'name' => 'University Library'), 
    [1] => ('id' => 7, 'type' => 'service', 'name' => 'Circulation Desk')); 

下面是爲WP插件管理窗格中的HTML標記和$instance['order']

<label> 
    <?php _e('Select ordering', 'olh'); ?>: 
</label> 

<select 
    name='select-hours-order' 
    class='select-hours-order'> 
    <!-- dynamically populated by jQuery --> 
</select> 

<a 
    class='add-location-service'> 
    <i>+</i> 
</a> 

<ul 
    name='<?php echo $this->get_field_name('order') ?>[]' 
    id='<?php echo $this->get_field_id('order') ?>' 
    class='location-service-order' > 

    <?php 
    foreach ($instance['order'] as $order) : ?> 

    <li 
     class="<?php echo $order['class'] ?>" 
     value="<?php echo $order['value'] ?>"> 
     <?php echo $order['name'] ?> 
    </li> 
    <?php endforeach; ?> 

</ul> 

用戶可以選擇什麼樣的位置/服務/他想要通過多選下拉菜單進行顯示。每當用戶選擇一個庫/服務時,它會自動填充select.select-hours-order。然後用戶可以點擊a.add-location-service+按鈕將其添加到ul.location-service-order

從那裏,我想保存liul.location-service-order與我上面指定的屬性。

謝謝gals/guys爲任何和所有信息。

回答

2

我最終使用一種解決方法來解決這個問題。我不是像上面描述的那樣嘗試存儲多維數組,而是存儲通過將類型與id相結合而創建的簡單數組值。

因此,而不是:

array(
    [0] => ('id' => 1, 'type' => 'location', 'name' => 'University Library'), 
    [1] => ('id' => 7, 'type' => 'service', 'name' => 'Circulation Desk')); 

的排列成爲

array('L1', 'S7'); 

列表項,我嵌套列表項中,將有名字命名一個隱藏輸入和我想存儲的值如下:

<?php 
    foreach($instance['order'] as $order) : 
     if($order) : ?> 

     <li value='<?php echo $order ?>'> 
      <span> Library name. </span> 
      <input 
      type='hidden' 
      name='<?php echo $this->get_field_name('order') ?>[]' 
      value='<?php echo $order ?>' 
      /> 

      <a class='destroy_parent'>X</a> 
     </li> 

    <?php 
    endif; 
    endforeach; ?> 

注意input的名字和它的價值。名稱末尾的[]表示將存儲值數組。

+0

在這個愚蠢的日子裏度過了漫長的一天之後,我非常感謝你的這個輸入! – ricricucit