SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP to PDF labels layout - Code problem

    I'm using PHP to output address details to a PDF which is formatted to match an avery label layout and am just struggling with the final part of my coding.

    When i use the following code everything works fine.

    PHP Code:
    do {
        
    $DueSweepDate strftime('%d / %m / %Y',$row ['NextSweepDate']);
        
    $text sprintf("%s %s\n%s %s\n%s\n%s\n%s\n%s\n\nCustID: %s\nDue Sweep Date: %s"$row['FirstName'] ,$row['Surname'], $row['HouseNumber'] ,$row['Address1'],  $row['Address2'], $row['Address3'], $row['TownCity'], $row['PostCode'], $row['CustID'], $DueSweepDate);
        
    $pdf->Add_Label($text);
    } while (
    $row mysql_fetch_array($result));

    echo 
    $text;

    $pdf->Output(); 
    but if the data for a particular field doesn't exist there is a gap in the flow of the address, so the address label appears along the lines of:

    Persons Name
    Address line 1

    Adress line 3
    Town or City
    Postcode

    In order to remove any additional liines so the address layout flows properly i felt i needed to generate the $text variable dynamically as the data is ploughed through.

    This is what i have ended up with after playing around a little

    PHP Code:
        $DueSweepDate strftime('%d / %m / %Y',$row ['NextSweepDate']);
        
    $vars "%s %s";
        if (
    $row['HouseNumber'])$vars .='\n%s';
        if (
    $row['Address1'])$vars .=' %s';
        if (
    $row['Address2'])$vars .='\n%s';
        if (
    $row['Address3'])$vars .='\n%s';
        if (
    $row['TownCity'])$vars .='\n%s';
        if (
    $row['PostCode'])$vars .='\n%s';
        
    $vars .='\n';
        if (
    $row['CustID'])$vars .='\nCustID: %s';
        if (
    $row['NextSweepDate'])$vars .='\nDue Sweep Date: %s';
        
        
    $data $row['FirstName'];
        
    $data .= ',' .$row['Surname'];
        if (
    $row['HouseNumber'])$data .= ',' $row['HouseNumber'];
        if (
    $row['Address1'])$data .= ', ' $row['Address1'];
        if (
    $row['Address2'])$data .= ', ' $row['Address2'];
        if (
    $row['Address3'])$data .= ', ' $row['Address3'];
        if (
    $row['TownCity'])$data .= ', ' $row['TownCity'];
        if (
    $row['PostCode'])$data .= ', ' $row['PostCode'];
        if (
    $row['CustID'])$data .= ', ' .$row['CustID'];
        if (
    $row['NextSweepDate'])$data .= ', ' $DueSweepDate;
        
        
        
    $text sprintf('"'.$vars.'"' .",".($data));

        
    $pdf->Add_Label($text);
    } while (
    $row mysql_fetch_array($result));

    $pdf->Output(); 
    i'm still learning php so there may be glaring errors in there but i just can't quite get the code to working.

    If you can see what i am trying to achieve can anyone offer me some advice and direction with this?

    Thanks

    James

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Dont you just need to check $data and if you find "\n" next to another "\n" then clearly a field was empty?

    Echo out $data with some test values, and see if you can get something like this to work;

    $data = str_replace("\n\n\", "\n", $data );

    There may be an issue with the slashes, so post the actual output of var_dump($data) here, where $data is one label with a missing address2 as in your question.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This would cause another problem i think, at the bottom of the address i actually want a blank line as a customer ID and due sweep date is added, but i want to separate it from the address.

    The blank line is created by having \n\n in the code.?

  4. #4
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error message i get when i run my current code is the following if it helps?

    Code:
    %s %s\n%s\n%s\n\nCustID: %s\nDue Sweep Date: %svbvbvbvnbvnb,vbnvnbnvbn,vbbnvbnvn, vbvb, 37, 12 / 12 / 2008
    Warning: sprintf() [function.sprintf]: Too few arguments in C:\wamp\www\FluetechCustomers\PDF\print_pdf.php on line 89
    however the code is indicating 6 agruments in the form of "%s" and there are 6 pieces of data attached (the random text and numbers). I think the problem may lie in that there is no split between the last %s and the follwing data string?

  5. #5
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well with some trail and error i have got this working!

    here's the code i ended up with:

    PHP Code:
    do {
        
    $DueSweepDate strftime('%d / %m / %Y',$row ['NextSweepDate']);
        
        
    $data $row['FirstName'] ." ";
        
    $data .=  $row['Surname'] ."\n";
        if (
    $row['HouseNumber'])$data .= $row['HouseNumber'];
        if (
    $row['Address1'])$data .= $row['Address1'] ."\n";
        if (
    $row['Address2'])$data .= $row['Address2'] ."\n";
        if (
    $row['Address3'])$data .= $row['Address3'] ."\n";
        if (
    $row['TownCity'])$data .= $row['TownCity'] ."\n";
        if (
    $row['PostCode'])$data .= $row['PostCode'] ."\n";
        if (
    $row['CustID'])$data .= "\n" ."CustID: " .$row['CustID'] ."\n";
        if (
    $row['NextSweepDate'])$data .=  "Due Sweep Date: " .$DueSweepDate;
        
        
    $query = ($data);
        
        
    $text sprintf($query);    
        
        
    $pdf->Add_Label($text);
    } while (
    $row mysql_fetch_array($result));

    $pdf->Output(); 
    I have been able to included the formatting of the layout into the if statements and it produces the pdf as expected!

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Fair enough, using an if() to see if the variable is set sounds much better.

    This means anyone filling in a form with a ' ' space, will cause it to add a line too, so just use this somewhere before you

    array_map( "trim" , $_POST) ;

    Before you store the info in the database, or

    array_map( "trim" , $row ) ;

    after you get it out.

  7. #7
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cups,

    i see what you are saying, if a field inadvertently only contains a blank space it will be recognised as having a value and therefore generate an empty line in the output.

    What exactly is the array_map("trim" , $row ); doing? i presume it is removing any values that equal "space"?? I also presume that it would not remove the space if a field was filled in as "something (space) something"??

    Last question!

    Where would include the array_map("trim" , $row ); considering i am using the follwing code:

    PHP Code:
    if ($StartPeriod && $EndPeriod){
    //// if StartPeriod and EndPeriod set displays all records relevant records//////////////////////////////
    $query        "SELECT CustID
                        , FirstName
                        , Surname
                        , HouseNumber
                        , Address1
                        , Address2
                        , Address3
                        , TownCity
                        , PostCode
                        , NextSweepDate
                    FROM customers where NextSweepDate BETWEEN '
    $StartPeriod' AND '$EndPeriod' ORDER BY NextSweepDate";
    $result        = @mysql_query($query);
    $row    mysql_fetch_array($result);
    };




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

    $pdf->AddPage();


    // Print labels

    do {
        
    $DueSweepDate strftime('%d / %m / %Y',$row ['NextSweepDate']);
        
        
    $data $row['FirstName'] ." ";
        
    $data .=  $row['Surname'] ."\n";
        if (
    $row['HouseNumber'])$data .= $row['HouseNumber'];
        if (
    $row['Address1'])$data .= $row['Address1'] ."\n";
        if (
    $row['Address2'])$data .= $row['Address2'] ."\n";
        if (
    $row['Address3'])$data .= $row['Address3'] ."\n";
        if (
    $row['TownCity'])$data .= $row['TownCity'] ."\n";
        if (
    $row['PostCode'])$data .= $row['PostCode'] ."\n";
        if (
    $row['CustID'])$data .= "\n" ."CustID: " .$row['CustID'] ."\n";
        if (
    $row['NextSweepDate'])$data .=  "Due Sweep Date: " .$DueSweepDate;
        
        
    $query = ($data);
        
        
    $text sprintf($query);    
        
        
    $pdf->Add_Label($text);
    } while (
    $row mysql_fetch_array($result));

    $pdf->Output(); 
    I'd guess that it would appear after the SELECT statement at the top, below the line

    PHP Code:
    $row    mysql_fetch_array($result); 
    Thanks for your advice.

    James

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    After you do command would work, I think.

    Ideally though, you should make sure that you trim all values if they are coming from any kind of input form as they are put into your database.

    Its pretty easy to chop off all strings starting or ending with a space with an sql command.

    If the values are coming from input forms or from any external source, then you should also be escaping the values before outputting them, using htmlentities or similar.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •