Solved

PHP question(s) about order of output

Posted on 2016-10-28
9
39 Views
Last Modified: 2016-10-31
I am new to coding or PHP so please bear with me. In the coding below, in the Bold section, how would we code it if we want the Full Name without the ATTN: before the Company?  And is there any other locations it needs to be changed?

The envelope currently prints out with the Company first and then the Name of the person. We want to reverse that.

Also on a side question, what does the space in-between the parentheses mean? = " "  
<?php
require_once("Business/Printer.php");
$oPrinter = new Printer();

class envelopePrinter
{
      
      public function __construct()
      {
            $args = func_get_args();            
      }
      
      function printEnvelope($arRecipient)
      {
            global $oPrinter;            
            
            $FirstLine = "";
            $SecondLine = "";
            $ThirdLine = "";
            $FourthLine = "";
            $FifthLine = "";
            $hndlPrinter = null;
            $hndlBrush = null;
            $hndlFont1 = null;
            $hndlFont2 = null;
            //Scale is .01 inches. You can change it in Printer.php
            $X = 480;
            $Y = 200;
            
            if(empty($arRecipient["Copies"])) $arRecipient["Copies"] = 1;
            if(
                  (empty($arRecipient["FullName"]) && empty($arRecipient["Company"])) ||       
                  empty($arRecipient["Address1"]) ||
                  empty($arRecipient["City"]) ||
                  empty($arRecipient["State"]) ||
                  empty($arRecipient["PostalCode"])
            )
            {
                  throw new Exception("Error in EnvelopePrinter.printEnvelope: Insufficient Data for mailing name and/or address.");
            }
            
            if($arRecipient["FullName"] != "" && $arRecipient["Company"] != "")
            {
                  $FirstLine = $arRecipient["Company"];
                  $SecondLine = "ATTN: " . $arRecipient["FullName"];

            }
            else if($arRecipient["FullName"] != "")
            {
                  $FirstLine = $arRecipient["FullName"];
            }
            else
            {
                  $FirstLine = $arRecipient["Company"];
            }
            
            $ThirdLine = $arRecipient["Address1"];
            if($arRecipient["Address2"] != "") $ThirdLine .= " " . $arRecipient["Address2"];            
            $FourthLine = $arRecipient["City"] . ", " .  $arRecipient["State"] . " " .  $arRecipient["PostalCode"];
            if($arRecipient["Country"] != "") $FifthLine = $arRecipient["Country"];
            
            try
            {
                  $hndlPrinter = $oPrinter->openPrinter($arRecipient["PrinterName"]); //DWMA Printer: HP LaserJet M606 PCL 6
                  
                  $oPrinter->setCopies($hndlPrinter, $arRecipient["Copies"]);
                  $oPrinter->setPaperBin($hndlPrinter, "Tray 4"); //DWMA Printer Envelope Feeder: "Tray 4"
                  $oPrinter->setPaperSize($hndlPrinter, DMPAPER_ENV_10);
                  $oPrinter->setOrientation($hndlPrinter, DMORIENT_LANDSCAPE);
                  
                  $oPrinter->setChangedOptions($hndlPrinter);
                  
                  $hndlBrush = $oPrinter->createBrush("PTR7_BRUSH_SOLID", "#000000");
                  $oPrinter->selectBrush($hndlPrinter, $hndlBrush);
                  
                  $hndlFont1 = $oPrinter->createFont($hndlPrinter, "Times New Roman", 12, PTR7_FW_BOLD);
                  $hndlFont2 = $oPrinter->createFont($hndlPrinter, "Times New Roman", 12);
                  
                  $oPrinter->startDoc($hndlPrinter, "DWMA Envelope");                  
                  $oPrinter->startPage($hndlPrinter);      
                  
                  $oPrinter->selectFont($hndlPrinter, $hndlFont1);
                  $oPrinter->drawText($hndlPrinter, $FirstLine, $X, $Y);
                  
                  $oPrinter->selectFont($hndlPrinter, $hndlFont2);
                  if($SecondLine != "")
                  {
                        $Y += 20;
                        $oPrinter->drawText($hndlPrinter, $SecondLine, $X, $Y);                  
                  }
                  
                  $Y += 20;
                  $oPrinter->drawText($hndlPrinter, $ThirdLine, $X, $Y);                  
                  
                  $Y += 20;
                  $oPrinter->drawText($hndlPrinter, $FourthLine, $X, $Y);
                  
                  /** Uncomment to print Country ***
                  if($FifthLine != "")
                  {
                        $Y += 20;
                        $oPrinter->drawText($hndlPrinter, $FifthLine, $X, $Y);
                  }
                  */
                  $oPrinter->endPage($hndlPrinter);
                  $oPrinter->endDoc($hndlPrinter);                  
                  
                  $oPrinter->deleteFont($hndlFont1);
                  $oPrinter->deleteFont($hndlFont2);
                  $oPrinter->deleteBrush($hndlBrush);                  
                  
                  $oPrinter->closePrinter($hndlPrinter);
                  //echo $oPrinter->sMsg;
            }
            catch(Exception $exp)
            {
                  throw new Exception("Error in EnvelopePrinter.printEnvelope: " . $exp->getMessage());
            }
      }
}
?>
0
Comment
Question by:regsamp
  • 5
  • 4
9 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Just a guess, but you might try something like this.
            if($arRecipient["FullName"] != "" && $arRecipient["Company"] != "")
            {
                  $SecondLine = $arRecipient["Company"];
                  $FirstLine = $arRecipient["FullName"];
            }

Open in new window

If you're new to PHP and want to learn the language, this article can help.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
what does the space in-between the parentheses ...
Please isolate an example that shows what you're asking about.  Spaces may or may not have semantic meaning, depending on the context.
0
 

Author Comment

by:regsamp
Comment Utility
Okay, let me look at your example. And the change can be done on the live site and it should take affect, correct? I will certainly take a look at the link.

Maybe not a space but an example would be:  $FirstLine = ""; Or even in your replacement code of:
if($arRecipient["FullName"] != "" && $arRecipient["Company"] != "")
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
And the change can be done on the live site and it should take affect, correct?
NEVER make a change on a live site!  Always test first, then push the code after all your tests pass.

if($arRecipient["FullName"] != "" ...

This statement is saying to PHP, compare $arRecipient["FullName"] to an empty string, and if it is not  an empty string, consider this part of the evaluation True.  The exclamation point is a negation sign in this context.
http://php.net/manual/en/language.expressions.php
http://php.net/manual/en/language.operators.comparison.php
http://php.net/manual/en/types.comparisons.php
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:regsamp
Comment Utility
I was under the impression that PHP like HTML could be changed live and if there were any issues just restore the PHP page from backup but I certainly understand. I just don't have the setup to test locally on any machine as we have a PHP Programmer that does our work.

I see. Thank you for the clarification.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
In that case, I would definitely make a backup before I made any change.  A better strategy is to use a version control system like Git.  You can make a branch, install your change, and revert easily if anything goes wrong.  Git assumes some programmer-like skills, but it's not too hard to figure out.
0
 

Author Comment

by:regsamp
Comment Utility
I will have to look into that. We currently run an Intranet Site with PHP on IIS and I was going to intall IIS and put the same exact coding on my machine to run tests in-case the programmer was not readily available. I thought for a quick change like this though (Your sample code) that I would try and restore the whole page if needed.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
There are a number of tricks-of-the-trade that you can choose, but if anything fails on a live server, that's bad.  So here is one thing you might try if the script is idempotent.

Backup the web site.

Copy the page (PHP scripts and all).  Give the copy a new name, that is unlike anything in the existing web application.  For example, if the original page was named "print.php," name the new page "regsamp_print.php."  Put this new copy into the same directory as the original page.  Then type the new page name into the browser address bar.  If it works as expected, make your changes in the new page and test until you're satisfied.  Then when things are working fine, rename "regsamp_print.php" to "print.php," overwriting the existing script.

It's a "hack" but it may work OK.  And if all else fails, you can restore the backup!
0
 

Author Comment

by:regsamp
Comment Utility
That is a great trick of the trade. Until I get my IIS going with the same coding, I might actually try that way. Thanks for that tip and I am going to look into doing that. Well hack or no hack I like it and every thing helps a beginner.
1

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now