2個hash table 函數



//最多產生10個表(0、1、2....9)

<pre name="code" class="php">    /**
     * @param string $table_name 表名
     * @param int $user_id 用戶id
     * @param int $total 分表總數
     * @link http://www.phpddt.com
     */
    function hash_table($table_name, $user_id, $total)
    {
        return $table_name . '_' . (($user_id % $total) + 1);
    }
     
    echo hash_table("artice", 1234, 5); //artice_5
    echo hash_table("artice", 3243, 5); //artice_4





//最多可產生99個表


    function get_hash_table($table, $userid) 
    {
        $str = crc32($userid);
        
        if ($str < 0) {
            $hash = "0" . substr(abs($str), 0, 1);
        } else {
            $hash = substr($str, 0, 2);
        }
     
        return $table . "_" . $hash;
    }
     
    echo get_hash_table('message', 'user18991'); //結果爲message_10
    echo get_hash_table('message', 'user34523'); //結果爲message_13


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章