Congratulations to AcaCandy on her 100,000th post!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen boot bsod computer connection crash css dell driver drivers email error ethernet excel firefox firefox 3 hard drive internet internet explorer itunes laptop linux malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express partition password printer problem router security slow software sound trojan usb video virus vista wifi windows windows xp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
right-alignement in php


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
klam's Avatar
Senior Member with 181 posts.
 
Join Date: Apr 2006
Experience: Advanced
16-Oct-2007, 01:36 PM #1
right-alignement in php
Hi im trying to print something but i havent managed right align certain string:

PHP Code:
$text sprintf("%s\n%s\n%s%20s"$query_arr['val1'], $query_arr['val2'], $query_arr['val3'], $query_arr['val4']); 
so how do right align $query_arr['val4'] which is in the same row as $query_arr['val3']?
I put %20s but that's temporarely, that's not really what i want.

Code:
1111111
222222222222222222222
333333            444
That's how i want it to look basically, I read something about a minus sign but it didnt't work for some reason. (i.e %-s)

Thanks in advanced
__________________
"the only refuge from the damnation of the panta rei, and she guessed it was the Pendulum’s business, not hers"
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,705 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
16-Oct-2007, 01:53 PM #2
Quote:
Originally Posted by klam
That's how i want it to look basically, I read something about a minus sign but it didnt't work for some reason. (i.e %-s)
The minus sign is to left-justify the data being printed. Using the padding, as you're doing now, seems like the best option since you're wanting to have left-justified AND right-justified data on the same line. There are examples on the page I linked to which shows how the justification of text looks.

Something else you can try is to NOT output a newline character after you output 'val3' and split the outputting of 'val3' and 'val4' across two sprintfs, like this:

Code:
$text = sprintf("%s\n%s\n%-s", $query_arr['val1'], $query_arr['val2'], $query_arr['val3']);
$text .= sprintf("%s", $query_arr['val4']);
See what that does.

Peace...
klam's Avatar
Senior Member with 181 posts.
 
Join Date: Apr 2006
Experience: Advanced
19-Oct-2007, 09:14 AM #3
Hey Tom 1, as helpful as usual!

Padding is not an option because I don't know how long var3 and var4 are.

That solution of yours looked excellent but I just tried it and didn't work unfortunally.. =/ plus var4 would have to go aligned to the right and that hasn't been specify.

I think what I'll have to end up doing is measure the size of var3 and var4 then the length of the spacing and substact all that up. =/ really mediocre so if someone can come up with a better solution than the one I just proposed it'll be reeeally appreciated. =)

Thanks
__________________
"the only refuge from the damnation of the panta rei, and she guessed it was the Pendulum’s business, not hers"

Last edited by klam : 19-Oct-2007 09:23 AM.
klam's Avatar
Senior Member with 181 posts.
 
Join Date: Apr 2006
Experience: Advanced
19-Oct-2007, 10:06 AM #4
OK so I went ahead with my idea and this is my new problem:

PHP Code:
$padding $total_lenght - ($var3 $var4);
$text sprintf("%s\n%s\n%s%20s"$query_arr['val1'], $query_arr['val2'], $query_arr['val3'], $query_arr['val4']); 
How am i supposed to put a variable instead of the 20???

is there something that behaves like this:
PHP Code:
"%s\n%s\n%s%(exec $padding)s" 
obviously that doesnt work but it gets the point across.

SOLVED: nevermind.. here:
PHP Code:
"%s\n%s\n%s%${padding}s" 
I should have remembered that lol
__________________
"the only refuge from the damnation of the panta rei, and she guessed it was the Pendulum’s business, not hers"
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,705 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
19-Oct-2007, 06:21 PM #5
Quote:
Originally Posted by klam
That solution of yours looked excellent but I just tried it and didn't work unfortunally.. =/ plus var4 would have to go aligned to the right and that hasn't been specify.
What did it do?

Peace...
klam's Avatar
Senior Member with 181 posts.
 
Join Date: Apr 2006
Experience: Advanced
22-Oct-2007, 11:54 AM #6
the same thing it did when it was all together in the same line.. it didnt make any difference and it make sense cause it was the same code in other words... but good try tho.
klam's Avatar
Senior Member with 181 posts.
 
Join Date: Apr 2006
Experience: Advanced
23-Oct-2007, 10:45 AM #7
I'll still get any ideas to avoid padding since it might fail because the number of characters that can fit in one line varies each time (character sizes varies) so my approach fails easily.

Any ideas would be very much appreciate it
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,705 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
23-Oct-2007, 11:16 AM #8
Quote:
Originally Posted by klam
the same thing it did when it was all together in the same line.. it didnt make any difference and it make sense cause it was the same code in other words... but good try tho.
Hmmmm, ok so what does this line of code produce:

Code:
$text = sprintf("%s\n%s\n%-s", $query_arr['val1'], $query_arr['val2'], $query_arr['val3']);
Does it produce this:

Code:
1111111
222222222222222222222
333333
by chance? If not, what does it produce? Something else you can try is to create a loop to add padding if you can't get the justification to work as you want.

Peace...
klam's Avatar
Senior Member with 181 posts.
 
Join Date: Apr 2006
Experience: Advanced
26-Oct-2007, 09:25 AM #9
Yes that's what's being produced.

And the loop well... I don't know if that can help cause the distance is measure in inches (it's being print out to a pdf file) so I have no way of knowing how many inches are being used by the characters =/
__________________
"the only refuge from the damnation of the panta rei, and she guessed it was the Pendulum’s business, not hers"
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,705 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
26-Oct-2007, 11:38 AM #10
Quote:
Originally Posted by klam
Yes that's what's being produced.
Ok, then does this:
Code:
$text = sprintf("%s\n%s\n%-s", $query_arr['val1'], $query_arr['val2'], $query_arr['val3']);
$text .= sprintf("%s", $query_arr['val4']);
produce something like this:
Code:
1111111
222222222222222222222
333333444
If not, what is does my code snipet do? Sorry for these questions but I'm not in a position to try the code out myself.

Quote:
And the loop well... I don't know if that can help cause the distance is measure in inches (it's being print out to a pdf file) so I have no way of knowing how many inches are being used by the characters =/
Ah, font metrics. Yeah, that will be a problem.

Once I get a better idea of what's specifically NOT working (since we already know it's generally not working), maybe I'll come up with something different. I've already got another idea but I want to see what didn't work with my idea above first.

Thanks!

Peace...
klam's Avatar
Senior Member with 181 posts.
 
Join Date: Apr 2006
Experience: Advanced
26-Oct-2007, 01:03 PM #11
Yes, that's what that code produces.
For some reason putting a '-' there doesn't seem to make any difference... huh maybe im not understanding very well the alignment thingie in printf.
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,705 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
26-Oct-2007, 02:01 PM #12
Ok, I'll see if I can get a working PHP environment I can use to play around with this a bit. I'll post any "discoveries" I have when I have them.

Which PHP environment are you using?

Peace...
klam's Avatar
Senior Member with 181 posts.
 
Join Date: Apr 2006
Experience: Advanced
26-Oct-2007, 03:06 PM #13
hey there Tom,

Well I'm using PHP5.. so basically i'm using fprintf to print my query results to pdf using the fpdf library.
I'm using spacing in inches for margins and columns, etc.. so as you can see, it's difficult to measure the spacing by the number of characters =/

Well it's a tricky thing to figure out but if you come up with something that'd be amazing
__________________
"the only refuge from the damnation of the panta rei, and she guessed it was the Pendulum’s business, not hers"
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,705 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
26-Oct-2007, 05:03 PM #14
Well, I got my PHP environment up and running and have been playing with a test script. I've been able to get padding to work with a loop but given I'm working with web output, spaces are padding as they would when written to a file. So, I'm using a pad character of some kind (# or !, etc). My loop will support a variable data length, so the amount of padding required would vary, etc.

I don't know of any way to do this without padding. I can post my test code, if you want to see what I did which is probably not much different from what you've been doing thus far.

Peace...
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 05:56 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.