Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ADD A Leading Space

    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.

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    text-align:right;

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by met View Post
    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 -

  • #4
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #5
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bazz View Post
    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 -

  • #6
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Spinosaurus View Post
    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);

  • #7
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    PHP Code:
    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):

    PHP Code:
    $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
    Last edited by Ahri; 03-10-2010 at 02:41 PM. Reason: Pretified post with PHP tags
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •