创建空白 PHP Table

Creating Blank PHP Table

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

我用this plugin to display nutrition labels on my WordPress site. With the creator's permission, I'm trying to update how it displays from standard vertical format to horizontal format.

我想不出如何在 PHP 中创建所需的 4 列 table。我用 Google 搜索了过去几个小时,但我发现的所有内容都是关于调用数据库项目以显示为 table。希望这是超级基本的,我只是没有正确搜索!

这是我从原始插件文件编辑的代码;我已经标记了我想要分栏符的位置。

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

当然,我也愿意接受任何非table方式来让它看起来像水平格式!

任何help/guidance真诚的感谢。

我认为您可以通过添加

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

到开头和

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

到分栏结束。这是一个起点。