CodeIgniter,页面重新加载后访问类属性,可能吗?
我有一个 CI 页面,它将使用 jQuery 加载到视图文件中的 div。使用 switch(page_parameter),我可以控制页面显示的内容。
当我第三次调用该页面时,我为类数组设置了一个值。 但是当我第四次调用时,数组变空了。
我想知道,是否真的可以使用类属性来存储可以在页面重新访问后使用的值?还是我脑子里缺少什么? 我知道使用 session 不是一个好主意,因为真正的数组是一大块序列化的 xml。
这是我的代码:
class MyClass extends MY_Controller
{
public static $pitems = array();
function Hotel(){
parent::MY_Controller();
}
function new_campaign(){
$params = $this->uri->uri_to_assoc();
switch($params['step']){
case '3' : self::$pitems = array("test","another"); //here the class array was set successfully
$this->load->view('viewer');
break;
case '4' : print_r(self::$pitems); //here the array is empty
break;
}
}
在查看器页面中,有一个对页面的调用:
<a href="myclass/new_campaign/step/4">Next page</a>
$this-> 也有同样的问题 我在这里缺少什么? 先谢谢了~
编辑: 我看到一个有类似场景的脚本。它成功地重用了构造函数中设置的变量,而不是将其视为类变量。我会彻底确认这一点,但现在,我将关闭此线程。感谢克里斯的分享。
I have a CI page that will load to a div in view file, using jQuery. Using switch(page_parameter), I control what is showing from the page.
When I call the page for 3rd time, I set a value to the class array.
But when I call the 4th time, the array become empty.
I was wondering, is it actually possible to use the class property to store value that can be used after page re-access? Or something missing in my head?
I know that using session is not a good idea, since the real array is a big chunk of serialized xml.
Here's my code:
class MyClass extends MY_Controller
{
public static $pitems = array();
function Hotel(){
parent::MY_Controller();
}
function new_campaign(){
$params = $this->uri->uri_to_assoc();
switch($params['step']){
case '3' : self::$pitems = array("test","another"); //here the class array was set successfully
$this->load->view('viewer');
break;
case '4' : print_r(self::$pitems); //here the array is empty
break;
}
}
In the viewer page, there's a call to the page:
<a href="myclass/new_campaign/step/4">Next page</a>
Same issue also with $this->
What am i missing here?
Thanks in advance~
edit:
I saw a script that has similar scenario. it successfully reused the variable set in the constructor, instead of treating it as a class variable. i'll look thorough to confirm this, but for now, i'll close this thread. Thanks Chris for sharing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定你想做什么,但我在 CI 中有很多次用户 jQuery post()/get()/ajax() 并且没有遇到任何问题。因此,尽管不知道或不理解您想要做什么。我想我至少会说我知道通过 jQuery 之类的东西在 CI 中加载数据而不刷新不是问题。我在 CI 上构建的系统示例有一个类似 twitter 的推文提要,其中计时器上的 jQuery 正在轮询新数据,如果要显示新内容,则每次都会相应地返回数据。
Im not really sure what your trying to do but I have users jQuery post()/get()/ajax() many of times in CI and have had no problems. So despite not knowing or understanding what your trying to do. I thought I'd at least say I know loading data without refresh in CI through something like jQuery isn't an issue. Example on system I built on CI had a twitter like feed of tweets where jQuery on a timer was polling for new data and coming back with it each time accordingly if something new was to be shown.