【问题标题】:Load database value with function使用函数加载数据库值
【发布时间】:2016-12-24 12:42:53
【问题描述】:

我没有看到任何类似的主题,所以我发布了一个新主题。
首先,我很抱歉我的英语不好。

我想要一个公共函数,它可以将数据库中的值存储在全局变量中,并使用该值让 php 脚本选择网站模板。你能帮帮我吗?

    private $page_title = null;
private $body_title = null;
private $body_content = null;
private $template = null;
private $settings = array();
private $file_template = 'content';
private $path = 'templates/';
private $parse_page = false;
global $template2;

public function GetHASH()
{
    return $this->hash;
}

function Tema()
{
    global $db,$user_class;
    if($user_class->authorized)
    {
        $row = $db->sql("SELECT * FROM `ucp_users` WHERE `user_hash` = '".$this->GetHASH()."'");
        $array = $db->get_array($row);
        $template2 = $array['template'];
    }
    else
    {
        $template2 = '1';
    }
    return $template2;
}
function __construct() { 
    switch($template2) {
        default:
        {
            $template_name = 'dsa/';
            $template2 = 1;
        }
        break;
        case 0: $template_name = 'lsrp/';
        break;
        case 1: $template_name = 'dsa/';
        break;
    }
    $this->path = $this->path.''.$template_name;
}

谢谢。

【问题讨论】:

  • 那么你已经拥有的究竟出了什么问题?
  • 这是在课堂上吗?
  • 是在一个类中,并且函数“Tema”不从数据库加载变量。 $template2 对于我能做的所有事情都是 0

标签: php


【解决方案1】:

当你实例化你的类时,构造函数被调用,此时你的 tema 方法还没有被调用,因为没有实例就无法调用它。一种解决方案是在构造函数中调用 tema 方法,如下所示:

function __construct() {
  $template2 = this->tema(); 
  switch($template2) {
    default:
    {
        $template_name = 'dsa/';
        $template2 = 1;
    }
    break;
    case 0: $template_name = 'lsrp/';
    break;
    case 1: $template_name = 'dsa/';
    break;
  }
  $this->path = $this->path.''.$template_name;

}

【讨论】:

    猜你喜欢
    • 2018-06-07
    • 2018-05-28
    • 2015-07-16
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    相关资源
    最近更新 更多