shalong81818 发表于 2012-3-12 10:11:31

关于全局变量的问题

本帖最后由 shalong81818 于 2012-3-12 10:12 编辑

小弟新手入门, 在一个控制器里我先定义了一个全局变量

<?php
$aaa='0';
....
...

然后在后面的一个function里更改了这个全局变量的值为从用户获取的一个值

function test(){
....
$GLOBALS['aaa']=$this->input->post('location');
....

然后在这个function内,这个aaa的值是变了,但是在其他function中调用的时候,这个aaa的值依然是在定义时候的初始值0,但我想要的事更改后的那个值,小弟实在找不到办法了,求高人解答啊~~~

Hex 发表于 2012-3-12 10:43:10

直接在方法里 $this->aaa = 'xxx'; 就好了。。不用全局变量。

shalong81818 发表于 2012-3-12 11:05:28

Hex 发表于 2012-3-12 10:43 static/image/common/back.gif
直接在方法里 $this->aaa = 'xxx'; 就好了。。不用全局变量。

我也试过了啊。。还有什么$this->config->set_item()和$this->config->item(),我也用过了,都是只能在改动的那个方法里把变量的值改掉,在其他方法里,变量的值一直都保持在初始值。。。昨天花了一天,相当不解啊。。。

Hex 发表于 2012-3-12 11:44:01

shalong81818 发表于 2012-3-12 11:05 static/image/common/back.gif
我也试过了啊。。还有什么$this->config->set_item()和$this->config->item(),我也用过了,都是只能在改 ...

不可能会那样。。你还是贴代码吧,多贴点,最好都贴上来。

shalong81818 发表于 2012-3-12 12:02:13

本帖最后由 shalong81818 于 2012-3-12 12:04 编辑

Hex 发表于 2012-3-12 11:44 http://codeigniter.org.cn/forums/static/image/common/back.gif
不可能会那样。。你还是贴代码吧,多贴点,最好都贴上来。

那我就把controller贴上来<?php
class Test extendsCI_Controller {
var $aaa='0';      //这个是我想要用的变量
function __construct()
    {
      parent::__construct();
      }

function index(){
    $this->load->helper('form');
    $data['title'] = "Project Test.";
    $data['headline'] = "Welcome! Please type your ID and select your location first.";
    $this->load->vars($data);
    $this->load->view('index');
}
function saveloc(){
$this->load->helper('url');
$this->load->model('Mtest','',TRUE);
$this->Mtest->addinfo();
$service=$this->input->post('service');
$this->aaa=$this->input->post('location');//更改aaa的值,这个我确认过,aaa的值是更改成功的。
$data['current']=$this->input->post('id');
if($service=='1'){
$this->load->helper('form');
$data['title'] = "Transportation";
$data['headline'] = "Please select the traffic situation of your location.";
$this->load->vars($data);
$this->load->view('trans');
}elseif ($service=='2'){
$this->load->helper('form');
$data['title'] = "Food";
$data['headline'] = "How is the food of your location?";
$this->load->vars($data);
$this->load->view('food');
}else{
$this->load->helper('form');
$data['title'] = "Price";
$data['headline'] = "Is there any special price at your location?";
$this->load->vars($data);
$this->load->view('price');
}
}
function updatetrans(){
$this->load->helper('url');
$this->load->model('Mtest','',TRUE);
$this->db->where('location',$this->aaa); //调用aaa的值,可这里aaa又变回0了
$this->Mtest->updatetrans();
redirect('test/thanks','refresh');
}
function updatefood(){
$this->load->helper('url');
$this->load->model('Mtest','',TRUE);
$this->Mtest->updatefood();
redirect('test/thanks','refresh');
}
function updateprice(){
$this->load->helper('url');
$this->load->model('Mtest','',TRUE);
$this->Mtest->updateprice();
redirect('test/thanks','refresh');
}
function thanks(){
   echo "<h1>Thank you !</h1>";
}
}
?>
如果需要,我再把model和view也传上来

shalong81818 发表于 2012-3-12 12:22:46

Hex 发表于 2012-3-12 11:44 static/image/common/back.gif
不可能会那样。。你还是贴代码吧,多贴点,最好都贴上来。

我又建了一个小的test控制器
<?php

class Newtest extendsCI_Controller {
var $aaa='0';
function __construct()
    {
      parent::__construct();

      }
function index(){
$this->aaa='2';
echo $this->aaa;
}
function abc(){
echo $this->aaa;
}
}
?>
我在index.php/newtest里显示的是2, 然后再进index.php/newtest/abc,显示的又变回0了,会不会是我的配置有问题。。

Hex 发表于 2012-3-12 12:24:56

呵呵 你是不同请求吧?不同请求不能共享数据。。。
你只能在同一个请求里共享数据

shalong81818 发表于 2012-3-12 12:36:28

本帖最后由 shalong81818 于 2012-3-12 12:38 编辑

Hex 发表于 2012-3-12 12:24 http://codeigniter.org.cn/forums/static/image/common/back.gif
呵呵 你是不同请求吧?不同请求不能共享数据。。。
你只能在同一个请求里共享数据 ...

我也刚刚才发现。。。。。那再麻烦问一下啊,我在saveloc()这个方法里把用户的id和位置存入数据库了,然后我在下面的方法里想在刚刚加入的ID和位置的其他条目里更新信息,那怎么定位到刚刚添加的那一行啊? 毕竟在saveloc方法里获取的id 和location信息不能共享到下面的方法里。可以给点主意吗?万分感谢。。。

Hex 发表于 2012-3-12 13:35:47

shalong81818 发表于 2012-3-12 12:36 static/image/common/back.gif
我也刚刚才发现。。。。。那再麻烦问一下啊,我在saveloc()这个方法里把用户的id和位置存入数据库了,然 ...

添加后马上获取 last_id ,然后传递到 view 中,也就是发送给浏览器,下一次通过URL带上这个ID就可以了,这是很常见的网站开发方法呀。
建议你还是先了解一些基础,再来学习 CI,CI 不是给初学者使用的,呵呵

shalong81818 发表于 2012-3-12 15:25:49

Hex 发表于 2012-3-12 13:35 static/image/common/back.gif
添加后马上获取 last_id ,然后传递到 view 中,也就是发送给浏览器,下一次通过URL带上这个ID就可以了, ...

谢谢啦~~PHP还真没怎么接触过,这几天要好好补习了~
页: [1]
查看完整版本: 关于全局变量的问题