0
我创建了一个循环遍历〜50个字段值的foreach
循环。我使用条件语句只输出我想要的字段。我想以不同方式交替排列样式(我必须使用内联css,因为这是用于HTML电子邮件的)。我假设我可以通过循环迭代使用$i
变量来实现这一点,然后得到奇数/偶数。我得到的问题是这个循环迭代通过~50个字段,因为我不输出一些字段,整数不是按顺序。结果是1,2,3,7,10等。这个结果我无法得到准确的奇数/偶数结果。使用条件语句自动增量PHP foreach循环
下面是代码:
$i = 1;
foreach ($form['fields'] as $field) {
$use_text = false;
$raw_field_value = RGFormsModel::get_lead_field_value($entry, $field);
$field_value = GFCommon::get_lead_field_display($field, $raw_field_value, rgar($entry, 'currency'), $use_text, $format, 'email');
if ($field->type == "product" || $field->type == "hidden" || $field->type == "section" || $field->type == "html") {
continue;
} else if (GFFormsModel::is_field_hidden($form, $field, array(), $entry)) {
// ignore fields hidden by conditional logic
continue;
}
if ($field_value === false) {
continue;
}
if (!empty($field_value) || strlen($field_value) > 0) {
$message .= '<tr style="vertical-align: top; text-align: left; padding: 0;" align="left">';
$message .= '<td style="word-break: break-word; -webkit-hyphens: none; -moz-hyphens: none; hyphens: none; border-collapse: collapse !important; vertical-align: top; text-align: left; color: #252b23; font-family: Helvetica, Arial, sans-serif; font-weight: normal; line-height: 19px; font-size: 14px; border-left-style: solid; border-left-color: #ccc; border-left-width: 1px; border-top-color: #ccc; border-top-width: 1px; border-top-style: solid; margin: 0; padding: 8px;" align="left" valign="top"><strong>' . $field->label . '</strong> ' . $i . '</td>';
$message .= '<td style="word-break: break-word; -webkit-hyphens: none; -moz-hyphens: none; hyphens: none; border-collapse: collapse !important; vertical-align: top; text-align: left; color: #252b23; font-family: Helvetica, Arial, sans-serif; font-weight: normal; line-height: 19px; font-size: 14px; border-left-style: solid; border-left-color: #ccc; border-left-width: 1px; border-top-color: #ccc; border-top-width: 1px; border-top-style: solid; margin: 0; padding: 8px;" align="left" valign="top">' . $field_value . '</td>';
$message .= '</tr>';
}
$i++;
}
是否有改变的整数结果的方式是基于最终的结果是输出?如果只输出5个字段,则整数值应为1,2,3,4,5。
我希望这是有道理的。
为什么不把'$ i ++'放在你的if语句中? – andrewsi
只会增加$ i,您不会忽略字段,这样它将与您期望的一致 – chaitanya
@andrewsi工作!我不知道增量是否受到if语句的影响。我认为这只受整个foreach循环的影响。谢谢! – katart