Undefined offset错误怎么解决
A PHP Error was encounteredSeverity: Notice
Message: Undefined offset: 2
Filename: controllers/leavemessage.php
Line Number: 44
foreach($data as $line)
{
list($username, $title, $mess) = explode('||', $line);//这行是怎么出错误的???
if($username !=="" && $title !=="" && $mess !=="")
{
echo $username."说:".$mess.'<hr>'; //可以在网页上输出数据
}
} $data数据长什么样子呢 explode爆出来的数组不足三个,请检测 回复 jeongee 的帖子
长成这个样子滴:a||a||a
按理说是三个变量啊
不用ci写的话可以正常运行... 回复 xyst0524 的帖子
不可能,只要使用对了,不可能出错的
$line是这样的,$data应该是个数组或者object吧,你把代码发完整点,不然没法帮你解决 回复 visvoy 的帖子
有吧,我上次就遇到过这个问题并且解决了。今天还遇到了,而且仔细再往这个方向做了验证,得到的结果都是一样的 本帖最后由 jeongee 于 2011-5-7 18:35 编辑
回复 xyst0524 的帖子
敢把代码发全吗?:lol 回复 jeongee 的帖子
什么敢不敢啊,发就发:
controller:
<?php if(!defined('BASEPATH')) exit('no direct script access allowed');
class LeaveMessage extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper(array('form','url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', '用户名','required');
$this->form_validation->set_rules('title', '主题', 'required');
$this->form_validation->set_rules('mess','required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('leavemessage/leavemessage');
}else
{
$filename = "text_data.txt";
$message = $_POST["username"]."||".$_POST["title"]."||".$_POST["mess"]."<|>";
$handle = fopen($filename, "a");
flock($handle, LOCK_EX);
fwrite($handle, $message);
flock($handle, LOCK_UN);
fclose($handle);
$buffer = "";
if(file_exists($filename))
{
$handle = fopen($filename, "r");
flock($handle, LOCK_SH);
while(!feof($handle))
{
$buffer.= fread($handle, 1024);
}
$data = explode('<|>', $buffer);
foreach($data as $line)
{
@list($username, $title, $mess) = explode('||', $line);
if($username !=="" && $title !=="" && $mess !=="")
{
echo $username."说:".$mess.'<hr>';
}
}
$this->load->view('leavemessage/leavemessage');
flock($handle, LOCK_UN);
fclose($handle);
}
}
}
}
?>
view:
<html>
<head>
<title>网络留言板</title>
</head>
<body>
<?php echo form_open('leavemessage/leavemessage');?>
用户名:<input type="text" name="username"><br>
主题:<input type="text" name="title"><br>
<textarea name="mess" rows="10" cols="50">请在这里留言!</textarea>
<input type="submit" name="sub" value="提交">
</form>
</body>
</html> 回复 jeongee 的帖子
把list前得@去掉吧
我后面加上去的... 本帖最后由 jeongee 于 2011-5-7 19:20 编辑
回复 xyst0524 的帖子
$_POST["username"]."||".$_POST["title"]."||".$_POST["mess"]."<|>";
你这明显有问题啊
按照你写的,你的文件内容应该是
aa||bb||cc<|>aa||bb||cc<|>aa||bb||cc<|>aa||bb||cc<|>
这样肯定有问题啊。
你读出文件内容后,把后面的3个字符截掉之后再explode就没问题了