Page 1 of 1

String Pad Help

Posted: Sun Jul 04, 2004 6:54 pm
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>');
	}

Posted: Sun Jul 04, 2004 7:19 pm
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

Posted: Sun Jul 04, 2004 7:35 pm
by redmonkey
You can pad with &nbsp;

Posted: Sun Jul 04, 2004 7:39 pm
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.

Posted: Sun Jul 04, 2004 7:42 pm
by redmonkey
Pad with a standard space character then str_replace all spaces with &nbsp;