0

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 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>

I would have possibly dozens more items...

Bigfootbud
  • 103
  • 1
  • 10
  • You don't need `isset` if you are using `!empty`. If it is not empty then it must be set. `That means empty() is essentially the concise equivalent to !isset($var) || $var == false.` -http://php.net/manual/en/function.empty.php – user3783243 May 20 '18 at 18:37
  • You could use a loop to iterate over `item3lQty`. – Blauharley May 20 '18 at 18:39
  • 1
    For improving your working code visit https://codereview.stackexchange.com – LW001 May 20 '18 at 20:58

1 Answers1

1

Yes, it is called LOOPS.

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');
}
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
TheSalamov
  • 877
  • 9
  • 16
  • 1
    `$_POST["item3lQty"][i]` should be `$_POST["item3lQty"][$i]` – ivanivan May 20 '18 at 19:08
  • Thanks! :) But now getting error at the start of the PDF creation code `$pdf = new PDF_receipt();` PHP Parse error: syntax error, unexpected '$pdf' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) – Bigfootbud May 21 '18 at 04:56
  • So why is your error related to this function ? :) try to paste your PDF_receipt class – TheSalamov May 21 '18 at 10:37