0

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>
3
  • I don't see any loop anywhere in your blade file? Do you have 20 barcode data in database? Commented Jul 30, 2019 at 23:19
  • no, 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 Commented Jul 31, 2019 at 2:55
  • You must have atleast 20 barcode so that you can print 20? Loop is essential for repeated task. Please share screenshot of your database table with data. Commented Jul 31, 2019 at 6:28

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.