0

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>" : "<a href=\"".$base_url."&amp;offset=".( $i - 1 ) * $per_page."\">{$i}</a>";
                    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>" : "<a href=\"".$base_url."&amp;offset=".( $i - 1 ) * $per_page."\">{$i}</a>";
                            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>" : "<a href=\"".$base_url."&amp;offset=".( $i - 1 ) * $per_page."\">{$i}</a>";
                        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>" : "<a href=\"".$base_url."&amp;offset=".( $i - 1 ) * $per_page."\">{$i}</a>";
                    if ( $i < $total_pages )
                    {
                        $page_string .= ", ";
                        break;
                    }
                } 
            } while (0);
            if ( 1 < $on_page )
            {
                $page_string = " <font size='2'><a href=\"".$base_url."&amp;offset=".( $on_page - 2 ) * $per_page."\">"."&laquo;"."</a></font>&nbsp;&nbsp;".$page_string;
            }
            if ( $on_page < $total_pages )
            {
                $page_string .= "&nbsp;&nbsp;<font size='2'><a href=\"".$base_url."&amp;offset=".$on_page * $per_page."\">"."&raquo;"."</a></font>";
            }
            $page_string = "Pages ({$total_pages}):"." ".$page_string;
            return $page_string;
        }
    }
dtufano
  • 21
  • 1
  • a simple copy-paste script to my editor shown that you missed the last '}'. I suggest you to use a decent editor with syntax highlighting and symbol ( '(', '{', '[' ) support. On windows you can use notepad++ for free. On linux, there's a bunch of great editor, like: geany, gedit, kate, etc. – ariefbayu Mar 30 '10 at 07:39
  • *(sidenote)* Your code is doing way too too much. Try to describe the function. For each 'and' take the corresponding code block and make it into a smaller separate function. That will make it much more maintainable. – Gordon Mar 30 '10 at 08:15

5 Answers5

4

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?

Martin Wickman
  • 19,662
  • 12
  • 82
  • 106
2

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.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

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

codaddict
  • 445,704
  • 82
  • 492
  • 529
1

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>" : "<a href=\"".$base_url."&amp;offset=".( $i - 1 ) * $per_page."\">{$i}</a>";
        if ( $i < $total_pages )
        {
            $page_string .= ", ";
            break;
        }
    }
} while (0);
Seidr
  • 4,946
  • 3
  • 27
  • 39
  • Edited, closing bracket that was missing was infact for the do statement, not the for loop. Check your indentation – Seidr Mar 30 '10 at 07:46
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.

Faisal
  • 19,358
  • 4
  • 30
  • 33