无法在类构造函数中设置会话变量

时间:2011-07-22 08:56:36

标签: php class oop

几个月前我才进入PHP,现在已经花了几周时间研究面向对象的PHP。我想我已经做好了。

我编写了一个Table Class,它将查询转换为带分页,排序等的表。

除了一件小事之外,所有工作都非常好,这就是我想要在实例化Table类时设置默认的$ _SESSION [' TABLE_ORDERBY']变量。

我现在在构造函数中将它设置为:

$_SESSION['TABLE_ORDERBY'] = $this->_arr_fetched_row_headers[1];

当我将$ this-> _arr_fetched_row_headers [1]替换为类似' NewsDate'的字符串时。它确实有效(我必须先开始一个新的会议)。

我如何使用$ this-> _arr_fetched_row_headers [1]来自动设置会话变量?

processQuery()方法使用的

$ arr_fetched_rows:

Array ( [0] => Array ( 
[0] => NewsID [1] => NewsTitle [2] => NewsDate ) 
[1] => Array ( [0] => 1753 [1] => Test2 [2] => 2011-07-05 15:26:38 ) 
[2] => Array ( [0] => 1754 [1] => Test3 [2] => 2011-07-05 15:26:51 ) 
[3] => Array ( [0] => 1755 [1] => Test4 [2] => 2011-07-05 15:26:51 ) 
[4] => Array ( [0] => 1756 [1] => Test5 [2] => 2011-07-19 08:44:13 ) 
[5] => Array ( [0] => 1758 [1] => Test8 [2] => 2011-07-19 08:44:38 )
etc......
[count_all_rows] => 14 )

Table.class.php(仅显示几种方法)

<?php
class Table
{
    /* construct */
    public function __construct()
    {
        // initialise max_result
        if(isset($_POST['maxresult'])) {
            $_SESSION['TABLE_MAXRESULT'] = $_POST['maxresult'];
        }
        if(!isset($_SESSION['TABLE_MAXRESULT'])) {
            $_SESSION['TABLE_MAXRESULT'] = 5;
        }

        // initialise pagination
        if(!isset($_SESSION['TABLE_FROM'])) {
            $_SESSION['TABLE_FROM'] = 0;
        }
        if(isset($_GET['page'])) {
            $_SESSION['TABLE_FROM'] = $_GET['page'] * $_SESSION['TABLE_MAXRESULT'] - $_SESSION['TABLE_MAXRESULT'];;
        }
        if(!isset($_GET['page'])) {
            $_GET['page'] = 1;
        }

        // initialise sorting
        if(isset($_GET['sort']) && $_GET['sort'] == 'asc' && isset($_GET['sortby'])) {
            $_SESSION['TABLE_ORDERBY'] = $_GET['sortby'];
            $_SESSION['TABLE_ORDER'] = 'ASC';
        }
        if(isset($_GET['sort']) && $_GET['sort'] == 'desc' && isset($_GET['sortby'])) {
            $_SESSION['TABLE_ORDERBY'] = $_GET['sortby'];
            $_SESSION['TABLE_ORDER'] = 'DESC';
        }
        if(!isset($_SESSION['TABLE_ORDERBY']) ||  !isset($_SESSION['TABLE_ORDER'])) {
            $_SESSION['TABLE_ORDERBY'] = $this->_arr_fetched_row_headers[1];
            $_SESSION['TABLE_ORDER'] = 'DESC';
        }

    }

    public function processQuery($arr_fetched_rows)
    {
        // define the $this->_arr_fetched_rows property
        $this->_arr_fetched_rows = $arr_fetched_rows;

        // extract the row headers from $this->_arr_fetched_rows
        $this->_arr_fetched_row_headers = $this->_arr_fetched_rows[0];

        // count the row headers in $this->_arr_fetched_row_headers
        $this->_count_row_headers = count($this->_arr_fetched_row_headers);

        // count the total amount of rows from $this->_arr_fetched_rows
        $this->_count_all_rows = $this->_arr_fetched_rows['count_all_rows'];

        // count keys in $this->_arr_fetched_rows, subtract by 2 because of row headers and count_all_rows
        $this->_count_fetched_rows = count($this->_arr_fetched_rows) - 2;

        // create a new array without the row headers and without count_all_rows
        for($i = 1; $i <= $this->_count_fetched_rows; $i++) {
            $this->_arr_sent_rows[] = $this->_arr_fetched_rows[$i];
        }
    }

    *** edited out other methods ***

    // show table
    public function showTable()
    {
        // return the built table
        return $this->buildTable();
    }
}

的index.php

<?php
require_once 'class/Session.class.php';
require_once 'class/Query.class.php';
require_once 'class/Table.class.php';

$session = new Session();
$query = new Query();
$table = new Table();

$result = $query->selectQuery("NewsID, NewsTitle, NewsDate", "tbl_news", "ORDER BY " . $_SESSION['TABLE_ORDERBY'] . " " . $_SESSION['TABLE_ORDER'] . " LIMIT " . $_SESSION['TABLE_FROM'] . ", " . $_SESSION['TABLE_MAXRESULT'] . " ");

$table->processQuery($result);
$table->renameHeaders('NieuwsID, Nieuwstitel, Publicatiedatum, Bewerk, Verwijder');
$table->showCheckEditDelete(true, true, true);
$table->hideRowID(true);
$table->enableSort(true);
?>
<?php echo $table->showTable(); ?>

1 个答案:

答案 0 :(得分:1)

您的活动顺序是:

$table = new Table();
...
$table->processQuery($result);

processQuery是设置_arr_fetched_row_headers的函数,但您已经尝试在Table构造函数中使用它(new Table实际调用__construct函数。)< / p>

这是问题的一系列事件。