Solved

PHP question(s) about order of output

Posted on 2016-10-28
9
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41864187
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 110

Expert Comment

by:Ray Paseur
ID: 41864191
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
ID: 41864194
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41864215
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
 

Author Comment

by:regsamp
ID: 41864236
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 110

Expert Comment

by:Ray Paseur
ID: 41864267
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
ID: 41864284
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 110

Expert Comment

by:Ray Paseur
ID: 41864299
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
ID: 41864304
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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