创建空白PHP表

时间:2017-10-19 18:49:35

标签: php wordpress plugins css-tables

在这里完成新手,尝试编辑我的第一个PHP代码,非常感谢任何帮助。

我使用this plugin在我的WordPress网站上显示营养标签。在创作者的许可下,我尝试更新从标准垂直格式到horizontal format的显示方式。

我不能为我的生活弄清楚如何在PHP中创建所需的4列表。我在过去的几个小时里用谷歌搜索过,但我发现的一切都是关于调用数据库项目显示为表格。希望这是超级基础,我只是没有正确搜索!

这是我从原始插件文件中编辑的代码;我已经标记了我希望列拆分的位置。

function nutr_label_generate($id, $width = 22)
{
global $rda, $nutritional_fields;

$label = get_post_meta($id);

$insufficient = []; //holds insufficient vitamins data

if (!$label) {
    return false;
}

// GET VARIABLES
foreach ($nutritional_fields as $name => $value) {
    $$name = $label['_' . $name][0];
}

// BUILD CALORIES IF WE DON'T HAVE ANY
if ($calories == 0) {
    $calories = (($protein + $carbohydrates) * 4) + ($totalfat * 9);
}

// WIDTH THE LABEL
$style = '';
if ($width != 22) {
    $style = " style='width: " . $width . "em; font-size: " . (($width / 22) * .75) . "em;'";
}


/* First Column */


$rtn = "";
$rtn .= "<div class='wp-nutrition-label' id='wp-nutrition-label-$id' " . ($style ? $style : "") . ">\n";

$rtn .= "   <div class='heading'>" . __('Nutrition Facts', 'nutrition-facts-vitamins') . "</div>\n";


$rtn .= "   <div style='font-size: 18px; padding-top: 3px; padding-bottom: 5px'>" . $servings . " " . __('servings per container',
                'nutrition-facts-vitamins') . "</div>\n";
$rtn .= "   <div style='font-size: 18px; font-weight: 900; padding-top: 3px; padding-bottom: 2px'>" . __('Serving size',
                'nutrition-facts-vitamins') . "</div>\n";
$rtn .= "   <div style='font-size: 18px; font-weight: 900; padding-top: 3px; padding-bottom: 5px'>" . $servingsize . "</div>\n";


$rtn .= "   <hr style='clear: both; height: 5px' />\n";


$rtn .= "   <div class='item_row cf noborder' style='font-size: 2.313em; font-weight: bold'>\n";
$rtn .= "       <span class='f-left' style='font-weight: 900; letter-spacing:0.03em; padding-top: 5px; padding-bottom: 5px'>" . __('Calories',
                'nutrition-facts-vitamins') . "</span>\n";
$rtn .= "       <span class='f-right' style='font-size: 1.500em;font-weight: 900; padding-top: 5px'>" . $calories . "</span>\n";
/* $rtn .= "        <span class='f-right'>" . __( 'Calories from Fat ',
                 'nutrition-facts-vitamins' ) . ( $totalfat * 9 ) . "</span>\n";*/
$rtn .= "   </div>\n";
$rtn .= "   <div class='amount-per small item_row noborder'>" . __('per serving',
                'nutrition-facts-vitamins') . "</div>\n";


/* Second Column */


$rtn .= "   <div class='item_row daily-value small'>\n";
$rtn .= "       <span class='f-left'> " . __('Amount/serving',
                'nutrition-facts-vitamins') . "</span>\n";
$rtn .= "       <span class='f-right'>% " . __('Daily Value',
                'nutrition-facts-vitamins') . "*</span>\n";
$rtn .= "   </div>\n";

$rtn .= "   <div class='item_top_row cf'>\n";
$rtn .= "       <span class='f-left'><strong>" . __('Total Fat',
                'nutrition-facts-vitamins') . "</strong> " . $totalfat . "g</span>\n";
$rtn .= "       <span class='f-right'>" . nutr_percentage($totalfat, $rda['totalfat']) . "%</span>\n";
$rtn .= "   </div>\n";

$rtn .= "   <div class='indent item_row cf'>\n";
$rtn .= "       <span class='f-left'>" . __('Saturated Fat',
                'nutrition-facts-vitamins') . " " . $satfat . "g</span>\n";
$rtn .= "       <span class='f-right'>" . nutr_percentage($satfat, $rda['satfat']) . "%</span>\n";
$rtn .= "   </div>\n";

$rtn .= "   <div class='indent item_row cf'>\n";
$rtn .= "       <span><em>" . __('Trans', 'nutrition-facts-vitamins') . '</em> ' . __('Fat',
                'nutrition-facts-vitamins') . " " . $transfat . "g</span>";
$rtn .= "   </div>\n";

$rtn .= "   <div class='item_row cf'>\n";
$rtn .= "       <span class='f-left'><strong>" . __('Cholesterol',
                'nutrition-facts-vitamins') . "</strong> " . $cholesterol . "mg</span>\n";
$rtn .= "       <span class='f-right'>" . nutr_percentage($cholesterol, $rda['cholesterol']) . "%</span>\n";
$rtn .= "   </div>\n";

$rtn .= "   <div class='item_bottom_row cf'>\n";
$rtn .= "       <span class='f-left'><strong>" . __('Sodium',
                'nutrition-facts-vitamins') . "</strong> " . $sodium . "mg</span>\n";
$rtn .= "       <span class='f-right'>" . nutr_percentage($sodium, $rda['sodium']) . "%</span>\n";
$rtn .= "   </div>\n";


/* Third Column */


$rtn .= "   <div class='item_row daily-value small'>\n";
$rtn .= "       <span class='f-left'> " . __('Amount/serving',
                'nutrition-facts-vitamins') . "</span>\n";
$rtn .= "       <span class='f-right'>% " . __('Daily Value',
                'nutrition-facts-vitamins') . "*</span>\n";
$rtn .= "   </div>\n";  

$rtn .= "   <div class='item_top_row cf'>\n";
$rtn .= "       <span class='f-left'><strong>" . __('Total Carbohydrate',
                'nutrition-facts-vitamins') . "</strong> " . $carbohydrates . "g</span>\n";
$rtn .= "       <span class='f-right'>" . nutr_percentage($carbohydrates, $rda['carbohydrates']) . "%</span>\n";
$rtn .= "   </div>\n";

$rtn .= "   <div class='indent item_row cf'>\n";
$rtn .= "       <span class='f-left'>" . __('Dietary Fiber',
                'nutrition-facts-vitamins') . " " . $fiber . "g</span>\n";
$rtn .= "       <span class='f-right'>" . nutr_percentage($fiber, $rda['fiber']) . "%</span>\n";
$rtn .= "   </div>\n";

$rtn .= "   <div class='indent item_row cf'>\n";
$rtn .= "       <span>" . __('Total Sugars', 'nutrition-facts-vitamins') . " " . $sugars . "g</span>";
$rtn .= "   </div>\n";

$rtn .= "   <div class='double-indent item_row cf'>\n";
$rtn .= "       <span>" . __('Includes',
                'nutrition-facts-vitamins') . " " . $added_sugars . 'g ' . __('Added Sugars') . "</span>";
$rtn .= "       <span class='f-right'>" . nutr_percentage($added_sugars, $rda['sugar']) . "%</span>\n";
$rtn .= "   </div>\n";

$rtn .= "   <div class='item_bottom_row cf'>\n";
$rtn .= "       <span class='f-left'><strong>" . __('Protein',
                'nutrition-facts-vitamins') . "</strong> " . $protein . "g</span>\n";
$rtn .= "       <span class='f-right'></span>\n";
$rtn .= "   </div>\n";


/* Fourth Column */


$rtn .= "   <div class='item_row cf noborder'>\n";
$rtn .= "   <div class='footnote'>" . __('The % Daily Value (DV) tells you how much a nutrient in a serving of 
    food contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.') . "</div>\n";
$rtn .= "   </div>\n";  


/* Bottom Row */


if (isset($vitamin_d) || ($vitamin_d === "0")) {
    $rtn .= "   <div class='item_row noborder cf'>\n";
    $rtn .= "       <span class='f-left'>" . __('Vitamin D',
                    'nutrition-facts-vitamins') . ' ' . $vitamin_d . "mcg</span>\n";
    $rtn .= "       <span class='f-right'>" . nutr_percentage($vitamin_d, $rda['vitamin_d']) . "%</span>\n";
    $rtn .= "   </div>\n";
} else {
    $insufficient[] = 'vitamin D';
}

if ($calcium || ($calcium === "0")) {
    $rtn .= "   <div class='item_row cf'>\n";
    $rtn .= "       <span class='f-left'>" . __('Calcium',
                    'nutrition-facts-vitamins') . ' ' . $calcium . "mg</span>\n";
    $rtn .= "       <span class='f-right'>" . nutr_percentage($calcium, $rda['calcium']) . "%</span>\n";
    $rtn .= "   </div>\n";
} else {
    $insufficient[] = 'calcium';
}

if ($iron || ($iron === "0")) {
    $rtn .= "   <div class='item_row cf'>\n";
    $rtn .= "       <span class='f-left'>" . __('Iron', 'nutrition-facts-vitamins') . ' ' . $iron . "mg</span>\n";
    $rtn .= "       <span class='f-right'>" . nutr_percentage($iron, $rda['iron']) . "%</span>\n";
    $rtn .= "   </div>\n";
} else {
    $insufficient[] = 'iron';
}

if (isset($potassium) || ($potassium === "0")) {
    $rtn .= "   <div class='item_row cf'>\n";
    $rtn .= "       <span class='f-left'>" . __('Potassium',
                    'nutrition-facts-vitamins') . ' ' . $potassium . "mg</span>\n";
    $rtn .= "       <span class='f-right'>" . nutr_percentage($potassium, $rda['potassium']) . "%</span>\n";
    $rtn .= "   </div>\n";
} else {
    $insufficient[] = 'potassium';
}

/*
    * Extra vitamins
 */
if (isset($label['_extra_vitamins']) && !empty($label['_extra_vitamins'])) {
    $extraVitamins = unserialize(current($label['_extra_vitamins']));

    $sufficient = [];
    foreach ($extraVitamins as $key => $vitamin) {
        if ($vitamin || $vitamin === '0') {
            $sufficient[$key] = $vitamin;
        } else {
            $insufficient[] = strtolower($key);
        }
    }

    if (!empty($sufficient)) {
        foreach ($sufficient as $extraLabel => $extraVit) {
            $rtn .= "   <div class='item_row cf'>\n";
            $rtn .= "       <span class='f-left'>" . $extraLabel . "</span>\n";
            $rtn .= "       <span class='f-right'>" . $extraVit . "%</span>\n";
            $rtn .= "   </div>\n";
        }
    }

}
if (!empty($insufficient)) {
    $last = "";
    if (count($insufficient) > 1) {
        $last = array_pop($insufficient);
        $last = ", or " . $last;
    }

    $rtn .= "   <div class='item_row cf'>\n";
    $rtn .= __("Not a significant source of ") . implode(', ', $insufficient);
    $rtn .= $last . ".\n";
    $rtn .= "   </div>";
}
$rtn .= "<hr />";

$rtn .= "</div> <!-- /wp-nutrition-label -->\n\n";

return $rtn;
}

当然,我也可以通过任何非表格的方式让它看起来像横向格式!

真诚感谢任何帮助/指导。

1 个答案:

答案 0 :(得分:0)

我认为您可以通过添加

来为每个部分包含一个列div
$rtn .= "<div class='column' style='width:25%;float:left;'>\n";

到开头

$rtn .= "</div>\n";

到列分隔符的末尾。这是一个开始的地方。