0

I have an array structure from custom fields inside custom post type like the following:


Array
(
    [webw_employee_projects] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_current_projects] => Array
        (
            [January] => 20
            [February] => 50
            [March] => 20
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_min_target] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_max_target] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_target] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_target_bonus] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_max_hours] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_vacation_hours] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_hours] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_hours_bonus] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_karma_points] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_extra_payment] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_salary] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

)

The goal here to create tables for every user like this to be displayed every month as evaluation for him, so there'd be a table for every month in the user page, how this can be sorted given the array structure above?

January [table of january data]

February [table of February data]

March [table of March data]

etc.. until December.

Thanks again.

1
  • 1
    Looks like general PHP question.
    – Max Yudin
    Commented Jan 23, 2022 at 9:04

1 Answer 1

0

This question is better suited at https://stackoverflow.com as Max suggested, but for a quick and dirty solution:

Solution (rough)

Adjust the code to suit your specifications but this should give you the basic structure you need.

<?php

// assume $meta_data holds your array (e.g. get_post_meta($post_id, 'some_key', true);
// for some sample data I made an array here: https://pastebin.com/8qgabnAM

$headings = ['<th class="empty"></th>'];

$rows = [];

foreach ($meta_data  as $key => $months) {

    $heading = str_replace('webw_employee_', '', $key);
    $heading = implode( ' ', explode('_', $heading) );
    $heading = ucwords( $heading );

    $headings[] = "<th>{$heading}</th>";

    foreach ($months  as $month => $value) {
        $rows[$month][$key] = "<td>{$value}</td>";
    }  
}  

foreach ($rows  as $month => $row) {

    $heading = "<th>{$month}</th>";
    $cells   = implode('', $row);

    $rows[$month] = "<tr>{$heading}{$cells}</tr>";

} 

$rows = implode( '', $rows );
$headings = implode( '', $headings );
$headings = "<tr>{$headings}</tr>";
?>

<table>
    <tbody>
    <?php echo $headings ?>
    <?php echo $rows ?>
    </tbody>
</table>

Result:

Note sample data is random, obviously will be different for your dataset, this was just to show how the table populates.

You can easily adjust this to break each month into it's own table if you require.

enter image description here

Result #2: (with computed meta_keys)

Regarding comment:

I need to add some values to the dataset, but whatever I do it gives me 0, let's say I want to add the salary + extra payment it gives me 0 even if I cast it to float or int, what do you think the issue? ibb.co/C2tt6QV

Please refer to GIST HERE

enter image description here

4
  • 1
    Thanks, Adam, appreciate it, I just have one more problem, I need to add some values to the dataset, but whatever I do it gives me 0, let's say I want to add the salary + extra payment it gives me 0 even if I cast it to float or int, what do you think the issue? ibb.co/C2tt6QV Commented Jan 24, 2022 at 0:48
  • @WalledMahmoudSoliman no proble... When you say "add" do you mean arithmetic or add column to table? I see you have three extra meta keys now, so is that you mean you what to add the value of webw_employee_extra_payment + webw_employee_salary and insert that figure on the total_salary key?
    – Adam
    Commented Jan 24, 2022 at 6:52
  • @WalledMahmoudSoliman please refer to gist for example code to handle computed keys.
    – Adam
    Commented Jan 24, 2022 at 9:29
  • Thank you so much, I really appreciate it, if I may ask you another Q please, now I have every employee as a post inside the custom post type 'webw-employees' I need to protect every post with a password to prevent an employee to see another one stats, how's that can be done ? thanks in advance. Commented Jan 24, 2022 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.