Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:44:10+00:00 2026-05-23T23:44:10+00:00

I only got into PHP a few months ago and have been dabbing into

  • 0

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(); ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:44:10+00:00Added an answer on May 23, 2026 at 11:44 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an app that's in invite-only beta right now. Problem is, I can't
I have a PHP program that encrypts a PDF file into .xxx file this
I have my 1and1 hosted web site. I've got server-side php I need to
I have a strange problem where a for loop in PHP only returns the
I have a 99% working RSS reader in PHP built into my joomla frontpage,
Seriously, I've trawled MSDN and only got half answers - what do the columns
I'm eager to give these a go, but I've only got my precious home
Im new to C# infact somedays past only.I got a project from universty so
We've got a test site hosted only by IP address. We really need to
I got a client which is website is under SSL only on the payment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.