Code review - PHP syntax error unexpected $end [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
Hey guys! I keep getting a syntax error (unexpected $end), and I've isolated it to this chunk of code. I can't for the life of me see any closure issues. It's probably something obvious but I'm going nutty trying to find it. Would appreciate an additional set of eyes.
function generate_pagination( $base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE )
{
global $lang;
if ( $num_items == 0 )
{
}
else
{
$total_pages = ceil( $num_items / $per_page );
if ( $total_pages == 1 )
{
return "";
}
$on_page = floor( $start_item / $per_page ) + 1;
$page_string = "";
if ( 8 < $total_pages )
{
$init_page_max = 2 < $total_pages ? 2 : $total_pages;
$i = 1;
for ( ; $i < $init_page_max + 1; ++$i )
{
$page_string .= $i == $on_page ? "<font face='verdana' size='2'><b>[{$i}]</b></font>" : "{$i}";
if ( $i < $init_page_max )
{
$page_string .= ", ";
}
}
if ( 2 < $total_pages )
{
if ( 1 < $on_page && $on_page < $total_pages )
{
$page_string .= 4 < $on_page ? " ... " : ", ";
$init_page_min = 3 < $on_page ? $on_page : 4;
$init_page_max = $on_page < $total_pages - 3 ? $on_page : $total_pages - 3;
$i = $init_page_min - 1;
for ( ; $i < $init_page_max + 2; ++$i )
{
$page_string .= $i == $on_page ? "<font face='verdana' size='2'><b>[{$i}]</b></font>" : "{$i}";
if ( $i < $init_page_max + 1 )
{
$page_string .= ", ";
}
}
$page_string .= $on_page < $total_pages - 3 ? " ... " : ", ";
}
else
{
$page_string .= " ... ";
}
$i = $total_pages - 1;
for ( ; $i < $total_pages + 1; ++$i )
{
$page_string .= $i == $on_page ? "<font face='verdana' size='2'><b>[{$i}]</b></font>" : "{$i}";
if ( $i < $total_pages )
{
$page_string .= ", ";
}
}
continue;
}
}
else
{
do
{
$i = 1;
for ( ; $i < $total_pages + 1; ++$i)
{
$page_string .= $i == $on_page ? "<font face='verdana' size='2'><b>[{$i}]</b></font>" : "{$i}";
if ( $i < $total_pages )
{
$page_string .= ", ";
break;
}
}
} while (0);
if ( 1 < $on_page )
{
$page_string = " <font size='2'>"."«"."</font> ".$page_string;
}
if ( $on_page < $total_pages )
{
$page_string .= " <font size='2'>"."»"."</font>";
}
$page_string = "Pages ({$total_pages}):"." ".$page_string;
return $page_string;
}
}

The code around while (0); followed by if (1 < $on_page) looks wrong to me. At a glance it looks like the else is not closed. Have you tried php -l (lint) on your code?

Put a } in the last line of your code. You are just not closing the body of your function.
Of course it could also be that you are missing a closing bracket somewhere in between.
But as we don't know how the code works (i.e. we don't know when which block should be executed), you should intend it correctly from the beginning and look over it again.

You need a } at the end of the file to close the function body.

You are missing a closing } for the do statement starting on line 65.
Replace that statement with the following text to repair it.
do
{
$i = 1;
for ( ; $i < $total_pages + 1; ++$i)
{
$page_string .= $i == $on_page ? "<font face='verdana' size='2'><b>[{$i}]</b></font>" : "{$i}";
if ( $i < $total_pages )
{
$page_string .= ", ";
break;
}
}
} while (0);

your indentation inside the do ... while(0) code is off. If you look at the for loop, you'll notice that everything inside it is one indentation less than it should be.
Add this indentation, and you'll see that what the others mentioned (extra } at the end of the file) is the problem.

Related

Pagination with part in php

I try to implement a pagination to my website in php
This is what I want
If I have 42 Pages and I am on the page 6
[1][2][3][4][5][6][7][8][9][10][20][30][40][42]
or I am on the page 23
[1][10][20][21][22][23][24][25][26][27][28][29][30][40][42]
This what I did
$nbPage = 42;
$actualPage = 23;
$min_part_pagination = floor($actualPage / 10)*10;
$max_part_pagination = ceil($actualPage / 10 )*10;
$last_part_pagination = floor($nbPage /10 )*10;
$first_part_pagination = 10;
$count = 1;
for($j = 1 ; $j <= $nbPage ; $j++){
if($j % 10){
if($pageActuelle == $count){
$max_part_pagination = ($max_part_pagination < $nbPage) ? $max_part_pagination : $nbPage+1;
for($k = $min_part_pagination+1 ; $k < $max_part_pagination ; $k++ ){
if($k == 1){
echo "<a href=http://dev.blablabl.fr/public_html/>".$k."</a> ";
}else{
echo "".$k." ";
}
}
}
elseif($count == $nbPage && $pageActuelle < $last_tranche_pagination ){
echo "".$count." ";
}elseif($count == 1 && $pageActuelle >= $first_tranche_pagination ){
echo "<a href=http://dev.blablabl.fr/public_html/>1</a> ";
}
}else{
if($pageActuelle == $count){
if($max_part_pagination == $max_part_pagination ){
$max_part_pagination += 10;
}else{
$max_part_pagination = $last_tranche_pagination;
}
echo 'min_part_pagination > '.$min_part_pagination.'<br />';
echo 'max_part_pagination > '.$max_part_pagination.'<br />';
for($k = $min_part_pagination ; $k < $max_part_pagination ; $k++){
echo "".$k." ";
}
}else{
echo "".$count." ";
}
}
My problem is If I am on the page 41 It show [40][41][42][43][44][45][46][47][48][49][50]
It doesn't stop at 42

PHP pagination loop issue

I tried to make a PHP pagination like this:
$count = $this->dataBaseFunctions->countItems();
$resultperPage = 10;
$offset = 0;
$adjacents = 3;
$totalPages = ceil(intval($count) / $resultperPage);
if (isset($_GET['offset'])) {
$offset = trim($_GET['offset']);
}
$j = $adjacents;
while ($j > 0) {
if (($offset / 10) - 1 > 0) {
echo '' . ($offset - 10 - ($j * 10)) . ' ';
}
$j--;
}
/*for ($j = $adjacents; $j > 0; $j--) {
echo '' . ($offset - 10 - ($j * 10)) . ' ';
}*/
echo '[' . ($offset - 10) . '] ';
for ($j = 1; $j < ($adjacents + 1); $j++) {
echo '' . ($offset - 10 + ($j * 10)) . ' ';
}
The result should look like this (Brackets = current clicket page):
[0] 1 2 3
When I click "3" it should look like this (works so far):
0 1 2 [3] 4 5 6
But when I click "1" the result looks like this:
-2 -1 0 [1] 2 3 4
The "0" is not a problem, I just 'echoed' it for debugging.
I know that I dont set the right conditions for not showing pages below 1 but I dont get it how to make it right. Seems like I have tomatoes on my eyes... The echo is just for testing - later I save the whole pagination links into a variable to assign it to the bottom of the page. Its a bit of debugging code you see here but it shows what I try to do. Please let me know if I can improve the question or if there are missing informations like: "why you do ($offset / 10) -1 > 0" ;-)
Later I need to do same stuff in JS but I think I can manage that, when I know what I did wrong in the PHP version.
*Maybe it's a bad Idea what I try to do there?
This one should do the trick:
Now including the $adjacents
$count = $this->dataBaseFunctions->countItems();
$resultperPage = 10;
$offset = 0;
$adjacents = 3;
$totalPages = ceil(intval($count) / $resultperPage);
if (isset($_GET['offset'])) {
$offset = trim($_GET['offset']);
}
$start = ($offset-$adjacents) > 0 ? ($offset-$adjacents) : 0;
$end = ($offset+$adjacents) < $totalPages ? ($offset+$adjacents) : $totalPages;
$pager = "";
for ($i = 0; $i < $totalPages; $i++) {
if ($i >= $start && $i <= $end) {
$pager .= $offset != $i ? '' . $i . ' ' : "[$i] ";
}
}
echo $pager;

Easy to Use Pagination

I am trying to get a nice paginator to work but I'm having trouble with getting the upcoming/middle numbers to work properly.
The goal is to show the first and last 5 pages of a result set regardless of what page your on, but these first and last 5 can only show if there are enough pages to allow it.
The pagination would look something like this: << 1 2 3 4 5 - 12 13 14 - 78 79 80 81 82 >>
With only a few pages: << 1 2 3 4 5 6 7 8 9 10 >>
And how to avoid this?: << 1 2 3 4 5 - 5 6 7 - 7 8 9 10 11 >>
My code so far is:
function pagination_links($page, $num_rows, $results_per_page, $each_direction = 3)
{
$total_pages = $num_rows ? ceil($num_rows / $results_per_page) : 1 ;
if($total_pages < 2)
{
return null;
}
$page = ((is_numeric($page)) && ($page >= 1) && ($page <= $total_pages)) ? (int)$page : 1 ;
$output = null;
if($page > 1)
{
$output .= '<div class="pageBtn"><<</div>' ;
}
else
{
$output .= '<div class="pageBtnDis"><<</div>' ;
}
for($i=1;$i<$total_pages;$i++)
{
if($page != $i)
{
$output .= '<div class="pageBtn">' . $i . '</div>' ;
}
else
{
$output .= '<div class="pageBtnSet">' . $i . '</div>' ;
}
if($i > 4)
{
break ;
}
}
for($i = $page - $each_direction; $i <= $page + $each_direction; $i++)
{
if(($i > 5) && ($i <= $total_pages-5))
{
if($page != $i)
{
$output .= '<div class="pageBtn">' . $i . '</div>' ;
}
else
{
$output .= '<div class="pageBtnSet">' . $i . '</div>' ;
}
}
}
for($i = $total_pages-5;$i<$total_pages;$i++) {
if($page != $i)
{
$output .= '<div class="pageBtn">' . $i . '</div>' ;
}
else
{
$output .= '<div class="pageBtnSet">' . $i . '</div>' ;
}
}
if($page < $total_pages)
{
$output .= '<div class="pageBtn">>></div>' ;
}
else
{
$output .= '<div class="pageBtnDis">>></div>' ;
}
return $output ;
}
I think the current best solution is to use Digg Style Pagination Class. It greatly simplifies the creation and styling of your pagination markup.
The Pager PEAR class is highly customizable and I've had a lot of success with it.

Part of pascal function

I'm trying to rewrite a pascal program to PHP, and don't understand what this part of pascal function do:
while (u[3] <> 1) and (u[3]<>0) and (v[3]<>0)do
begin
q:=u[3] div v[3];
for i:=1 to 3 do
begin
t:=u[i]-v[i]*q;
u[i]:=v[i];
v[i]:=t;
{writeln('u',i,'=',u[i],' v',i,'=',v[i]); }
end;
end;
if u[1]<0 then u[1]:=n+u[1];
rae:=u[1];
Please help to rewrite it to PHP.
Thanks.
A very literal translation of that code, should be this one:
while ($u[3] != 1 && $u[3] != 0 && $v[3] != 1 )
{
$q = floor($u[3] / $v[3]);
for ($i = 1; $i <= 3; $i++)
{
$t = $u[$i] - $v[$i] * $q;
$u[$i] = $v[$i];
$v[$i] = $t;
//writeln('u',i,'=',u[i],' v',i,'=',v[i]);
}
}
if ($u[1] < 0 )
$u1] = $n + $u[1];
$rae = $u[1];
Of course, u and v are arrays. Sorry for not giving any more info, but it's been like 10 years since Pascal and I last saw each other, but we had a profound romance for a long time, since I feel inlove for to hotties(C# and PHP) :)
while ($u[3] != 1) && ($u[3] != 0) && ($v[3] != 0) {
$q = floor($u[3] / $v[3]);
for ($i = 1; $i <= 3; $i++) {
$t = $u[$i] - $v[$i] * $q;
$u[$i] = $v[$i];
$v[$i] = $t;
echo "u$i={$u[$i]} v$i={$v[$i]}\n";
}
}
if ($u[1] < 0) {
$u[1] = $n + $u[1];
}
$rae = $u[1];
2 small corrections to David's code:
while ($u[3] != 1 && $u[3] != 0 && $v[3] != 1 )
should be
while ($u[3] != 1 && $u[3] != 0 && $v[3] != 0 )
and
for ($i = 1; $i < 3; $i++)
i never reaches the value of 3
for ($i = 1; $i <= 3; $i++)
May be the Writeln can be translated to
echo 'u'.$i.'='.$u[$i].' v'.$i.'='.$v[$i];
When you do the translation of arrays, take into account that arrays in php uses 0 as the first index.
$u= array( 3, 5, 22 )
echo u[1]; // prints 5
while($u[3] != 1 && $u[3] != 0 && $v[3] != 0)
{
$q = ($u[3] - ($u[3] % $v[3]) ) / $v[3]; //just the same as floor($u[3]/$v[3]), but i want to use % here :)
for ($i = 1; $i <= 3; $i++)
{
$t = $u[$i] - $v[$i]*$q;
$u[$i] = $v[$i];
$v[$i] = $t;
echo '<br />u'.$i.'='.$u[$i].' v'.$i.'='.$v[$i];
}
}
if ($u[1] < 0) $u[1] = $n + $u[1];
$rae = $u[1];
I dont know pascal But i have tried :)
while ($u[3]!=1 && $u[3]!=0 && $v[3]!=0) [
$q=floor($u[3]/ $v[3]);
for ($i=1;$i<3;$i++) {
$t=$u[$i]-$v[$i]*$q;
$u[$i]=$v[$i];
$v[$i]=$t;
echo "u".$i."=".$u[$i]."v".$i."=".$v[$i];
}
if ($u[1]<0) {
$u[1]=$n+$u[1];
}
$rae=$u[1];
In php variable Name Start With $
No Begin End used here in php only braces :)

php pagination numbered page links

This is a pagination code used for the navigation, any ideas how to get this code to display simply a numbered list of the pages as links?
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
}
else {
$pageno = 1;
}
if(isset($_GET['niche']))
{
$query = "SELECT count(*) FROM studies WHERE niche = '{$_GET['niche']}'";
$result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR);
}
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
$rows_per_page = 4;
$lastpage = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
}
if ($pageno < 1) {
$pageno = 1;
} // if
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT * FROM studies WHERE niche = '{$_GET['niche']}' $limit";
$result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR);
and...
if ($pageno == 1) {
echo "<div class='container'>FIRST PREV ";
} else {
echo "<div class='container'> <a href='{$_SERVER['PHP_SELF']}?pageno=1&niche={$_GET['niche']}'>FIRST</a> ";
$prevpage = $pageno-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage&niche={$_GET['niche']}'>PREV</a> ";
} // if
echo " ( Page $pageno of $lastpage ) ";
if ($pageno == $lastpage) {
echo " NEXT LAST</div><br />";
} else {
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&niche={$_GET['niche']}'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage&niche={$_GET['niche']}'>LAST</a></div><br /> ";
} // if
?>
Try this:
$totalpages = ceil($numrows / $rows_per_page);
if($totalpages >= 1){ $pagelinkcount = 1; } else { $pagelinkcount = 0; }
while($pagelinkcount <= $totalpages && $totalpages > 1) {
echo "{$pagelinkcount} ";
$pagelinkcount++;
}
On a side note, as Ian Elliot pointed out in the comments for your question, using $_GET in an SQL query leaves your database VERY vulnerable, and is thus considered an extremely insecure coding practice. You should escape and parse the $_GET data that you need diligently before passing it to the DB.
Here's a function I've been using for pagination for a while. It returns nothing if there's only one page, returns up to 15 pages with numbers, then adds a dropdown that lets you skip to any 10th page when there are more than 15 pages. It relies on some prev/next images, but you can easily take that out.
function paginate( $items_per_page, $number_of_results ) {
if( isset( $_REQUEST['page'] ) ) {
$page = $_REQUEST['page'];
} else {
$page = 1;
}
$url = htmlentities( preg_replace( '/(\?|&)page=[\d]+/', '', $_SERVER['REQUEST_URI'] ).'&' );
$html = '';
$numbers_html = '';
$navigation_html = '';
if( $number_of_results > $items_per_page ) {
$html .= '<div class="pagination">';
if( $page == 1 or $page == '1' ) {
$numbers_html .= '<img src="images/prev.png" alt="← prev" class="inactive" /> - ';
} else {
$numbers_html .= '<img src="images/prev.png" alt="← prev" /> - ';
}
$count = 0;
$total_pages = ceil( $number_of_results / $items_per_page )-1;
while( $count <= $total_pages ) {
$count++;
if( $total_pages > 12 and floor($count / 10) != floor($page / 10) ) {
while( $count < $total_pages and floor($count / 10) != floor($page / 10) ) {
if( $count == 1 ) {
$endpage = 9;
} elseif( $count + 9 < $total_pages ) {
$endpage = $count + 9;
} else {
$endpage = $total_pages + 1;
}
$ten_group = floor( $count / 10 );
if( $ten_group == 0 ) {
$navigation_html .= '<option value="'.$url.'page='.$count.'">page 1</option>';
} else {
$navigation_html .= '<option value="'.$url.'page='.$count.'">page '.($ten_group*10).'</option>';
}
$count += 10;
}
$count -= 2;
} else {
if( $page == $count ) {
$numbers_html .= '<span class="current">'.$count.'</span>';
if( $count == 1 ) {
$endpage = 9;
} elseif( $count + 9 < $total_pages ) {
$endpage = $count + 9;
} else {
$endpage = $total_pages + 1;
}
if( $total_pages > 15 ) {
$ten_group = floor( $count / 10 );
if( $ten_group == 0 ) {
$navigation_html .= '<option value="'.$url.'page='.$count.'" selected="selected">page 1</option>';
} else {
$navigation_html .= '<option value="'.$url.'page='.$count.'" selected="selected">page '.($ten_group*10).'</option>';
}
}
} else {
$numbers_html .= ''.$count.'';
}
if( ( $total_pages > 12 and $count % 10 == 9 ) or $count == $total_pages+1 ) {
} else {
$numbers_html .= ' - ';
}
}
}
if( $page != $count ) {
$numbers_html .= ' - <img src="images/next.png" alt="next →" />';
} else {
$numbers_html .= ' - <img src="images/next.png" alt="next →" class="inactive"/>';
}
$count++;
$html .= '<div class="pagination_numbers">'.$numbers_html.'</div>';
if( $navigation_html ) {
$html .= '<div class="pagination_navigation">skip to: <select onchange="window.location=this.value">'.$navigation_html.'</select> of '.($total_pages+1).'</div>';
}
$html .= '</div>';
}
return $html;
}
If you have many pages to display, you might want to consider "logarithmic" page naviagtion, as I describe in my answer here:
How to do page navigation for many, many pages? Logarithmic page navigation
(Sample PHP code only handles the pagination display - not the DB query).

Categories