2017-08-08 8 views
0

我想根據$ _SESSION變量在我的一個CodeIgniter模型中更改表名。該變量決定語言,我想根據所選語言查詢不同的表。如何根據會話變量確定CodeIgnitor模型中的表名

現在我有這樣的:

<?php if (!defined('BASEPATH')) exit ('No direct script access allowed'); 

class Tp_modules_model extends DataMapper { 

    public function __construct(){ 
    parent::__construct(); 
    } 

    public $table = determineTable($_SESSION['lang']); 

    public function getModules($ids) { 
    $this->clear(); 
    return $this->where_in('id', $ids)->get()->all_to_array(); 
    } 

    public function determineTable($lang) { 
    if ($lang == 'nl') { 
     return 'modules_dutch'; 
    } else { 
     return 'modules'; 
    } 
} 
} 

但我收到此錯誤信息:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in /usr/www/users/staginntkx/application/modules/ecom_bookings/models/tp_modules_model.php on line 9

是否有這種或者其他的方式解決辦法來改變基於輸出我的模型表$ _SESSION ['lang']?任何幫助將是偉大的,謝謝!

回答

0

你可以嘗試以下方法:

你只聲明變量表作爲私有/公共然後初始化其在構造函​​數值。這裏是你如何在你的代碼做到這一點:

<?php if (!defined('BASEPATH')) exit ('No direct script access allowed'); 

class Tp_modules_model extends DataMapper { 

private $table; 

public function __construct(){ 
    parent::__construct(); 

    // setting the variable for table as per session variable 
    $this->table = determineTable($this->session->userdata('lang'); 
} 

public function getModules($ids) { 
    $this->clear(); 
    return $this->where_in('id', $ids)->get()->all_to_array(); 
} 

public function determineTable($lang) { 
    if ($lang == 'nl'){ 
    return 'modules_dutch'; 
    }else{ 
    return 'modules'; 
    } 
} 
} 

你應該用笨會話庫設置和獲取會話變量。 https://codeigniter.com/userguide3/libraries/sessions.html