[thelist] mysql primary key question..

Shashank Tripathi sub at shanx.com
Mon Jul 8 12:16:01 CDT 2002


Erick, good ideas...but I guess your best bet is to have the numeric
field as ID to be auto-sequenced by MySQL, and then generate a base36
number based on it (which you can use as a unique key etc). Following is
a code snippet for base36 function in PHP, I am adding it as a tip for
the benefit of EVOLT posterity. Hope this solves your problems..

Cheers,
-Shanx.


<tip type="Base16, Base36  and Base64 functions in PHP" author="Shashank
Tripathi (shanx at shanx.com)">

<?

/**
 * Function to calculate base36 values from a number. Very
 * useful if you wish to generate IDs from numbers.
 *
 * @param $value The number
 * @param $base The base to be applied (16, 36 or 64)
 * @return The calculated string
 * @author Shashank Tripathi (shanx at shanx.com)
 * @version 0.1 - Let me know if something doesnt work
 *
 */

function getBaseString($value, $base)
{
    $baseChars = 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'
                     );

    $remainder = 0;
    $newval = "";

    while ( $value > 0 )
    {
        $remainder = $value % $base;
        $value = ( ($value - $remainder)/ $base );
        $newval .= $baseChars[$remainder];
    }
    return strrev($newval);

}



echo "The string for 46655, for instance, is " . getBaseString(46655,
36);

?>

</tip>





More information about the thelist mailing list