从字符串中检索字符实现翻页

<?php

$str = <<<EOF
    生命的力量|23
    我们的世界|45
    生命的活力|67
    力量的源泉|14
    生命|11
    热爱生命|12
    伟大的生命|15
EOF;

$arr = explode("\n", $str);
$word = $_REQUEST['word'];
$newarr = array();
if ($word) {
    foreach ($arr as $val) {
        if (strpos($val, $word) !== false) {
            $newarr[] = $val;
        }
    }
} else {
    $newarr = $arr;
}
$count = count($newarr);
$perpage = 2;
$page = intval($_GET['page']) > 0 ? intval($_GET['page']) : 1;
$start = ($page - 1) * $perpage;


$datalist = array_slice($newarr, $start, $perpage);
var_dump($datalist);


$mpurl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $_SERVER['SCRIPT_NAME'] . '?word=' . urlencode($word);

// 复杂点的分页
echo multi($count, $perpage, $page, $mpurl);

// 简单的分页
//$num = 0;
//if (isset($newarr[$start + $perpage])) {
//    $num = $perpage;
//}
//echo simplepage($num, $perpage, $page, $mpurl);

//discuz simplepage function simplepage($num, $perpage, $curpage, $mpurl) { $return = ''; $lang['next'] = '下一页'; $lang['prev'] = '上一页'; $next = $num == $perpage ? '<a href="' . $mpurl . '&amp;page=' . ($curpage + 1) . '" class="nxt">' . $lang['next'] . '</a>' : ''; $prev = $curpage > 1 ? '<span class="pgb"><a href="' . $mpurl . '&amp;page=' . ($curpage - 1) . '">' . $lang['prev'] . '</a></span>' : ''; if ($next || $prev) { $return = '<div class="pg">' . $prev . $next . '</div>'; } return $return; } // discuz multi function multi($num, $perpage, $curpage, $mpurl, $maxpages = 0, $page = 10, $autogoto = FALSE, $simple = FALSE) { global $_G; $ajaxtarget = ''; $a_name = ''; if (strpos($mpurl, '#') !== FALSE) { $a_strs = explode('#', $mpurl); $mpurl = $a_strs[0]; $a_name = '#' . $a_strs[1]; } $lang['next'] = '下一页'; $lang['prev'] = '上一页'; $dot = '...'; $multipage = ''; $mpurl .= strpos($mpurl, '?') !== FALSE ? '&amp;' : '?'; $realpages = 1; $page -= strlen($curpage) - 1; if ($page <= 0) { $page = 1; } if ($num > $perpage) { $offset = floor($page * 0.5); $realpages = @ceil($num / $perpage); $pages = $maxpages && $maxpages < $realpages ? $maxpages : $realpages; if ($page > $pages) { $from = 1; $to = $pages; } else { $from = $curpage - $offset; $to = $from + $page - 1; if ($from < 1) { $to = $curpage + 1 - $from; $from = 1; if ($to - $from < $page) { $to = $page; } } elseif ($to > $pages) { $from = $pages - $page + 1; $to = $pages; } } $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="' . $mpurl . 'page=1' . $a_name . '" class="first"' . $ajaxtarget . '>1 ' . $dot . '</a>' : '') . ($curpage > 1 && !$simple ? '<a href="' . $mpurl . 'page=' . ($curpage - 1) . $a_name . '" class="prev"' . $ajaxtarget . '>' . $lang['prev'] . '</a>' : ''); for ($i = $from; $i <= $to; $i++) { $multipage .= $i == $curpage ? '<strong>' . $i . '</strong>' : '<a href="' . $mpurl . 'page=' . $i . ($ajaxtarget && $i == $pages && $autogoto ? '#' : $a_name) . '"' . $ajaxtarget . '>' . $i . '</a>'; } $multipage .= ($to < $pages ? '<a href="' . $mpurl . 'page=' . $pages . $a_name . '" class="last"' . $ajaxtarget . '>' . $dot . ' ' . $realpages . '</a>' : '') . ($curpage < $pages && !$simple ? '<a href="' . $mpurl . 'page=' . ($curpage + 1) . $a_name . '" class="nxt"' . $ajaxtarget . '>' . $lang['next'] . '</a>' : '') ; $multipage = $multipage ? '<div class="pg">' . ($shownum && !$simple ? '<em>&nbsp;' . $num . '&nbsp;</em>' : '') . $multipage . '</div>' : ''; } $maxpage = $realpages; return $multipage; } ?>

 

posted on 2013-09-13 14:04  bandbandme  阅读(283)  评论(0编辑  收藏  举报