分页

时间:2016-04-16 18:03:30

标签: php mysql mysqli pagination

我在网页的PHP部分中遇到for loop的小问题,并且包含在嵌套的if语句中。我不确定为什么这个for loop会抛出错误。

有问题的for loop如下;

for($i = $page_number-4; $i < $page_number; $i++)
            {
                if($i > 0){
                    $pagination_controls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
                }
            }

我得到的错误是

( ! ) Parse error: syntax error, unexpected 'for' (T_FOR) in /home/40082562/public_html/Courses/SearchResultsPage.php on line 54

我不确定为什么会发生这种错误以及为什么for循环是个问题。

任何帮助将不胜感激,谢谢。

我的代码在下面;

<meta charset="UTF-8">
<?php
    $mysqli = new mysqli('localhost', 'user', 'password','db');
    if ($mysqli->connect_errno) 
    {
        die('Database connection failed');
    }
    //$m->set_charset('utf8');
    $search_course = "
    SELECT title, summary, id
    FROM course
    WHERE title LIKE '%".$_POST['searchBar']."%'";
    $result = $mysqli->query($search_course) or die($mysqli->error);
    $search_result = $result->fetch_assoc();
    $row = mysql_fetch_row($result);
    //total rows for search
    $rows = $row[0];
    //number of results per page
    $rows_per_page = 10;
    //shows last page
    $last_page = ceil($rows/$rows_per_page);
    if($last_page < 1){
        $last_page = 1;
    }
    $page_number = 1;
    if(isset($_GET['pn'])){
        $page_number = preg_replace('#[^0-9]#', '', $_GET['pn']);
    }
    //makes sure page number is between limits of $page_number
    if($page_number < 1){
        $page_number = 1;
    } else if($page_number > $last_page){
        $page_number = $last_page;
    }

    // sets the value of items to view
    $limit = 'LIMIT ' .($page_number -1) * $rows_per_page .',' .$rows_per_page;

    // query again only grabbing the set number of rows depending on page number
    $search_course = "
    SELECT title, summary, id
    FROM course
    WHERE title LIKE '%".$_POST['searchBar']."%'
    ORDER BY title DESC $limit";
    $result = $mysqli->query($search_course) or die($mysqli->error);
    $search_result = $result->fetch_assoc();
    //displays to the user the total number of results and the page numbers
    $total_number_of_results = "Search Results (<b>$rows</b>)";
    $page_user_is_on = "Page <b>$page_number</b> of <b>$last_page</b>";
    //set up pagination
    $pagination_controls = '';
    if($last_page != 1){
        if($page_number > 1){
            $previous = $page_number - 1;
            $pagination_controls .='<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">previous</a> &nbsp; &nbsp; '
            for($i = $page_number-4; $i < $page_number; $i++)
            {
                if($i > 0){
                    $pagination_controls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
                }
            }
        }
        $pagination_controls.=''.$page_number.' &nbsm; ';
        //clickable links to the left
        for($i = $page_number+1; $i <= $last_page; $i++)
            {
                $pagination_controls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
                if($i >= $page_number+4){
                    break;
                }
            }
            if($page_number != $last){
                $next = $page_number + 1;
                $pagination_controls.='&nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a>';
            }
    }
    $list = '';
    while($row = mysql_fetch_array($search_result, MYSQL_ASSOC)){
        $title = $row['title'];
        $id = $row['id'];
        $list.='<p><a href="SearchResultsPage.php?title='.$title.'">'.$summary.' </p>'
    }
    mysqli_close($mysqli);
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <link rel='stylesheet' href='courses.css'>
</head>
<body>
    <div class="header">
        <h1>Search Results for - <?= $_POST['searchBar'] ?></h1>
    </div>
    <div>
        <p><?php echo $list; ?></p>
        <div id="pagnation_controls"><?php echo $pagnation_controls; ?>
        </div>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您在上一行末尾缺少;分号。

这就是你收到错误的原因。 PHP解析器期望你会结束该行,附加一些其他字符串或变量,但不能在同一行中启动for循环。