regsamp
asked on
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/Pri nter.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["Cop ies"])) $arRecipient["Copies"] = 1;
if(
(empty($arRecipient["FullN ame"]) && empty($arRecipient["Compan y"])) ||
empty($arRecipient["Addres s1"]) ||
empty($arRecipient["City"] ) ||
empty($arRecipient["State" ]) ||
empty($arRecipient["Postal Code"])
)
{
throw new Exception("Error in EnvelopePrinter.printEnvel ope: 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($ar Recipient[ "PrinterNa me"]); //DWMA Printer: HP LaserJet M606 PCL 6
$oPrinter->setCopies($hndl Printer, $arRecipient["Copies"]);
$oPrinter->setPaperBin($hn dlPrinter, "Tray 4"); //DWMA Printer Envelope Feeder: "Tray 4"
$oPrinter->setPaperSize($h ndlPrinter , DMPAPER_ENV_10);
$oPrinter->setOrientation( $hndlPrint er, DMORIENT_LANDSCAPE);
$oPrinter->setChangedOptio ns($hndlPr inter);
$hndlBrush = $oPrinter->createBrush("PT R7_BRUSH_S OLID", "#000000");
$oPrinter->selectBrush($hn dlPrinter, $hndlBrush);
$hndlFont1 = $oPrinter->createFont($hnd lPrinter, "Times New Roman", 12, PTR7_FW_BOLD);
$hndlFont2 = $oPrinter->createFont($hnd lPrinter, "Times New Roman", 12);
$oPrinter->startDoc($hndlP rinter, "DWMA Envelope");
$oPrinter->startPage($hndl Printer);
$oPrinter->selectFont($hnd lPrinter, $hndlFont1);
$oPrinter->drawText($hndlP rinter, $FirstLine, $X, $Y);
$oPrinter->selectFont($hnd lPrinter, $hndlFont2);
if($SecondLine != "")
{
$Y += 20;
$oPrinter->drawText($hndlP rinter, $SecondLine, $X, $Y);
}
$Y += 20;
$oPrinter->drawText($hndlP rinter, $ThirdLine, $X, $Y);
$Y += 20;
$oPrinter->drawText($hndlP rinter, $FourthLine, $X, $Y);
/** Uncomment to print Country ***
if($FifthLine != "")
{
$Y += 20;
$oPrinter->drawText($hndlP rinter, $FifthLine, $X, $Y);
}
*/
$oPrinter->endPage($hndlPr inter);
$oPrinter->endDoc($hndlPri nter);
$oPrinter->deleteFont($hnd lFont1);
$oPrinter->deleteFont($hnd lFont2);
$oPrinter->deleteBrush($hn dlBrush);
$oPrinter->closePrinter($h ndlPrinter );
//echo $oPrinter->sMsg;
}
catch(Exception $exp)
{
throw new Exception("Error in EnvelopePrinter.printEnvel ope: " . $exp->getMessage());
}
}
}
?>
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/Pri
$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["Cop
if(
(empty($arRecipient["FullN
empty($arRecipient["Addres
empty($arRecipient["City"]
empty($arRecipient["State"
empty($arRecipient["Postal
)
{
throw new Exception("Error in EnvelopePrinter.printEnvel
}
if($arRecipient["FullName"
{
$FirstLine = $arRecipient["Company"];
$SecondLine = "ATTN: " . $arRecipient["FullName"];
}
else if($arRecipient["FullName"
{
$FirstLine = $arRecipient["FullName"];
}
else
{
$FirstLine = $arRecipient["Company"];
}
$ThirdLine = $arRecipient["Address1"];
if($arRecipient["Address2"
$FourthLine = $arRecipient["City"] . ", " . $arRecipient["State"] . " " . $arRecipient["PostalCode"]
if($arRecipient["Country"]
try
{
$hndlPrinter = $oPrinter->openPrinter($ar
$oPrinter->setCopies($hndl
$oPrinter->setPaperBin($hn
$oPrinter->setPaperSize($h
$oPrinter->setOrientation(
$oPrinter->setChangedOptio
$hndlBrush = $oPrinter->createBrush("PT
$oPrinter->selectBrush($hn
$hndlFont1 = $oPrinter->createFont($hnd
$hndlFont2 = $oPrinter->createFont($hnd
$oPrinter->startDoc($hndlP
$oPrinter->startPage($hndl
$oPrinter->selectFont($hnd
$oPrinter->drawText($hndlP
$oPrinter->selectFont($hnd
if($SecondLine != "")
{
$Y += 20;
$oPrinter->drawText($hndlP
}
$Y += 20;
$oPrinter->drawText($hndlP
$Y += 20;
$oPrinter->drawText($hndlP
/** Uncomment to print Country ***
if($FifthLine != "")
{
$Y += 20;
$oPrinter->drawText($hndlP
}
*/
$oPrinter->endPage($hndlPr
$oPrinter->endDoc($hndlPri
$oPrinter->deleteFont($hnd
$oPrinter->deleteFont($hnd
$oPrinter->deleteBrush($hn
$oPrinter->closePrinter($h
//echo $oPrinter->sMsg;
}
catch(Exception $exp)
{
throw new Exception("Error in EnvelopePrinter.printEnvel
}
}
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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"] != "")
Maybe not a space but an example would be: $FirstLine = ""; Or even in your replacement code of:
if($arRecipient["FullName"
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
ASKER
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.
I see. Thank you for the clarification.
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.
ASKER
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.
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!
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!
ASKER
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.