Закончите новичка здесь, пытаясь отредактировать мой первый PHP-код, и буду благодарен за любую помощь.
я использую этот плагин для отображения ярлыков питания на моем сайте WordPress. С разрешения автора я пытаюсь обновить отображение стандартного вертикального формата до горизонтальный формат.
Я не могу понять, как создать необходимую таблицу из 4 столбцов в PHP. Я гуглил последние пару часов, но все, что я нашел, касалось вызова элементов базы данных для отображения в виде таблицы. Надеюсь, это супер простой, и я просто не правильно искать!
Вот код, который я редактирую из исходных файлов плагинов; Я отметил, где я хочу разрывы столбцов.
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;
}
Конечно, я также открыт для любого не табличного способа, чтобы он тоже выглядел как горизонтальный формат!
Любая помощь / руководство искренне приветствуется.
Я думаю, что вы можете заключить каждый раздел в столбец div, добавив
$rtn .= "<div class='column' style='width:25%;float:left;'>\n";
к началу и
$rtn .= "</div>\n";
до конца разрыва столбца. Это место для начала.
Других решений пока нет …