dongmaqiu6084 2017-08-01 22:10
浏览 65
已采纳

试图更新ps_pagination类

I have an older pagination script that has served me well over the years, however its been pretty much abandoned by the developer and I'm unable to wrap my head around upgrading the mysql calls to mysqli due to it being oop, which I'm pretty unfamiliar with. My main issue is I know what I need to change, but I can't figure out the syntax when using $this-. simply changing the call isn't enough and when I add the parameters my editor complains about it.

say when I change

$all_rs = @mysql_query($this->sql );

to

$all_rs = mysqli_query($this->conn, $this->sql );

(which is probably totally wrong syntax for oop) Im told I'm missing the query parameter or that I have undeclared vars. Bear in mind the editor is only seeing the class and not the values of the vars being fed to it, so I'm kind of at a loss about how to code it.

heres the full class

<?php
/**
 * PHPSense Pagination Class
 *
 * PHP tutorials and scripts
 *
 * @package     PHPSense
 * @author      Jatinder Singh Thind
 * @copyright   Copyright (c) 2006, Jatinder Singh Thind
 * @link        http://www.phpsense.com
 */

// ------------------------------------------------------------------------


class PS_Pagination {
    var $php_self;
    var $rows_per_page = 10; //Number of records to display per page
    var $total_rows = 0; //Total number of rows returned by the query
    var $links_per_page = 5; //Number of links to display per page
    var $append = ""; //Paremeters to append to pagination links
    var $sql = "";
    var $debug = false;
    var $conn = false;
    var $page = 1;
    var $max_pages = 0;
    var $offset = 0;

    /**
     * Constructor
     *
     * @param resource $connection Mysql connection link
     * @param string $sql SQL query to paginate. Example : SELECT * FROM users
     * @param integer $rows_per_page Number of records to display per page. Defaults to 10
     * @param integer $links_per_page Number of links to display per page. Defaults to 5
     * @param string $append Parameters to be appended to pagination links 
     */

    function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5, $append = "") {
        $this->conn = $connection;
        $this->sql = $sql;
        $this->rows_per_page = (int)$rows_per_page;
        if (intval($links_per_page ) > 0) {
            $this->links_per_page = (int)$links_per_page;
        } else {
            $this->links_per_page = 5;
        }
        $this->append = $append;
        $this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
        if (isset($_GET['page'] )) {
            $this->page = intval($_GET['page'] );
        }
    }

    /**
     * Executes the SQL query and initializes internal variables
     *
     * @access public
     * @return resource
     */
    function paginate() {
        //Check for valid mysql connection
        if (! $this->conn || ! is_resource($this->conn )) {
            if ($this->debug)
                echo "MySQL connection missing<br />";
            return false;
        }

        //Find total number of rows
        $all_rs = @mysql_query($this->sql );
        if (! $all_rs) {
            if ($this->debug)
                echo "SQL query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
            return false;
        }
        $this->total_rows = mysql_num_rows($all_rs );
        @mysql_close($all_rs );

        //Return FALSE if no rows found
        if ($this->total_rows == 0) {
            if ($this->debug)
                echo "Query returned zero rows.";
            return FALSE;
        }

        //Max number of pages
        $this->max_pages = ceil($this->total_rows / $this->rows_per_page );
        if ($this->links_per_page > $this->max_pages) {
            $this->links_per_page = $this->max_pages;
        }

        //Check the page value just in case someone is trying to input an aribitrary value
        if ($this->page > $this->max_pages || $this->page <= 0) {
            $this->page = 1;
        }

        //Calculate Offset
        $this->offset = $this->rows_per_page * ($this->page - 1);

        //Fetch the required result set
        $rs = @mysql_query($this->sql . " LIMIT {$this->offset}, {$this->rows_per_page}" );
        if (! $rs) {
            if ($this->debug)
                echo "Pagination query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
            return false;
        }
        return $rs;
    }

    /**
     * Display the link to the first page
     *
     * @access public
     * @param string $tag Text string to be displayed as the link. Defaults to 'First'
     * @return string
     */
    function renderFirst($tag = 'First') {
        if ($this->total_rows == 0)
            return FALSE;

        if ($this->page == 1) {
            return '"previous-off">' . $tag;
        } else {
            return '"next"><a href="' . $this->php_self . '?page=1&amp;' . $this->append . '">' . $tag . '</a> ';
        }
    }

    /**
     * Display the link to the last page
     *
     * @access public
     * @param string $tag Text string to be displayed as the link. Defaults to 'Last'
     * @return string
     */
    function renderLast($tag = 'Last') {
        if ($this->total_rows == 0)
            return FALSE;

        if ($this->page == $this->max_pages) {
            return '"previous-off">' . $tag;
        } else {
            return '"next"><a href="' . $this->php_self . '?page=' . $this->max_pages . '&amp;' . $this->append . '">' . $tag . '</a>';
        }
    }

    /**
     * Display the next link
     *
     * @access public
     * @param string $tag Text string to be displayed as the link. Defaults to '>>'
     * @return string
     */
    function renderNext($tag = '&gt;&gt;') {
        if ($this->total_rows == 0)
            return FALSE;

        if ($this->page < $this->max_pages) {
            return '"next"><a href="' . $this->php_self . '?page=' . ($this->page + 1) . '&amp;' . $this->append . '">' . $tag . '</a>';
        } else {
            return '"next-off">' . $tag;
        }
    }

    /**
     * Display the previous link
     *
     * @access public
     * @param string $tag Text string to be displayed as the link. Defaults to '<<'
     * @return string
     */
    function renderPrev($tag = '&lt;&lt;') {
        if ($this->total_rows == 0)
            return FALSE;

        if ($this->page > 1) {
            return ' "next"><a href="' . $this->php_self . '?page=' . ($this->page - 1) . '&amp;' . $this->append . '">' . $tag . '</a>';
        } else {
            return '"previous-off">' . $tag;
        }
    }

    /**
     * Display the page links
     *
     * @access public
     * @return string
     */
    function renderNav($prefix = '<span class="page_link">', $suffix = '</span>') {
        if ($this->total_rows == 0)
            return FALSE;

        $batch = ceil($this->page / $this->links_per_page );
        $end = $batch * $this->links_per_page;
        if ($end == $this->page) {
            //$end = $end + $this->links_per_page - 1;
        //$end = $end + ceil($this->links_per_page/2);
        }
        if ($end > $this->max_pages) {
            $end = $this->max_pages;
        }
        $start = $end - $this->links_per_page + 1;
        $links = '';

        for($i = $start; $i <= $end; $i ++) {
            if ($i == $this->page) {
                $links .= $prefix . ' class="active">' . "$i"  . $suffix;
            } else {
                $links .= ' ' . $prefix . '><a href="' . $this->php_self . '?page=' . $i . '&amp;' . $this->append . '">' . $i . '</a>' . $suffix . ' ';
            }
        }

        return $links;
    }

    /**
     * Display full pagination navigation
     *
     * @access public
     * @return string
     */
    function renderFullNav() {
        return $this->renderFirst() . '&nbsp;' . $this->renderPrev() . '&nbsp;' . $this->renderNav() . '&nbsp;' . $this->renderNext() . '&nbsp;' . $this->renderLast();
    }

    /**
     * Set debug mode
     *
     * @access public
     * @param bool $debug Set to TRUE to enable debug messages
     * @return void
     */
    function setDebug($debug) {
        $this->debug = $debug;
    }
}
?>

any thoughts on how I should be formatting these calls? Thanks

  • 写回答

1条回答 默认 最新

  • dqellle310167 2017-08-02 02:18
    关注

    Here, I fixed a few basic things about the way the class is constructed, mainly respecting currently in use standards and modern oop constructs.

    The gist of it however, resides in the statement call:

    $all_rs = $this->conn->query($this->sql);
    
    • PHP has the __construct() function we should use as a constructor.
    • Use private, protected and public when defining your members.
    • Use typehints to designate your parameters (php7 offers a lot of scalar typehints that were missing in php5 (int, string, float for example) but we can typehint any class.
    • There is no need to manually verify if connection was successfull, or to check for errors. The connection object should be built with the desired error reporting level, the mysqli implementation will throw exceptions as needed afterwards.
    • Never use the fairly ridiculous @ in front of the queries. Suppressing error messages is not a sane way to reason about database access, or programming at all as far as my experience goes, and can only lead to hard debugging.
    • Using setter injection (for the debug parameter) can also only lead to problems. Having it as a constructor parameter ensures correct behavior for all the pagination object's life cycle.

    The bad news

    However, this is wide open to sql injection, because I haven't changed the function signatures so this can continue to be used along actual code. Normally, you would prepare, then execute the statements with the values to take advantage of parameterized statements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?