简体   繁体   English

CodeIgniter闪存数据不均

[英]CodeIgniter flashdata incoerence

So, I've been struggling with this for some time, but to no avail. 因此,我已经为此苦苦挣扎了一段时间,但无济于事。 My research didn't help much, either. 我的研究也没有太大帮助。 Here it is: I built a test to prevent people to go to the alter page of an module by typing the address directly in the url without the id of the registry to be altered (therefore causing trouble in the DB). 它是这样的:我构建了一个测试,以防止人们通过直接在url中键入地址而不更改注册表的ID来防止进入模块的更改页面(从而在DB中引起麻烦)。 Simply put, it searches for the id passed as parameter in the url in the DB; 简而言之,它将在数据库的url中搜索作为参数传递的id。 if it has a match, it proceeds, if not, I redirect to the main module page with an error message passed via flashdata. 如果匹配,则继续,如果不匹配,则通过通过flashdata传递的错误消息重定向到主模块页面。 I use a similar process to impede the insertion/alteration of registries if a field from a different table is not defined (as it is required in both in order to work, as the tables are related). 如果未定义其他表中的字段,我将使用类似的过程来阻止注册表的插入/更改(因为这两个表都是相关的,因此这两个函数都需要它)。 A rough example of what I'm doing in the Controller is: 我在Controller中正在做的一个粗略示例是:

if(is_numeric($id)) $search=$this->model_foo->search($id);
else
{
  $this->session->set_flashdata('error_message','not numeric');
  redirect('myurl/index','refresh');
}
if($search->num_rows()==0)
{
  $this->session->set_flashdata('error_message','not found');
  redirect('myurl/index','refresh');
}
$search=$this->model_foo2->list();
if ($search->num_rows()==0)
{
  $this->session->set_flashdata('error_message','other table empty');
  redirect('myurl/index','refresh');
}

And my view (index) is like this: 我的观点(索引)是这样的:

<?php
 $error=$this->session->flashdata('error_message');
 $success=$this->session->flashdata('success_message'); /*success_message goes after a successful inset/update*/
 if ($error!="")echo $error;
?>

So here is the issue: the success messages show up normally (I checked and double checked, they are being declared in the Controller the exact same way the error ones), as well as the 'not numeric' one, but not the 'not found' and 'other table empty' ones. 这就是问题所在:成功消息正常显示(我检查并再次检查,它们在控制器中以与错误消息完全相同的方式声明),以及“非数字”消息,而不是“非”消息找到”和“其他表为空”。 I'm really confused by this one, since the flashdata seems to work in some instances and not in others, which is specially weird given the flashdata are being declared in pretty much the same way... =/ Sorry if I wasn't clear enough, this is my first post here, so (try to) be patient ;D Thanks in advance for any help in this matter. 我对此感到非常困惑,因为flashdata似乎在某些情况下有效,而在另一些情况下却不起作用,这特别奇怪,因为flashdata的声明方式几乎相同... = /对不起,如果我没有很清楚,这是我在这里的第一篇文章,所以(请尝试)耐心; D预先感谢您在此问题上的任何帮助。

EDIT: Found out the source of the issue. 编辑:找出问题的根源。 It was something with my browsers cache storage. 这与我的浏览器缓存存储有关。 Rebooted my machine and cleaned the cache and the output worked like a charm. 重新启动我的机器并清理了缓存,输出结果像个魅力。 Thanks for all the help. 感谢您的所有帮助。

So, i just improved your code a little bit so you can try and debug... 所以,我只是稍微改善了您的代码,以便您可以尝试调试...

Controller: 控制器:

$arr = array('error_message' => '');
if(!empty($id) && is_numeric($id)) {
  $search=$this->model_foo->search($id);

  if($search->num_rows()==0) {
    $arr['error_message'] = 'not found';
  } else {
    $search=$this->model_foo2->list();
    if ($search->num_rows()==0) {
      $arr['error_message'] = 'other table empty';
    }
  }
} else {
  $arr['error_message'] = 'not numeric';
}

$this->session->set_flashdata($arr);
redirect('myurl/index');

View (same): 查看(相同):

<?php
 $error=$this->session->flashdata('error_message');
 $success=$this->session->flashdata('success_message'); /*success_message goes after a successful inset/update*/
 if ($error!="")echo $error;
?>

Give a try with this and before check if the message appears, make sure the flashdata is set. 尝试一下,在检查消息是否出现之前,请确保已设置闪存数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM