PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
icarpenter
Forum Commoner
Posts: 84 Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England
Post
by icarpenter » Tue Apr 19, 2005 5:55 am
Hi can anyone help?
I am trying to send an email in text format but I cannot get all of my values to display at equal intervals on the page without manually altering the tab amounts for each label. I intend to use the $emess variable in a loop but the labels may differ in length and I would like to display all values in the same area on the page without using HTML...
Does anyone no of an easy way this can be done?
Code: Select all
<pre>
<?
$value1='test data1';
$value2='test data2';
$value3='test data3';
$value4='test data4';
$to = "someone@somewhere.com";
$subject = "My subject";
$emess= "Test1:\t$value1\n";
$emess.= "Long label2:$label2\t$value2\n";
$emess.= "3:$label3\t$value3\n";
$emess.= "Test4:$label4\t$value4\n";
$headers .= "My Website <web@mysite.com>\r\n";
mail($to, $subject, $emess, $headers);
echo"$emess";
?>
</pre>
Thanks Ian...
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Apr 19, 2005 7:17 am
suggestion: don't use tab. Tab is varying size in differing clients. Instead, use sprintf() to align with spaces for you.
icarpenter
Forum Commoner
Posts: 84 Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England
Post
by icarpenter » Wed Apr 20, 2005 12:33 am
I have tried using the following but the formatting the data displayed in my second column is still inconsistant:
This is what gets printed:
Code: Select all
Test1: test data1
Long label2: test data2
3: test data3
Test4: test data4
This is what I am trying to acheive...
Code: Select all
Test1: test data1
Long label2: test data2
3: test data3
Test4: test data4
I tried using:
Code: Select all
$value2='%f test data2';
sprintf($value2,' ');
But this gave the same results as using \t as this was spacing from the last character of the left column.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Apr 20, 2005 1:15 am
you're supposed to use the alignment rules in sprintf().. %10s for instance..
icarpenter
Forum Commoner
Posts: 84 Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England
Post
by icarpenter » Wed Apr 20, 2005 2:38 am
I have just tried the following but this is still seems to be taking its reference point from the last character of the preceeding text...
I tried...
Code: Select all
$value1='test1';
$value2='test2';
$test = sprintf('%20s',$value1);
$test1 = sprintf('%20s',$value2);
$emess= "Long label2:$test\n";
$emess.= "3:$test1\n";
echo"$emess";
Do you know of anything else I could try?
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Wed Apr 20, 2005 6:41 am
You can always count the length of your label, and compare it to a set value of how far the next column should be.
Code: Select all
$spaces = 30;
$output = ($spaces - count($word).$spaces.$label);
A rough, very rough, example
.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Apr 20, 2005 7:09 am
The label should be sent through sprintf's alignment stuff to pad out the right side for the rest of the line.
icarpenter
Forum Commoner
Posts: 84 Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England
Post
by icarpenter » Thu Apr 21, 2005 12:42 am
Thats got it
...Many Thanks Fayd,Jcart...
For those who are interested...
The following:
Code: Select all
$value1='test1';
$value2='test2';
$test1 = sprintf('%-20s',$label1='Long label 2:');
$test2 = sprintf('%-20s',$label2='3:');
$emess= "$test1 $value1\n";
$emess.= "$test2 $value2\n";
Prints...