Могу ли я использовать меньше строк isset для циклического перебора ключей массива?

Есть ли способ «СУХОЙ» (не повторять себя) этот код, или мне нужны все эти строки «isset»? У меня есть много предметов для обработки, как этот, только если они выбраны. Я использую FPDF. Я действительно хотел бы сократить код процессора, если это возможно! 🙂

function Item3l() {
if (isset($_POST["item3lQty"][0]) && !empty($_POST["item3lQty"][0]) ||
isset($_POST["item3lQty"][1]) && !empty($_POST["item3lQty"][1]) ||
isset($_POST["item3lQty"][2]) && !empty($_POST["item3lQty"][2]) ||
isset($_POST["item3lQty"][3]) && !empty($_POST["item3lQty"][3]) ||
isset($_POST["item3lQty"][4]) && !empty($_POST["item3lQty"][4]) ||
isset($_POST["item3lQty"][5]) && !empty($_POST["item3lQty"][5])) {
$item3lQty = $_POST['item3lQty'];
$itm3l     = $_POST['itm3l'];
$des3l     = $_POST['des3l'];
$clr3l     = $_POST['clr3l'];
$this->SetFont('Arial', '', 10);
$this->SetFillColor(242);
$this->SetLineWidth(1);
$this->SetX(33);
$this->Cell(97, 20, $itm3l, 'LRB', 0, 'L');
$this->Cell(300, 20, $des3l, 'LRB', 0, 'L');
$this->Cell(95, 20, $clr3l, 'LRB', 0, 'L');
$this->Cell(28, 20, $item3lQty[0], 'LRB', 0, 'C');
$this->Cell(28, 20, $item3lQty[1], 'LRB', 0, 'C');
$this->Cell(28, 20, $item3lQty[2], 'LRB', 0, 'C');
$this->Cell(28, 20, $item3lQty[3], 'LRB', 0, 'C');
$this->Cell(28, 20, $item3lQty[4], 'LRB', 0, 'C');
$this->Cell(28, 20, $item3lQty[5], 'LRB', 0, 'C');
$this->Cell(28, 20, '', 'LRB', 0, 'C');
$this->Cell(38, 20, array_sum($item3lQty), 'LRB', 1, 'C');
}

}

<fieldset class="item-fieldset" form="itemsForm">
<legend><?php echo $itm3l ?></legend>

<div class="item_image_container">
<a href="<?php echo $img3l_full_lnk ?>" target="_blank">
<img class="item_image" src="<?php echo $img3l_82px_lnk ?>" /></a>
<span class="click_full_image">Click for full size</span>
</div><!-- ITEM_IMAGE div CLOSE -->

<table class="inputs" border="1" cellspacing="1">
<tbody>
<tr class="gridaddrows">
<td colspan="8" class="radius">
<div class="itemdesc"><?php echo $des3l ?></div>
</td>
</tr>
<tr class="gridrows">
<td class="gridtitle">XS</td>
<td class="gridtitle">SM</td>
<td class="gridtitle">MD</td>
<td class="gridtitle">LG</td>
<td class="gridtitle"></td>
<td class="gridtitle"></td>
<td class="gridtitle">Total Pcs</td>
</tr>
<tr>
<input type="hidden" name="itm3l" value='<?php echo $itm3l ?>'>
<input type="hidden" name="des3l" value='<?php echo $des3l ?>'>
<input type="hidden" name="clr3l" value='<?php echo $clr3l ?>'>
<td><input type="number" class="item3l" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off"><br>
<span class="price"><?php echo $price3l ?></span></td>

<td><input type="number" class="item3l" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off"><br>
<span class="price"><?php echo $price3l ?></span></td>

<td><input type="number" class="item3l" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off"><br>
<span class="price"><?php echo $price3l ?></span></td>

<td><input type="number" class="item3l" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off"><br>
<span class="price"><?php echo $price3l ?></span></td>

<td><input type="number" class="item3l" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off" style="visibility: hidden"><br>
<span class="price" style="visibility: hidden"><?php echo $price3l ?></span></td>

<td><input type="number" class="item3l" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off" style="visibility: hidden"><br>
<span class="price" style="visibility: hidden"><?php echo $price3l_xx ?></span></td>

<td><input type="number" class="item3ltotal" value="" readonly/></td>
</tr>
</tbody>
</table>
</fieldset>

Я бы, возможно, еще десятки предметов …

0

Решение

Да, это называется ПЕТЛИ.

function existQty(){
$flag = false;
for ($i = 0 ; $i<6 ; $i++){
if ( isset($_POST["item3lQty"][$i]) && !empty($_POST["item3lQty"][$i]) ){
flag = true;
break;
}
}
return flag;
}

function Item3l() {
if (existQty()){
$item3lQty = $_POST['item3lQty'];
$itm3l     = $_POST['itm3l'];
$des3l     = $_POST['des3l'];
$clr3l     = $_POST['clr3l'];
$this->SetFont('Arial', '', 10);
$this->SetFillColor(242);
$this->SetLineWidth(1);
$this->SetX(33);
$this->Cell(97, 20, $itm3l, 'LRB', 0, 'L');
$this->Cell(300, 20, $des3l, 'LRB', 0, 'L');
$this->Cell(95, 20, $clr3l, 'LRB', 0, 'L');
for ($i = 0 ; $i < 6 ; $i++){
$this->Cell(28, 20, $item3lQty[$i], 'LRB', 0, 'C');
}
$this->Cell(28, 20, '', 'LRB', 0, 'C');
$this->Cell(38, 20, array_sum($item3lQty), 'LRB', 1, 'C');
}
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector