String Pad Help

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

Post Reply
mattmcb
Forum Commoner
Posts: 27
Joined: Sun Jan 25, 2004 3:34 pm

String Pad Help

Post by mattmcb »

Does anyone have any ideas on why this won't work? I'm pulling a variables from the DB and I want to display them in a list evenly spaced out. But the str_pad function does not seem to be working so I must be doing something wrong:

Code: Select all

// Display member list for private message selection
	while ($row_add = mysql_fetch_array($result_add))
	{   
		echo ('<option value="'.$row_add['user_id'].'">'.str_pad($row_add['username'],35).' | '.str_pad($row_add['storename'], 25).' | '.str_pad($row_add['companyname'], 25).'</option>');
	}
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It just the way html ignores multiple spaces and also appears to be the formatting allowed inside the <option> tags.
Eg if you do this just as a test you'll see it's ok (without any select tags, just plain old echo's) :

Code: Select all

while ($row_add = mysql_fetch_array($result_add))
   {
      echo '<pre>'.str_pad($row_add['username'],35).' | '.str_pad($row_add['storename'], 25).' | '.str_pad($row_add['companyname'], 25).'</pre>';
   }
I'm not sure of the solution to get the correct spacing in the option tags, but maybe it's CSS related as using <pre> etc doesn't seem to work either around the select or inside the option tag :o
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

You can pad with &nbsp;
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Problem with using &nbsp; is that it's 6 chars long and therefore can get trimmed at weird places in a str_pad, i.e you could end up with sp; for example.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Pad with a standard space character then str_replace all spaces with &nbsp;
Post Reply