来自带有多个参数的短码的超文本标记语言表格

时间:2017-12-07 作者:D. Bleumink

我已经解决了这个问题,但我无法解决它。我在所有的数学都在表格后面走了很长的一段路,而表格是由于短代码后面的函数而产生的。然后,我还通过使用分配给短代码的属性来创建表变量。但完成我的功能的能力是将多个输入传递到同一个短代码,这些输入形成一个漂亮的表。我试过使用几个短代码,但最好是所有内容都可以放在一个短代码中。现在可能有点模糊,让我通过提供一些代码来澄清一下。

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;
}
正如你所看到的,这是一个相当大的函数,需要一些时间来理解它。现在,我看到了this post, 但正如你所看到的,我每行都有多个数据输入,我只是不知道如何按照我现在所做的方式来组合它们。

有人知道吗?你会帮我的!

亲切的问候

1 个回复
SO网友:Drupalizeme

我创建了一个示例,说明如何实现您的目标:

您可以这样使用它:

[itable data=“库存;硬币;硬币金额;货币#库存;硬币;硬币金额;货币”]

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\');

结束