views:

123

answers:

5

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;
        }
    }
+1  A: 

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
+2  A: 

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
+1 just check the indentation for the for loop and you should be fine..
pinaki
As the indentation seems to be broken in this part of the code, we can only guess...
Felix Kling
Thank you, thank you! I've been staring at it for the past few hours going nutty.
dtufano
A: 

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

codaddict
A: 

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
Edited, closing bracket that was missing was infact for the do statement, not the for loop. Check your indentation
Seidr
A: 

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