PHP ~ 簡易~自動換頁程式

很簡易的換頁程式.........目前沒發現bug~~~~~加減用囉XD

使用前要用下javascript語法.........其實可以不用另外再放javascript~~只是我不喜歡直接寫在裡面= =

Javascript Code
function toSelectPage(e)
{
var uu = e.options[e.selectedIndex].value;
toPage('messages.php?page=' + uu);
}
function toPage(x)
{
document.location = x;
}

PHP Code
function getTablePage($table, $sql_where, $num_per_page, $link_page, $current_page)
{
$sql = "SELECT COUNT(*) as xx FROM ".$table.' '.$sql_where;
$rr = db_query($sql);
$xx = db_fetch($rr);

$total = $xx['xx'];

if(isset($current_page))
{
$page = $current_page - 1;
}
else
{
$current_page = 1;
$page = 0;
}

$page_num = intval($total / $num_per_page);
if($total > $page_num * $num_per_page)
{
$page_num++;
}


$up_page = $current_page - 1;
$down_page = $current_page + 1;


if($current_page >= $page_num)
{
$down_page = $page_num;
}
if($current_page == 1)
{
$up_page = 1;

}

$page_display = '<div align="center"><input type="button" onclick="toPage(\''.$link_page.'?page='.$up_page.'\')" value="上一頁" /><select onchange=toSelectPage(this)>';
for($i = 1; $i <= $page_num; $i++)
{
if($_GET['page'] == $i)
{
$page_display .= '<option value="'.$i.'" selected="selected">'.$i.'</option>';
}
else
{
$page_display .= '<option value="'.$i.'">'.$i.'</option>';
}
}
$page_display .= '</select><input type="button" onclick="toPage(\''.$link_page.'?page='.$down_page.'\')" value="下一頁" /></div>';

$num_from = $num_per_page * $page;
$output['page_display'] = $page_display;
$output['num_from'] = $num_from;

return $output;
}

How to user it
$num_per_page = 5;
$xx = getTablePage('messages', '', $num_per_page, 'messages.php', $_GET['page']);

$num_from = $xx['num_from'];
$sql = "SELECT * FROM messages WHERE status = '1' ORDER BY id DESC LIMIT ".$num_from.','.$num_per_page;
print $xx['page_display'];

沒有留言:

發佈留言