//最多产生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