开发者

Trouble setting a session variable in class constructor

开发者 https://www.devze.com 2023-03-22 00:21 出处:网络
I only got into PHP a few months ago and have been dabbing into Object Oriented PHP for a few weeks now. I think I\'m doing ok with it.

I only got into PHP a few months ago and have been dabbing into Object Oriented PHP for a few weeks now. I think I'm doing ok with it.

I've written a Table Class that will convert a query to a table with pagination, sorting etc.

It's all working quite nicely except for one little thing and that is I want a default $_SESSION['TABLE_ORDERBY'] variable to get set when instantiating the Table class.

I'm now setting it like this in the constructor:

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

When I replace $this->_arr_fetched_row_headers[1] for a string like 'NewsDate'. It does work (I have to start a new session first).

How would I go about and make it able to automatically set the session variable by using $this->_arr_fetched_row_headers[1]?

$arr_fetched_rows used by the processQuery() method:

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] =>开发者_如何学JAVA; 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 (only showing a couple of methods)

<?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(); ?>


Your sequence of events is:

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

processQuery is the function that sets _arr_fetched_row_headers, but you've already tried to use it in the Table constructor (new Table actually calls the __construct function.)

It's the sequence of events that's the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消