php - Can I use less isset lines to loop through array keys?
114
Is there a way to "DRY" (don't repeat yourself) this code, or do I need all these "isset" lines? I have lots of items to process just like this one, only IF they are selected. I am using FPDF. I would really like to shorten the processor code if possible! :)
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 form="itemsForm">
<legend><?php echo $itm3l ?></legend>
<div >
<a href="<?php echo $img3l_full_lnk ?>" target="_blank">
<img src="<?php echo $img3l_82px_lnk ?>" /></a>
<span >Click for full size</span>
</div><!-- ITEM_IMAGE div CLOSE -->
<table border="1" cellspacing="1">
<tbody>
<tr >
<td colspan="8" >
<div ><?php echo $des3l ?></div>
</td>
</tr>
<tr >
<td >XS</td>
<td >SM</td>
<td >MD</td>
<td >LG</td>
<td ></td>
<td ></td>
<td >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" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off"><br>
<span ><?php echo $price3l ?></span></td>
<td><input type="number" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off"><br>
<span ><?php echo $price3l ?></span></td>
<td><input type="number" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off"><br>
<span ><?php echo $price3l ?></span></td>
<td><input type="number" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off"><br>
<span ><?php echo $price3l ?></span></td>
<td><input type="number" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off" ><br>
<span ><?php echo $price3l ?></span></td>
<td><input type="number" min="0" max="288" name="item3lQty[]" placeholder="Qty" autocomplete="off" ><br>
<span ><?php echo $price3l_xx ?></span></td>
<td><input type="number" value="" readonly/></td>
</tr>
</tbody>
</table>
</fieldset>
I would have possibly dozens more items...
Answer
Solution:
Yes, it is called LOOPS.