好用的換頁程式


分 <code>頁頁碼效果

下面這個是個還蠻好用的換頁程式,搭配CSS之後,就會變成上面那張圖的樣子,當然,你可以使用CSS任意的修改它的外觀,我個人覺得,非常好用,真可以說是出外旅遊居家良伴(…我在說啥??)。

function split_page($pageRow,$current_page,$total_rows,$current_file_path) {

  $total_pages = ceil($total_rows/$pageRow);

  $pageLeft = Array();

  $pageRight = Array();

  $rightItem = 5;

  $leftItem = 4;

  $rightcnt = $rightItem;

  $leftcnt = $leftItem;
if ($current_page <= $leftcnt) { $rightcnt += $leftcnt-$current_page+1; }
  for ($rightcnt,$i=1 ; $rightcnt>0 ; $rightcnt--,$i++) {

  if (($current_page+$i) > $total_pages) {

    $leftcnt += $rightcnt;

    break;

  } else {

    $pageRight[] = $current_page + $i;

  }

}
for ($leftcnt,$i=1 ; $leftcnt>0 ; $leftcnt--,$i++) {

  if (($current_page-$i) < 1) {

    break;

  }else{

    $pageLeft[] = $current_page - $i;

  }

}

//翻轉左半邊陣列
$pageLeft = array_reverse($pageLeft);
$pagerightcnt = count($pageRight);
$pageleftcnt = count($pageLeft);
$split_html.= '<div><img src="/img/sp.gif" mce_src="/img/sp.gif" height="20"></div>';
$split_html.= '<div id="pages">';
if ($current_page != "1") {
$split_html.= '<a href="'.$current_file_path.'&page=1" mce_href="'.$current_file_path.'&page=1"> <span>第一頁</span> </a> ';
$split_html.= '<a href="'.$current_file_path.'&page='.($current_page-1).'" mce_href="'.$current_file_path.'&page='.($current_page-1).'"> <span>上一頁</span> </a> ';
}

if ($current_page > ($pageleftcnt+1)) { $split_html.= ‘<span>…</span> ‘; }

for ($x=0 ; $x<$pageleftcnt ; $x++) {
$split_html .= '<a href="'.$current_file_path.'&page='.($current_page-$pageleftcnt+$x).'" mce_href="'.$current_file_path.'&page='.($current_page-$pageleftcnt+$x).'"> '.$pageLeft[$x].' </a> ';
}

$split_html.= $current_page.' ';

for ($x=0 ; $x<$pagerightcnt ; $x++) {
$split_html .= '<a href="'.$current_file_path.'&page='.($current_page+$x+1).'" mce_href="'.$current_file_path.'&page='.($current_page+$x+1).'"> '.$pageRight[$x].' </a> ';
}

if ($current_page < ($total_pages-$pagerightcnt)) { $split_html.= '<span>...</span> '; }

if ($current_page != $total_pages && $total_rows!=0) {
$split_html.= '<a href="'.$current_file_path.'&page='.($current_page+1).'" mce_href="'.$current_file_path.'&page='.($current_page+1).'"> <span>下一頁</span> </a> ';
$split_html.= '<a href="'.$current_file_path.'&page='.$total_pages.'" mce_href="'.$current_file_path.'&page='.$total_pages.'"> <span>最末頁</span> </a> ';
}

if (($pageRow*$current_page) > $total_rows) $finalrow = $total_rows;
else $finalrow = ($pageRow*$current_page);

$split_html.= '<br><br><span>總共有 <strong>'.$total_rows.'</strong> 則資料,以上列出的是 <strong>'.($pageRow*($current_page-1)+1).'-'.$finalrow.'</strong> 則資料</span>';
$split_html.= '</div>';
$split_html.= '<div style="padding:20px;"></div>';

return $split_html;
}

程式說明:
$pageRow 為每一頁你要出現的資料筆數
$current_page 指得是目前你所在的頁碼,也就是說每次換頁的同時,你都必須傳回新的頁碼才行
$total_rows 就是你的資料總筆數
$current_file_path 這個是你目前所在網頁的網址,如果要加參數,也必須要一併寫在這裡

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s