Home > Backend Development > PHP Tutorial > Thinkphp 变量保存问题

Thinkphp 变量保存问题

WBOY
Release: 2016-06-06 20:34:29
Original
1213 people have browsed it

<code>class AdminAction extends Action {
    private  $uid_t;
    public function addassets(){
        $uid = I('id');
        $this->uid = $uid;
        $this->setuid_t($uid);
        $this->display();
    }

    public function setuid_t($uid){
        $this->uid_t = $uid;
    }

    public function getuid_t(){
        return $this->$uid_t;
    }

    public function actaddassets(){
        $data['uid'] = $this->getuid_t();
        $data['assets_name'] = I('assets_name');

        if ($id=M('device')->add($data)) {
            $this->redirect('index');
        }else{
            echo M('device')->getLastsql();exit;
            $this->redirect('addassets');
        }
    }
</code>
Copy after login
Copy after login

在addassets()中从前端获得了一个变量id,保存在本地的uid,然后display出addassets.html页面。但是这个页面中不用uid这个变量,而下一个页面(addassets.html中有一个button跳转到actaddassets.html)actaddassets.html中需要用到uid这个变量。通过上面的方法在actaddassets()中获得不了uid这个变量。
初次学习,还往大神指点,谢谢!

回复内容:

<code>class AdminAction extends Action {
    private  $uid_t;
    public function addassets(){
        $uid = I('id');
        $this->uid = $uid;
        $this->setuid_t($uid);
        $this->display();
    }

    public function setuid_t($uid){
        $this->uid_t = $uid;
    }

    public function getuid_t(){
        return $this->$uid_t;
    }

    public function actaddassets(){
        $data['uid'] = $this->getuid_t();
        $data['assets_name'] = I('assets_name');

        if ($id=M('device')->add($data)) {
            $this->redirect('index');
        }else{
            echo M('device')->getLastsql();exit;
            $this->redirect('addassets');
        }
    }
</code>
Copy after login
Copy after login

在addassets()中从前端获得了一个变量id,保存在本地的uid,然后display出addassets.html页面。但是这个页面中不用uid这个变量,而下一个页面(addassets.html中有一个button跳转到actaddassets.html)actaddassets.html中需要用到uid这个变量。通过上面的方法在actaddassets()中获得不了uid这个变量。
初次学习,还往大神指点,谢谢!

<code>class AdminAction extends Action {
    static $uid_t="";

    public function addassets(){
        $uid = I('id');
        $this->uid = $uid;
        self::$uid_t = $uid;
        $this->display();
        //echo self::$uid_t;
    }

    public function actaddassets(){
        echo self::$uid_t;
        /*
        $data['uid'] = $uid;
        $data['assets_name'] = I('assets_name');

        if ($id=M('device')->add($data)) {
            $this->redirect('index');
        }else{
            echo M('device')->getLastsql();exit;
            $this->redirect('addassets');
        }
        */
    }
</code>
Copy after login

做了一些尝试,定义一个static的$uid_t,然后在addassets()中不输出页面,直接echo self::$uid_t是有值的,但是紧接着在actaddassets()中再次echo self::$uid_t就没值了。。求教这是为什么。。?谢谢!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template