php搜索页码

时间:2013-11-28 08:39:55

标签: php mysqli

我有2套代码1用于限制结果和创建额外页面1 2 3 4等 并且搜索将通过所有数据库记录来查找特定的内容。

但我似乎找不到将这两者结合起来的方法。 所以我们的目标是获得一个显示有限数量结果的搜索,并为其余部分制作额外的页面。我试图将它们组合起来但没有成功。

这是我的分页代码:

if(isset($_GET['page'])){
$page = (int) $_GET['page'];
}else{
$page=1;
}
if ($page < 1) $page = 1;

$numberOfPages = 6;
$resultsPerPage = 4;
$startResults = ($page - 1) * $resultsPerPage;
$numberOfRows = mysqli_num_rows($mysqli->query('SELECT title FROM advertenties'));
$totalPages = ceil($numberOfRows / $resultsPerPage);

$query = $mysqli->query("SELECT * FROM advertenties order by date desc LIMIT $startResults, $resultsPerPage");

while ($output = mysqli_fetch_assoc($query))
echo <<<EOT
    <div id="advertenties">
        <table>
            <th><a href=thread.php?thread_id={$output['advertentie_id']}> {$output['title']} </a></th>
            <tr>
            <img src="images/{$output['images']}"><br>
            <td><p>{$output['description']}</p></td>
            </tr>
        </table>
    </div>
    EOT;
    $halfPages = floor($numberOfPages / 2);
    $range = array('start' => 1, 'end' => $totalPages);
    $isEven = ($numberOfPages % 2 == 0);
    $atRangeEnd = $totalPages - $halfPages;

    if($isEven) $atRangeEnd++;

    if($totalPages > $numberOfPages)
    {
        if($page <= $halfPages)
            $range['end'] = $numberOfPages;
        elseif ($page >= $atRangeEnd)
            $range['start'] = $totalPages - $numberOfPages + 1;
        else
        {
            $range['start'] = $page - $halfPages;
            $range['end'] = $page + $halfPages;
            if($isEven) $range['end']--;
        }
    }

    if($page > 1)
    echo '<a href="?page='.($page - 1).'">&laquo;</a>&nbsp';

    for ($i = $range['start']; $i <= $range['end']; $i++)
    {
    if($i == $page)
    echo '<strong>'.$i.'</strong>&nbsp;';
    else
    echo '<a href="?page='.$i.'">'.$i.'</a>&nbsp;';
    }
    if ($page < $totalPages)
    echo '<a href="?page='.($page + 1).'">&raquo;</a>&nbsp;';

这是我的搜索代码:

    if(isset($_GET['searchword'])){
    $result=true;
    $word="";
    $word=$_GET['searchword'];
    if(!empty($word)){
    $alles_goed=false;
    $pos='';
    }
    $resultaat=$mysqli->query("SELECT * FROM advertenties order by date desc");

    $alles_goed=true;
    $word=$mysqli->real_escape_string($_GET['searchword']);
    strtoupper($word);

    if((strlen($word)<4)&&(strlen($word)>0)){
    echo 'word moet minimaal 4 letters hebben';
    $alles_goed=false;
    }

    if($alles_goed==true){
    $wordreal= explode(" ", $word);
    while ($thread = mysqli_fetch_assoc($resultaat)) {
    $zoek_title_en_description=$thread['title'] . $thread['description'];
    foreach($wordreal as $word){
    if(!empty($word)){
    $pos=stripos($zoek_title_en_description,$word);
    }
    }
    if($pos!==false){
    echo <<<EOT
        <div id="advertenties">
            <table>
                <th><a href=thread.php?thread_id=      {$thread['advertentie_id']}> {$thread['title']}</a></th>
                <tr>
                <img src="images/{$thread['images']}">
                <td><p>{$thread['description']}</p></td>
                </tr>
            </table>
        </div>
        EOT;
        $_SESSION['idcheck']=$thread['advertentie_id'];
        }else{
        $result=false;
        }

        }
        }

        }else{
        $resultaat=$mysqli->query("SELECT * FROM advertenties order by date desc limit 4");
        while ($thread = mysqli_fetch_assoc($resultaat)) {
        echo <<<EOT
            <div id="advertenties">
                <table>
                    <th><a href=thread.php?thread_id={$thread['advertentie_id']}> {$thread['title']}</a></th>
                    <tr>
                    <img src="images/{$thread['images']}">
                    <td><p>{$thread['description']}</p></td>
                    </tr>
                </table>
            </div>
            EOT;
            }
            }

0 个答案:

没有答案