ipi bk
ipi bk

Reputation: 60

how to looping one data in laravel

i have page for barcode print by id product

like this :

https://i.sstatic.net/LekwH.jpg

i want page show 20 barcode like picture bellow

https://i.sstatic.net/SgAle.jpg

I only have 1 barcode data but I want to display as many as 20 barcodes on the page ... so, when I print a barcode, I can print as many as 20 barcodes, not just one

my controller :

public function barcodes($id){
    $title = $this->title; 

    $products = Product::find($id);
    $vars = compact('products');

    $data = ['products' => $products];
    return view($title.'.barcodes',compact('data','products'));
}

blade view:

<div class="row">
   <div class="col-12">
      <div class="card-box">
         <div class="header-title"> 
            <a href="#" class="btn btn-info btn-sm" onclick="printDiv('printableArea')" >
            <i class="fa fa-print"></i>
            Print
            </a>
         </div>
         <div class="panel-body" id="printableArea">
            <div class="col-md-2" style="padding: 10px; border: 1px solid #adadad;display:inline-block;line-height:16px !important; " align="center">
               <p>{{$products->name}}</p>
               <?php echo '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG($products->code, "c128A",1,40,array(1,1,1), true) . '"   />'; ?>
               <br>
               <small style="font-size: 8px !important;"><b>{{$products->code}}</b></small>
               <p style="line-height: 12px !important; font-size: 8px !important;">
                  <b>Price: {{$products->sale_price}} </b>
               </p>
            </div>
         </div>
      </div>
   </div>
</div>
<script>
   function printDiv(divName) {
       var printContents = document.getElementById(divName).innerHTML;
       var originalContents = document.body.innerHTML;
       document.body.innerHTML = printContents;
       window.print();
       document.body.innerHTML = originalContents;
   }
</script>

Upvotes: 0

Views: 478

Answers (3)

Christhofer Natalius
Christhofer Natalius

Reputation: 3388

I only have 1 barcode data but I want to display as many as 20 barcodes on the page ... so, when I print a barcode, I can print as many as 20 barcodes, not just one

Use @for

@for ($i = 0; $i < 20; $i++)
    // put HTML for barcode here
@endfor

Upvotes: 0

schutte
schutte

Reputation: 2166

One way you can use is foreach loop.

In your case you can do something like this.

@foreach($products AS $product)
    <div class="row">
       <div class="col-12">
          <div class="card-box">
             <div class="header-title"> 
                <a href="#" class="btn btn-info btn-sm" onclick="printDiv('printableArea')" >
                <i class="fa fa-print"></i>
                Print
                </a>
             </div>
             <div class="panel-body" id="printableArea">
                <div class="col-md-2" style="padding: 10px; border: 1px solid #adadad;display:inline-block;line-height:16px !important; " align="center">
                   <p>{{$product->name}}</p>
                   <?php echo '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG($product->code, "c128A",1,40,array(1,1,1), true) . '"   />'; ?>
                   <br>
                   <small style="font-size: 8px !important;"><b>{{$product->code}}</b></small>
                   <p style="line-height: 12px !important; font-size: 8px !important;">
                      <b>Price: {{$product->sale_price}} </b>
                   </p>
                </div>
             </div>
          </div>
       </div>
    </div>
@endforeach


Foreach construct provides an easy way to iterate over arrays.

Upvotes: 1

Taylor Goodall AU
Taylor Goodall AU

Reputation: 214

This will loop over each of the $products

 @foreach ($products as $product)
              <div class="card-box">
             <div class="header-title"> 
                <a href="#" class="btn btn-info btn-sm" onclick="printDiv('printableArea')" >
                <i class="fa fa-print"></i>
                Print
                </a>
             </div>
             <div class="panel-body" id="printableArea">
                <div class="col-md-2" style="padding: 10px; border: 1px solid #adadad;display:inline-block;line-height:16px !important; " align="center">
                   <p>{{$products->name}}</p>
                   <?php echo '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG($products->code, "c128A",1,40,array(1,1,1), true) . '"   />'; ?>
                   <br>
                   <small style="font-size: 8px !important;"><b>{{$products->code}}</b></small>
                   <p style="line-height: 12px !important; font-size: 8px !important;">
                      <b>Price: {{$products->sale_price}} </b>
                   </p>
                </div>
             </div>
          </div>
       </div>
    @endforeach

https://laravel.com/docs/5.8/blade#loops

Upvotes: 1

Related Questions