bdg bdg - 7 months ago 9
SQL Question

php for loop errors when paginating

so I am having a little issue with a

for loop
within my php section of a web page, and is contained within a nested if statement. I am unsure as to why this
for loop
is throwing back errors.

The
for loop
in question is below;

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


And the error I am getting is

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


I am unsure as to why this error is occurring and why the for loop is the issue.

Any help would be appreciated, Thanks.

my code is below;

<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;
//queery 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>

Answer

You're missing ; semicolon at the end of previous line.

That's why you got the error. PHP parser expects you would end the line, append some other string or variable, but not start a for loop within the same line.

Comments