...

View Full Version : ADD A Leading Space



Spinosaurus
03-07-2010, 03:00 PM
I'm a novice cobbling a script together for a new development. I've search your forum, but can't find an answer.

Project: I'm generating columns of random numbers (in sets) that output to HTML. Nothing fancy, no database, just simple HTML. I have managed to assemble a script that does exactly what I want, sans one.

Problem: I would like the rows of the output to line up neatly, strictly for appearances sake. So, I need to know how to add a leading space (not zeros) in front of the numbers 0-9

Here's a snip of the relevant code:


function generator($maxnumber="99",$maxcols="10") {
srand((double) microtime() * 1000000);

while (1>0) {
$generator[] = rand(0,$maxnumber);


I hope that there is a simple solution, thanks in advance.

met
03-07-2010, 04:19 PM
text-align:right;

Spinosaurus
03-08-2010, 01:41 PM
text-align:right;

Thanks for the reply, but that solution doesn't resolve the issue, it just pushes the uneven data to the right. My desire is to have rows and columns of data that line up. Example:

Output as the script functions now:
XX X X XX X
X XX XX XX X
X X X XX X

Desired solution:
XX xX xX XX xX
xX XX XX XX xX
xX xX xX XX xX

I'm working with special needs individuals and believe that neatly defined cols and rows would aid in perception.

Thanks again -

bazz
03-09-2010, 08:46 AM
can you show us a link to the page?

If you align the text to the right, then the unit digits should align vertically. then if you don't want the numbers to be so far to the right, you could add padding-right:10px; or some other value.

bazz

Spinosaurus
03-09-2010, 05:24 PM
can you show us a link to the page?
bazz

I'm working on it in a local PHP server environment. The numbers aren't called individually, but as an array.

Here's the function...:
function whitenumbers($maxnumber="99",$maxcols="10") {
srand((double) microtime() * 1000000);

while (1>0) {
$whitenumbers[] = rand(0,$maxnumber);
$whitenumbers = array_unique($whitenumbers);
if (sizeof($whitenumbers) == $maxcols) break;
}
sort($whitenumbers);
return implode("   ",$whitenumbers);
}

...then I can call as many sets (rows) as needed ($whitenumbers1, $whitenumbers2, etc.):

$whitenumbers1 = whitenumbers();
<h3><?=$whitenumbers1?></h3>

Again, I admit that I'm a hack when it come to PHP, but can't afford a pro coder. Usually I can manage to string together what I need by researching, but this particular issue is proving to be particularly evasive. I know it can be done as I see many references to it when Googled, but can't seem to find the solution that works with my particular situation.

Thanks again -

Spinosaurus
03-09-2010, 10:42 PM
function whitenumbers($maxnumber="99",$maxcols="10") {
srand((double) microtime() * 1000000);

while (1>0) {
$whitenumbers[] = rand(0,$maxnumber);
$whitenumbers = array_unique($whitenumbers);
if (sizeof($whitenumbers) == $maxcols) break;
}


The solution:
$whitenumbers[] = str_pad(rand(1,$maxnumber), 2, "0", STR_PAD_LEFT);

Ahri
03-10-2010, 02:30 PM
printf("% 2d", $number);(print-format)

"%d" is "decimal", modifiers go in between;
" " -- prefix with spaces (could add 0 here, or whatever other char, but you said spaces)
"2" -- pad the number up to 2 characters long

result:
9 -> " 9"
99 -> "99"
999 -> "999"

Obviously printf() will output straight away, if you would prefer $generator to contain pre-padded numbers you'll need to use sprintf() (string-print-format):


$generator[] = sprintf("% 2d", rand(0,$maxnumber));

printf() is directly "borrowed" off C, but here's the PHP description of it: http://php.net/printf

Formatting is specified on the sprintf page: http://php.net/sprintf



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum