Professional Web Applications Themes

Auto-increment numbers and letters possible? - PHP Development

Is there a way to auto-increment mixed lower and upper case letters, and numbers? I'd like to start from 1 to ZZZZZZZ. That would combine 10 numbers + 26 lower-case letters + 26 upper-case letters. I think it's pretty tricky but there's gotta be a way... -- Thanks, Justin. http://www.opera.com/mail/...

  1. #1

    Default Auto-increment numbers and letters possible?

    Is there a way to auto-increment mixed lower and upper case letters, and
    numbers?

    I'd like to start from 1 to ZZZZZZZ. That would combine 10 numbers + 26
    lower-case letters + 26 upper-case letters.

    I think it's pretty tricky but there's gotta be a way...

    --
    Thanks,

    Justin.
    http://www.opera.com/mail/
    Justin Guest

  2. #2

    Default Re: Auto-increment numbers and letters possible?

    On Thu, 23 Jun 2005 17:37:39 -0300, "Justin Sane" <net> wrote:
     

    Use base_convert to convert to base36.

    <?php
    for ($i=0; $i<46656; $i++)
    echo base_convert($i, 10, 36). "<br>";
    ?>

    --
    Andy Hassall / <co.uk> / <http://www.andyh.co.uk>
    <http://www.andyhsoftware.co.uk/space> Space: disk usage ysis tool
    Andy Guest

  3. #3

    Default Re: Auto-increment numbers and letters possible?

    Em Thu, 23 Jun 2005 21:06:09 -0000, Andy Hassall <co.uk>
    escreveu:
     

    Very Good...

    Sorry, but I am curious. And I'd like to know how this work. I still did
    not undertand very well.

    --

    Thanks,

    Marcos José Setim
    ________________________________________
    Usando o M2 do Opera: http://www.opera.com/m2/
    Marcos Guest

  4. #4

    Default Re: Auto-increment numbers and letters possible?

    Justin Sane wrote:
     [/ref]

    Andy Hassall wrote:
     

    I suppose Justin meant base 62 (treat 'a' and 'A' differently) and base_convert
    does not do it (it treats 'a' and 'A' same, giving only 36 unique digits).

    Something like the code below should work for conversion between INT and base
    62 (but there are faster and better ways). The problem is that ZZZZZZZ
    represents 3521614606207 which is greater than max 32 bit signed int value
    (2147483647). With this you could reach only number 2lkCB1 (in base 62).

    <?php
    error_reporting( E_ALL );

    $b62 = array(
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
    'u', 'v', 'w', 'x', 'y', 'z',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
    'U', 'V', 'W', 'X', 'Y', 'Z'
    );
    $rev_b62 = array(
    '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
    'a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17, 'i' => 18, 'j' => 19,
    'k' => 20, 'l' => 21, 'm' => 22, 'n' => 23, 'o' => 24, 'p' => 25, 'q' => 26, 'r' => 27, 's' => 28, 't' => 29,
    'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33, 'y' => 34, 'z' => 35,
    'A' => 36, 'B' => 37, 'C' => 38, 'D' => 39, 'E' => 40, 'F' => 41, 'G' => 42, 'H' => 43, 'I' => 44, 'J' => 45,
    'K' => 46, 'L' => 47, 'M' => 48, 'N' => 49, 'O' => 50, 'P' => 51, 'Q' => 52, 'R' => 53, 'S' => 54, 'T' => 55,
    'U' => 56, 'V' => 57, 'W' => 58, 'X' => 59, 'Y' => 60, 'Z' => 61
    );

    function b10to62( $i )
    {
    global $b62;
    $i = IntVal( $i );
    if ($i==0)
    return '0';
    if ($i < 0)
    {
    $sgn = '-';
    $i = -$i;
    }
    else
    $sgn = '';
    $result = '';
    while ($i)
    {
    $result = $b62[ $i % 62 ] . $result;
    $i = IntVal( $i / 62 );
    }
    return $sgn . $result;
    }

    function b62to10( $s )
    {
    global $rev_b62;
    $s = trim( $s );
    $result = 0;
    if ($s{0}=='-')
    {
    $sgn = -1;
    $s = substr( $s, 1 );
    }
    else
    $sgn = 1;
    $len = strlen( $s );
    for( $i=0; $i < $len; $i++ )
    {
    if (!isset( $rev_b62[$s{$i}] ))
    {
    trigger_error( 'Unknown char: ' . $s{$i}, E_USER_ERROR );
    return NULL;
    }
    $result *= 62;
    $result += $rev_b62[$s{$i}];
    }
    return $sgn * $result;
    }

    set_time_limit( 0 );
    for( $i = 0; $i <= 2147483647; $i++ )
    {
    printf( "%11d:%9s:%11d\n", $i, b10to62( $i ), b62to10( b10to62( $i ) ) );
    flush();
    }

    ?>





    To do incrementation you do not need conversion.

    <?php

    $b62 = array(
    // as above
    );
    $rev_b62 = array(

    // as above
    );

    function increment( $b62num, $incr_by_int )
    {
    global $b62, $rev_b62;
    $b62num = trim( $b62num );
    if ($b62num == '')
    {
    trigger_error( 'Wrong parameter.', E_USER_ERROR );
    return NULL;
    }
    $last_digit = $rev_b62[ substr( $b62num, -1, 1 ) ] + IntVal( $incr_by_int );
    $incr_by_int = IntVal( $last_digit / 62 );
    $last_digit = $b62[ $last_digit % 62 ];
    $b62num = substr( $b62num, 0, -1 );
    if (!$incr_by_int)
    return $b62num . $last_digit;
    if ($b62num == '')
    $b62num = '0';
    return increment( $b62num, $incr_by_int ) . $last_digit;
    }

    $txt = '00000';
    $step = 63;
    while (strlen($txt) < 8)
    {
    echo $txt . "\n";
    $txt = increment( $txt, $step );
    }

    ?>

    The "b62num" param can't be less than '0' but can be as long
    (as big) as you want. The "incr_by_int" should be between zero
    and (MAX_INT - 61).
    It has one feature: it keeps leading zeros.
    It should be easy to adapt it to support decrementing
    ($incr_by_int < 0).


    Hilarion
    Hilarion Guest

  5. #5

    Default Re: Auto-increment numbers and letters possible?

    Andy Hassall wrote:
     [/ref]

    Marcos wrote:
     

    How what works? The function "base_convert" takes a string (first param)
    representing a number with given base (in second param) and converts
    it to a string representing same number in another base (given in third
    param).

    executing this | means this
    --------------------------------+------------------------------------
    base_convert( '1234', 10, 2 ) | convert decimal 1234 to binary
    base_convert( '1010', 2, 8 ) | convert binary 1010 to octal
    base_convert( '1FEA', 16, 2 ) | convert hexadecimal 1FEA to binary

    You can use any base between 2 and 36. The base_convert uses decimal digits
    from '0' to '9' and base letters (from 'A' to 'Z' - it does not matter
    if letter is capital or not) giving them values from 0 to 9 (for digits)
    and from 10 to 35 (for letters). This 36 digit set is truncated for
    the specified base, so for binary input / output we have 0 and 1, for
    octal - 0, 1, 2, 3, 4, 5, 6, 7, for decimal from 0 to 9, for hexadecimal
    - 0..9, A, B, C, D, E, F.

    The first parameter is converted to string, so base_convert( 1010, 2, 8 )
    gives same result as base_convert( '1010', 2, 8 ).


    Hilarion
    Hilarion Guest

  6. #6

    Default Re: Auto-increment numbers and letters possible?

    Hilarion wrote:
     

    The above line in "increment" function should be replaced with lines:

    $last_digit = substr( $b62num, -1, 1 );
    if (!isset($rev_b62[ $last_digit ]))
    {
    trigger_error( Unknown char: ' . $last_digit, E_USER_ERROR );
    return NULL;
    }
    $last_digit = $rev_b62[ $last_digit ] + IntVal( $incr_by_int );

    to turn on $b62num value validation.


    Hilarion
    Hilarion Guest

  7. #7

    Default Re: Auto-increment numbers and letters possible?

    Hilarion wrote:
     

    To generate $rev_b62 array one could use array_flip function,
    but using it each time the page is loaded could slow things
    down a bit so I used the function result in the code.


    Hilarion
    Hilarion Guest

  8. #8

    Default Re: Auto-increment numbers and letters possible?

    Hilarion wrote:
     
    >
    > The above line in "increment" function should be replaced with lines:
    >
    > $last_digit = substr( $b62num, -1, 1 );
    > if (!isset($rev_b62[ $last_digit ]))
    > {
    > trigger_error( Unknown char: ' . $last_digit, E_USER_ERROR );
    > return NULL;
    > }
    > $last_digit = $rev_b62[ $last_digit ] + IntVal( $incr_by_int );
    >
    > to turn on $b62num value validation.[/ref]

    But it validates only the digits it's changing so increment( '*&%&000', 36 )
    would raise no error.


    Hilarion
    Hilarion Guest

Similar Threads

  1. Auto increment VALUE attribute extension
    By arzo2000 in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 1
    Last Post: November 11th, 05:55 PM
  2. auto-increment
    By tony in forum MySQL
    Replies: 2
    Last Post: October 6th, 05:17 PM
  3. Auto Increment increase by 100 at a time?
    By Gary@garywhittle.co.uk in forum MySQL
    Replies: 2
    Last Post: November 23rd, 04:50 PM
  4. How to Handle Primary Key Auto Increment?
    By tbemcf14 in forum Coldfusion - Advanced Techniques
    Replies: 2
    Last Post: March 22nd, 09:47 PM
  5. id after auto increment from a form
    By Faith in forum ASP
    Replies: 7
    Last Post: August 6th, 05:18 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139