2017-08-08 9 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