Forum Moderators: coopster

Message Too Old, No Replies

sprintf problem

need to print invoice in plain text, can't align columns

         

stidj

11:09 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



Hi guys,

I'm almost done my php shopping cart but the only problem I have I have is with the plain text email.

As you will see I am using sprintf to try and format the invoice but no matter what I try, the columns are never lined up. I know there is a way to do this, but I can't see how and this is my last resort for help :).

$confirmation .= sprintf("%08s %40s %5s %4s %8s\n", $item, $productname,$productprice, $productquantity, $totalproductprice);

I'm not sure if this will show up right, but the items below appear on a single line. If you took each line, you would see they are far from being aligned.

11158250 Rainwater Shampoo $6.85 1 $6.85

11544100 Scalp Lotion (Itchy flaking scalp) $30.75 7 $215.25

00022113 25 ml. Cobalt Round / Dropper - 1 oz. $1.60 2 $3.20

I would be so thankful if anyone can show me how to format text into columns of equal width (virtually) using sprintf or any other php function.

Thanks guys.

Cheers
:)

Netizen

10:09 am on Apr 27, 2004 (gmt 0)

10+ Year Member



I am guessing it doesn't look aligned because multiple spaces are being ignored - this might depend on the email client you are using. Try looking at the message source to see if it is formatting correctly. Your code should work fine.

Try

$confirmation .= sprintf("%08s %'-40s %'-5s %'-4s %'-8s\n", $item, $productname,$productprice, $productquantity, $totalproductprice);

and see what it does. (I haven't checked the code!)

hakre

10:39 am on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi stidj,

(comment: haven't seen netizens answer here, but this is still valuable i think)

from what i know so far about sprintf, your code looks ok. parameters are right (

%[i]#n[/i]s
), i'm just wondering if the output is pre-formatted (i.e. a font like courier used and duplicate spaces are displayed.

use the

<pre></pre>
tag in your html output to verify or use an emailclient which uses courier new as font to display mails.

--hakre

stidj

4:40 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



I printed it out to the browser and it looks the same as in the plain-text email. It is not aligned into columns.

Is there any other way to format a plain text email to ensure everything is aligned into columns?

Or is there some php script that formats invoices into aligned plain-text emails :)

stidj

4:44 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



An update:

If I print it out in html and use <pre> tags then it looks proper....

But I still don't know how to make it appear the same way in the plain text email I send based on the sprintf output?

Any help would be appreciated very much.

Netizen

6:03 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



It is your email client deciding to view all the spaces as a single space, in all likelihood.

Can you not just send an HTML version of the email?

stidj

6:21 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



I don't think it is a client issue, it appeared the same way when checking in a webmail and local client (Outlook Express).

I decided to just go html because when using <pre> tags it works perfectly. I realize that some people view plain text but most can view html I suppose :)

Thanks all

hakre

8:59 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi stidji,

you can configure outlook express to use courier on plain text mails. i did ever because a lot of mails using plain text look better and are formated better with a fixed-width font like courier.

anyways now with eudora, every plain text mail is displayed in courier by default. hope that helps you a bit for your decisions.

--hakre