【问题标题】:Wordpress how to display user_meta array as a table?Wordpress 如何将 user_meta 数组显示为表格?
【发布时间】:2021-11-05 03:43:58
【问题描述】:

我已尝试在 Wordpress stackoverflow 上发布此内容,但没有得到任何答案。

这是 PHP + Wordpress 的问题,所以也许这里有人可以帮忙。

好的,所以我正在使用user_meta 数组为用户构建一个“事务”表。

我是新手,正在努力解决问题。

基本上,实际上我知道如何将数组存储到user_meta

$the_meta_array = array (
    'type' => 'value-1',
    'amount' => 'value-2',
    'date' => 'value-3',
    'method' => 'value-4',
    'status' => 'value-5',
);  
$user_id = 12345;  
$user_meta_key = 'transactions';  
add_user_meta( $user_id, $user_meta_key, $the_meta_array );

我的问题是如何以如下表格格式为特定用户显示“交易”meta_key

-----------------------------------------------------
type    | amount  | date          | method | status
-----------------------------------------------------
deposit | $15     | 2021,09,18    | Paypal | done
-----------------------------------------------------
deposit | $35     | 2021,09,14    | Paypal | done
-----------------------------------------------------
cashout | $25     | 2021,09,11    | Paypal | done

是否可以将meta_key 数组显示为循环?

不知道如何实际做到这一点。

我知道这应该是可能的。

任何帮助将不胜感激。

【问题讨论】:

    标签: php arrays wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:

    1- 使用get_users 函数获取所有用户:

    $args = array(
      'fields' => array('ID')
    );
    $users = get_users($args);
    

    2- 为有交易数据的用户创建一个空数组:

    $users_with_transaction = array();
    

    3- 遍历所有用户并挑选出具有交易数据的用户,然后使用 array_push 填充 $users_with_transaction 数组:

    foreach ($users as $user) {
      $users_transaction = (get_user_meta($user->ID, 'transactions'));
      if ($users_transaction) {
        array_push($users_with_transaction, $users_transaction);
      }
    }
    

    4- 创建表结构并通过循环 $users_with_transaction 数组来填充它:

    <table>
      <thead>
        <tr>
          <td>type</td>
          <td>amount</td>
          <td>date</td>
          <td>method</td>
          <td>status</td>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($users_with_transaction as $user_data) { ?>
          <tr>
            <?php foreach ($user_data[0] as $data_key => $data_value) { ?>
              <td><?php echo $data_value; ?></td>
            <?php } ?>
          </tr>
        <?php } ?>
      </tbody>
    </table>
    

    哪个会输出这个:

    然后您可以使用css 来设置您的表格样式。

    这是一个带有内联 css 的表格结构示例,它为您的表格行提供边框:

    <table style='border-collapse:collapse'>
      <thead>
        <tr>
          <td style="border: 1px solid black;">type</td>
          <td style="border: 1px solid black;">amount</td>
          <td style="border: 1px solid black;">date</td>
          <td style="border: 1px solid black;">method</td>
          <td style="border: 1px solid black;">status</td>
        </tr>
      </thead>
      <tbody>
        <?php
        foreach ($users_with_transaction as $user_data) { ?>
          <tr>
            <?php
            foreach ($user_data[0] as $data_key => $data_value) {
            ?>
    
              <td style="border: 1px solid black;"><?php echo $data_value; ?></td>
            <?php
            } ?>
          </tr>
        <?php
        }
        ?>
      </tbody>
    </table>
    

    哪个会输出这个:

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 2017-03-08
      • 2021-03-22
      • 2013-11-14
      • 2017-12-01
      • 2020-03-06
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多