1

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] => 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(); ?>
4
  • Please, only ask one question per question. I.e. consider starting a new question for "Also I would like to know what you think of my code and am I using OOP correctly?". Also, limit the code you post to a bare minimum to avoid the TL;DR effect. Commented Jul 22, 2011 at 9:18
  • Thanks for the tip, I have limited the code. Commented Jul 22, 2011 at 10:02
  • 2
    You missed the most important part: how are you setting _arr_fetched_row_headers? There's nothing in the constructor to set $this->_arr_fetched_row_headers! Commented Jul 22, 2011 at 10:50
  • I added the array returned by Query.class.php from which $this->_arr_fetched_row_headers gets pulled in processQuery(), which is in Table.class.php. I also added the processQuery method to the code above. Hope it will make things more clear. Commented Jul 22, 2011 at 11:08

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That makes a lot of sense. This will get me going again, now to find a way of sequencing things properly... Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.