-5

I have some errors, I cant fix the problem. Can you help me pleasee ? This is because you are using a deprecated function ? How would I fix this?

Here is the code

<?php

class Pagination {
    function getStartRow($page,$limit){
        $startrow = $page * $limit - ($limit);
        return $startrow;
    }   
    function showPageNumbers($totalrows,$page,$limit){

        $query_string = $this->queryString();

        $pagination_links = null;

        /*
        PAGINATION SCRIPT
        seperates the list into pages
        */      
         $numofpages = $totalrows / $limit; 
        /* We divide our total amount of rows (for example 102) by the limit (25). This 

    will yield 4.08, which we can round down to 4. In the next few lines, we'll 
    create 4 pages, and then check to see if we have extra rows remaining for a 5th 
    page. */

        for($i = 1; $i <= $numofpages; $i++){
        /* This for loop will add 1 to $i at the end of each pass until $i is greater 
    than $numofpages (4.08). */     

          if($i == $page){
                $pagination_links .= '<div class="page-link"><span>'.$i.'</span></div> ';
            }else{ 
                $pagination_links .= '<div class="page-link"><a href="?page='.$i.'&'.$query_string.'">'.$i.'</a></div> '; 
            }
            /* This if statement will not make the current page number available in 
    link form. It will, however, make all other pages available in link form. */
        }   // This ends the for loop

        if(($totalrows % $limit) != 0){
        /* The above statement is the key to knowing if there are remainders, and it's 
        all because of the %. In PHP, C++, and other languages, the % is known as a 
        Modulus. It returns the remainder after dividing two numbers. If there is no 
        remainder, it returns zero. In our example, it will return 0.8 */

            if($i == $page){
                $pagination_links .= '<div class="page-link"><span>'.$i.'</span></div> ';
            }else{
                $pagination_links .= '<div class="page-link"><a href="?page='.$i.'&'.$query_string.'">'.$i.'</a></div> ';
            }
            /* This is the exact statement that turns pages into link form that is used above */ 
        }   // Ends the if statement 

        return $pagination_links;
    }

    //added by drale.com - 1-19-2010
    function showNext($totalrows,$page,$limit,$text="next &raquo;"){    
        $next_link = null;
        $numofpages = $totalrows / $limit;

        if($page < $numofpages){
            $page++;
            $next_link = '<div class="page-link"><a href="?page='.$page.'&'.$query_string.'">'.$text.'</a></div>';
        }

        return $next_link;
    }

    function showPrev($totalrows,$page,$limit,$text="&laquo; prev"){    
        $next_link = null;
        $numofpages = $totalrows / $limit;

        if($page > 1){
            $page--;
            $prev_link = '<div class="page-link"><a href="?page='.$page.'&'.$query_string.'">'.$text.'</a></div>';
        }

        return $prev_link;
    }

    function queryString(){ 
        //matches up to 10 digits in page number
        $query_string = eregi_replace("page=[0-9]{0,10}&","",$_SERVER['QUERY_STRING']);
        return $query_string;
    }
} 
?>

is because you have upgraded your PHP version to 5.3 ?

razvan
  • 61
  • 11
  • As stated by the deprecation notice, the `ereg()` function is no longer supported. You should replace it with `preg_match()`. (search this site for examples of how to do this; it's pretty easy) – Spudley May 20 '13 at 16:21
  • ALL the errors are self-explanatory – samayo May 20 '13 at 16:23

4 Answers4

3

This is because you are using a deprecated function eregi_replace as the error states

eregi_replace() is deprecated as of PHP 5.3.0. preg_replace() with the i (PCRE_CASELESS) modifier is the suggested alternative.

Use preg_replace() function to get rid of the problem.

Fabio
  • 23,183
  • 12
  • 55
  • 64
2

The reason you're getting the error is because of this code:

    if($page > 1){
        $page--;
        $prev_link = '<div class="page-link"><a href="?page='.$page.'&'.$query_string.'">'.$text.'</a></div>';
    }

    return $prev_link;

The $prev_link variable is not initialised before this, and is only populated if $page is greater than 1.

Therefore, if $page is equal to 1 (or less?), then the return statement will be trying to return a variable that doesn't exist.

This will cause the error that you're seeing.

To fix this, add a line at the top of the function, as follows:

$prev_link = '';

This will make sure the variable is initialised regardless of the page number.

The $query_string error is exactly the same, but elsewhere in the program. Hopefully you can work out how to fix that by adapting the help above.

Finally, the ereg error is because you have upgraded your PHP version to 5.3 but the code was developed originally for an earlier PHP version. The ereg() function, and all related functions, are no longer supported since PHP 5.3, and should be replaced with preg_match(), preg_replace(), etc. There are many questions here on SO that can help you with this -- try this one for example: ereg_replace to preg_replace?

hope that helps.

Community
  • 1
  • 1
Spudley
  • 166,037
  • 39
  • 233
  • 307
  • I will get other error from preg_replace: "Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\manager\Pagination.php on line 83" – razvan May 20 '13 at 16:48
  • @razvan - Read the question I linked at the end of my answer -- it has the solution to this issue. (and if that doesn't help, there are hundreds of other posts on SO asking the same thing). – Spudley May 20 '13 at 16:52
1

Deprecated: Function eregi_replace() is deprecated in C:\xampp\htdocs\manager\Pagination.php on line 86

You want to use preg_replace instead like this:

$query_string = preg_replace("page=[0-9]{0,10}&","",$_SERVER['QUERY_STRING']);

Notice: Undefined variable: prev_link in C:\xampp\htdocs\manager\Pagination.php on line 81

This is happening because prev_link isn't really defined for the return so change the code to:

$prev_link = null;
if($page > 1){
    $page--;
    $prev_link = '<div class="page-link"><a href="?page='.$page.'&'.$query_string.'">'.$text.'</a></div>';
}

return $prev_link;

Notice: Undefined variable: query_string in C:\xampp\htdocs\manager\Pagination.php on line 66

This is happening because the variable $query_string isn't getting defined in the function showPrev. I don't know where you want to get that from, but you need to either send it in as a parameter or gather it from somewhere.

Now, to address the downvotes that you received. Questions like this show no effort. Though you may have performed some effort, you didn't document that, so as far as we can tell you just want us to do the debugging for you. It's good that you provided all the code, but remember, show what effort you've already put forth.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
1

First Notice: you should switch to preg_replace,

Second Notice: $prev_link is initialized in a conditional, easiest solution to remove those notices is to $prev_link = ''; prior to the if statement in the showPrev function.

Third Notice: $query_string is not defined anywhere in the scope of showNext.

Orangepill
  • 24,500
  • 3
  • 42
  • 63