Skip to content
Advertisement

WordPress how to display user_meta array as a table?

I have tried posting this on WordPress stackoverflow, but haven’t got any answers.

This is PHP + WordPress question, so maybe someone here could help.

Ok, so I’m building a “Transactions” table for a user using user_meta array.

I’m new to this and trying to figure things out.

Basically, in practice I know how to store array to 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 );

My question is how to display ‘transactions’ meta_key for that specific user in a table format like this:

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

Is it possible to display meta_key array as a loop?

Have no idea how practically do this.

I know it should be possible somehow.

Any help would be very appreciated.

Advertisement

Answer

1- Get all of your users by using get_users function:

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

2- Create an empty array for the users that have transaction data:

$users_with_transaction = array();

3- Loop through all of your users and pick out those with transaction data and then use array_push to populate $users_with_transaction array:

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

4- Create your table structure and populate it by looping through the $users_with_transaction array:

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

Which will output this:

enter image description here

You could then use css to style your table.

This is an example of a table structure with in-line css which gives your table rows borders:

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

Which will output this:

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement