2

I've struck this problem, and I just can't solve it. I've come a long way with all the maths behind the table and the table being put out as a result of a function behind the shortcode. Then I also managed to make the table variable by using the attributes assigned to a shortcode. But what would complete my function is the ability to pass several inputs to the same shortcode and those inputs forming a nice table. I've tried using several shortcodes, but better would be if everything could be fit into one shortcode. It's probably a little vague now, let me clear things up by supplying some code.

function test( $atts ) {

// Attributes
$atts = shortcode_atts(
    array(
        'investment' => '1',
        'coinamount' => '0.5',
        'coin' => 'BTC',
        'currency' => 'EUR',
    ),
    $atts,
    'investment'
);

//Declaring all my variables
$coinworth = get_coin_worth($atts['coin'], $atts['currency']);
$currencyworth = get_currency_worth($atts['coin'], $atts['currency']);
$coinname = get_coin_name($atts['coin']);
$currencyname = get_currency_name($atts['currency']);
$investment = $atts['investment'];
$coinamount = $atts['coinamount'];
$portfolioworth = $coinamount*$coinworth;
$result = $portfolioworth - $investment;
$result = number_format((float)$result, 2, '.', '');

//Creating the table 
$table = <<<EOD
        <h1 style="text-align:center">Uw Portfolio</h1>
        <table style="width:100%">
            <tr>
                <th>Invested Currency</th>
                <th>Investment Worth</th>
                <th>Coin amount</th>
                <th>Recent Portfolioworth</th>
                <th>Result</th>
            </tr>
            <tr>
                <td>$currencyname</td>
                <td>$investment</td>
                <td>$coinamount</td>
                <td>$coinworth</td>
                <td $style$result</td>
            </tr>
        </table>
EOD; 
return $table;
}

As you can see, it's a pretty big function, take some time to understand it. Now, I've seen this post, but as you see I have multiple data inputs for every row and I just can't grasp how to combine them all the way i'm doing right now.

Anyone any idea? You will be helping me loads!

Kind regards

1

I have created an example of how you can achive what you are after:

You can use it like this :

[itable data="inv;coin;coinamount;currency#inv;coin;coinamount;currency"]

function itable_shortcode( $atts ) {
    extract( shortcode_atts( array(
        'data' => 'none',
    ), $atts ) );

    $data = explode('#',$data);

    $output = "";

    foreach ($data as $value) {
        $output .= '<tr>';
        $in_value = explode(';',$value);

        // next step is check if exist or is the type you want like integer or string
        $investment  = $in_value[0];
        $coin        = $in_value[1];
        $coinamount  = $in_value[2];
        $currency    = $in_value[3];

        //Declaring all my variables
        $coinworth      = get_coin_worth($coin, $currency);
        $currencyworth  = get_currency_worth($coin,$currency);
        $coinname       = get_coin_name($coin);
        $currencyname   = get_currency_name($currency);


        $portfolioworth = $coinamount*$coinworth;
        $result = $portfolioworth - $investment;
        $result = number_format((float)$result, 2, '.', '');

        $output .= '<td>'.$currencyname.'</td>';
        $output .= '<td>'.$investment.'</td>';
        $output .= '<td>'.$coinamount.'</td>';
        $output .= '<td>'.$coinworth.'</td>';
        $output .= '<td>'.$result.'</td>';
        $output .= '</tr>';
    }
//Creating the table 
$table = <<<EOD
        <h1 style="text-align:center">Uw Portfolio</h1>
        <table style="width:100%">
            <tr>
                <th>Invested Currency</th>
                <th>Investment Worth</th>
                <th>Coin amount</th>
                <th>Recent Portfolioworth</th>
                <th>Result</th>
            </tr>
            $output
        </table>
EOD;
    return $table;

}

function itable_shortcodes_init() {
    add_shortcode('itable', 'itable_shortcode');
}

add_action('init', 'itable_shortcodes_init');
  • Beautiful! Works like a charm. Thank you so much. This was just the thing I couldn't get done :) – D. Bleumink Dec 7 '17 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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