[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 111
  • Last Modified:

PHP question(s) about order of output

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
regsamp
Asked:
regsamp
  • 5
  • 4
1 Solution
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
 
regsampAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Ray PaseurCommented:
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
 
regsampAuthor Commented:
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
 
Ray PaseurCommented:
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
 
regsampAuthor Commented:
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
 
Ray PaseurCommented:
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
 
regsampAuthor Commented:
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

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now