我幾個月前才進入PHP,現在幾個星期一直在面向對象PHP。我認爲我做得很好。在類構造函數中設置會話變量時遇到麻煩
我寫了一個表類,將查詢轉換爲一個表,分頁,排序等
這一切都相當不錯,除了一個小東西的工作,那就是我想要一個默認的$ _SESSION [」 TABLE_ORDERBY']變量在實例化Table類時得到設置。
我現在將它設置這樣的構造:
$_SESSION['TABLE_ORDERBY'] = $this->_arr_fetched_row_headers[1];
當我更換$這個 - > _ 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(); ?>
請只問每個問題一個問題。即考慮開始一個新的問題「'我也想知道你對我的代碼的看法,我是否正確使用OOP?'」。此外,將您發佈的代碼限制在最低限度以避免TL; DR效應。 –
感謝您的提示,我限制了代碼。 – Xorp25
您錯過了最重要的部分:您如何設置_arr_fetched_row_headers?在構造函數中沒有設置'$ this - > _ arr_fetched_row_headers'! – searlea