Page 1 of 1

Padding Variables?

Posted: Thu Oct 10, 2002 2:47 pm
by JPlush76
Hey guys, I have to pass data to another system and instead of comma separated I have to pass fixed width fields

I have to pass order data as a string
IE:

ordernum, customer_number, customer_name

well in my order file the ordernum might be 6 length in the mysql table, the customer_number 7 length, and the customer_name 30 length.

basically what I want to do is test the variable against the field length in the table and then if its less than that, pad the rest of the field with spaces

so my string would look like:

12 6985 John Smith

instead of:
126985JohnSmith

anyone? :(

Posted: Thu Oct 10, 2002 2:55 pm
by hob_goblin
you could do this:

Code: Select all

str_pad($var, 10, " ", STR_PAD_RIGHT);
now, $var would be the var getting padded, it will then pad it with " " until it is 10 chars in length, and it's set to pad to the right

remember, you're probably going to want to convert these " "s to &nbps;'s

Posted: Thu Oct 10, 2002 3:41 pm
by volka
to retrieve the table-definition take a look at the result of

Code: Select all

$query = 'DESCRIBE '.$tablename;
(if you wanted to know this either ;) )

Posted: Thu Oct 10, 2002 3:42 pm
by JPlush76
hob you are the F'ing man :)

works likes charm