Connecting Tech Pros Worldwide Forums | Help | Site Map

Maintain spacing when concatenating

Newbie
 
Join Date: Nov 2008
Location: San Diego
Posts: 1
#1: Nov 1 '08
I'm trying to concatenate a half dozen fields from an sql statement into a string, then loading the string in a select list. I can't seem to find a way to keep them 'lined up' - so if, for instance, I concatenate a name field and address field, I want the first letter of the first names to line up, and the first letter of the addresses to line up in my list box. I've been trying str_pad(), but can't get that to work for some reason - If I pad with a character, I get the characters in the result, but if I pad with blanks, I lose the blanks. Don't get it.

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#2: Nov 1 '08

re: Maintain spacing when concatenating


I'm not sure what you been by concatenating in a select list (I'm assuming you mean a HTML select box)

HTML ignores spaces. For output you can use the <pre></pre> tags and that will display the text just as you type it into the code, spaces and all.

From what it sounds like you're having a HTML issue, and its not related to PHP.





Dan
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#3: Nov 1 '08

re: Maintain spacing when concatenating


We would have to see the actual code to be able to say for sure, but I think Dan got it right. HTML does not preserve white-spaces, such as spaces and newlines.

If you need to add a series of spaces into your HTML you have three options:
  • The easiest solution would be to use the &nbsp; entity. It represents a single space.
  • Second, as Dan suggested, the <pre> tag preserves any white-spaces that occur within it.
  • Or, if you use CSS, you can apply the "white-space: pre" style to the element containing your data. That would preserve the white-spaces, just like a <pre> tag would.
Reply