用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2799|回复: 5
收起左侧

[HELP] 今天被查询整惨了。请HEX救命!!

[复制链接]
发表于 2010-3-25 19:31:31 | 显示全部楼层 |阅读模式
1.controllers\blog.php
PHP复制代码
<?php
class Blog extends Controller{
function Blog(){
parent::Controller();
$this->load->library('table');
}
function index(){
$starttime = $_POST['starttime'];
$endtime = $_POST['endtime'];
$data['query']= $this->db->get_where('entries',array('powertime >'=>$starttime,'powertime <'=>$endtime));
$this->load->view('blog_view');
}
}
?>
复制代码

2.views\blog_view.php
PHP复制代码
<html>
<head>
<title>111</title>
</head>
<body>
<form action="http://localhost/ci/index.php/blog/index/" method="post">
<input type="text" name="starttime" />
<input type="text" name="endtime" />
<input type="submit" value="提交" />
</form>
<?php echo $this->table->generate($query); ?>
</body>
</html>
复制代码

3.数据库信息列表
PHP复制代码
id powertime data
4 2010-01-02 2
5 2010-01-03 3
6 2010-01-04 4
7 2010-01-05 5
8 2010-01-06 6
9 2010-01-07 7
10 2010-01-08 8
11 2010-01-09 9
复制代码

////////////////////////////////////////////
程序无法运行,问题在于解析blog.php的时候$_POST['starttime']和$_POST['endtime']没有值。才会报错。
最终研究了一个贼笨的方法。用了四个文件才搞定。
1.controllers\blog.php
PHP复制代码
<?php
class Blog extends Controller{
function Blog(){
parent::Controller();
}
function index(){
$this->load->view('blog_view');
}
}
?>
复制代码

2.views\blog_view.php
PHP复制代码
<html>
<head>
<title>111</title>
</head>
<body>
<form action="http://localhost/ci/index.php/data/index/" method="post">
<input type="text" name="starttime" />
<input type="text" name="endtime" />
<input type="submit" value="提交" />
</form>
</body>
</html>
复制代码

3.controllers\data.php
PHP复制代码
<?php
class Data extends Controller{
function Data(){
parent::Controller();
$this->load->scaffolding('entries');
$this->load->library('table');
}
function index(){
$starttime = $_POST['starttime'];
$endtime = $_POST['endtime'];
$data['query']= $this->db->get_where('entries',array('powertime >'=>$starttime,'powertime <'=>$endtime));
$this->load->view('data_view',$data);
}
}
?>
复制代码

4.views\data_views
PHP复制代码
<html>
<head>
<title>111</title>
</head>
<body>
<form action="http://localhost/ci/index.php/data/index/" method="post">
<input type="text" name="starttime" />
<input type="text" name="endtime" />
<input type="submit" value="提交" />
</form>
<?php echo $this->table->generate($query); ?>
</body>
</html>
复制代码

这样可以解决,但是太笨了。
感觉自就会好点?请己好像钻进了一个死循环里面。是不是用上M层高手帮忙。。。
发表于 2010-3-25 20:07:46 | 显示全部楼层
$starttime = $_POST['starttime'];
$endtime = $_POST['endtime'];
if(!$starttime||!$endtime)$data['query']= $this->db->get_where('entries');
else $data['query']= $this->db->get_where('entries',array('powertime >'=>$starttime,'powertime <'=>$endtime));
 楼主| 发表于 2010-3-26 07:46:08 | 显示全部楼层
多谢版主,基本解决问题了。可是会出现一些报警:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: starttime

不明确的变量?
发表于 2010-3-26 08:01:07 | 显示全部楼层
要用isset(),判断starttime/endtime,自己想想怎么写,很简单的
发表于 2010-3-26 09:10:46 | 显示全部楼层
应该用 $this->input->post('some_data');
楼主多看看手册,会有很多宝贝的,呵呵

参考 http://codeigniter.org.cn/user_guide/libraries/input.html
 楼主| 发表于 2010-3-26 12:08:30 | 显示全部楼层
我觉得跟语法好像没有关系,好像是逻辑上的关系。
使用$this->input->post也一样。
控制器调用视图,而表单在视图里提交。控制器初始就没有表单传送的值。

本版积分规则