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