Jump to content

Need to loop thru variables to build PDF file for labels


n1concepts

Recommended Posts

Hi,

 

I found a script from www.fpdf.org entitled, "Labels" to which provide code to build labels which works just fine.

However, the default script - see below - is only displaying a static entry based on $test. Note: I need to modify this loop to pull variables as explained later in example:

 

Here's the default script to which show a working solution if just the SAME static text repeating based on $test declared as string:

 

<?php
require('PDF_Label.php');

/*------------------------------------------------
To create the object, 2 possibilities:
either pass a custom format via an array
or use a built-in AVERY name
------------------------------------------------*/

// Example of custom format
// $pdf = new PDF_Label(array('paper-size'=>'A4', 'metric'=>'mm', 'marginLeft'=>1, 'marginTop'=>1, 'NX'=>2, 'NY'=>7, 'SpaceX'=>0, 'SpaceY'=>0, 'width'=>99, 'height'=>38, 'font-size'=>14));

// Standard format
$pdf = new PDF_Label('L7163');

$pdf->AddPage();

// Print labels
for($i=1;$i<=20;$i++) {
    $text = sprintf("%s\n%s\n%s\n%s %s, %s", "Laurent $i", 'Immeuble Toto', 'av. Fragonard', '06000', 'NICE', 'FRANCE');
    $pdf->Add_Label($text);
}

$pdf->Output();
?>

 

-----------

 

I need to modify the loop to parse the specified variables (shown below) and display those in PDF format:

 

<?php

require('PDF_Label.php');

/*------------------------------------------------
To create the object, 2 possibilities:
either pass a custom format via an array
or use a built-in AVERY name
------------------------------------------------*/

// Example of custom format
// $pdf = new PDF_Label(array('paper-size'=>'A4', 'metric'=>'mm', 'marginLeft'=>1, 'marginTop'=>1, 'NX'=>2, 'NY'=>7, 'SpaceX'=>0, 'SpaceY'=>0, 'width'=>99, 'height'=>38, 'font-size'=>14));

// Standard format
$pdf = new PDF_Label('L7163');

$pdf->AddPage();


// Assign each product to the $test variable to display each set of product (six strings) in same format as the default ex29.pdf - just w/the provided variable below:

$pn1 = "Product Name 1";
$f11 = "Feature 1 for Product 1";
$f12 = "Feature 2 for Product 1";
$f13 = "Feature 3 for Product 1";
$f14 = "Feature 4 for Product 1";
$price1 = "$4.95";


$pn2 = "Product Name 2";
$f21 = "Feature 1 for Product 2";
$f22 = "Feature 2 for Product 2";
$f23 = "Feature 3 for Product 2";
$f24 = "Feature 4 for Product 2";
$price2 = "$2.50";


$pn3 = "Product Name 3";
$f31 = "Feature 1 for Product 3";
$f32 = "Feature 2 for Product 3";
$f33 = "Feature 3 for Product 3";
$f34 = "Feature 4 for Product 3";
$price3 = "$1.75";


$pn4 = "Product Name 1";
$f41 = "Feature 1 for Product 4";
$f42 = "Feature 2 for Product 4";
$f43 = "Feature 3 for Product 4";
$f44 = "Feature 4 for Product 4";
$price4 = "$3.25";

// Print labels
for($i=1;$i<=4;$i++) {
//    need support to understand how I loop through each of the four session variables - inserting them into $test array
    $pdf->Add_Label($text);
}

$pdf->Output();
?>

 

In short, I included four possible sets of variable - each is a product which includes four strings, along with the product's name ($pn) and the price of product ($price).

 

Again, my question is how can I modify the default code to pull each set of those variables (per product) to display each as:

 

Product Name x

Feature 1

Feature 2

Feature 3

Feature 4

Price

 

for each of the four sets of products?

---------

 

You can find the entire set of code on www.fpdf.org by clicking on Scripts, and searching for "Labels".

There, you'll  find all the code and the example link which is http://fpdf.org/en/script/ex29.pdf

 

thx,

Craig

Link to comment
Share on other sites

Yes, that's actually what I have defined (sessions which is grabbing all the collected data).

My brian just fried at the moment but I think I should be able to simple define a sessions array (as you stated), pop all of the data in that array, then loop through it to fill the $test (array) variable, then convert as PDF.

 

I see the logic but just dumped right now - probably due to fatigue in fighting w/the code all day - on defining the snippet of code.

Going to crash for awhile and come back to it in afew hours.

 

I'll put results - as always but welcome suggestions if someone have it... :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.