目录
回复讨论(解决方案)
首页 后端开发 php教程 YII 按照日期过滤不起作用,怎么回事?

YII 按照日期过滤不起作用,怎么回事?

Jun 23, 2016 pm 02:13 PM

Yii PHP 日期过滤

YII  按照日期过滤不起作用,怎么回事?

模型

<?php/** * This is the model class for table "user". * * The followings are the available columns in table 'user': * @property integer $id * @property string $uname */class Timefliter extends CActiveRecord{	/**	 * Returns the static model of the specified AR class.	 * @param string $className active record class name.	 * @return User the static model class	 */	public static function model($className=__CLASS__)	{		return parent::model($className);	}	/**	 * @return string the associated database table name	 */	public function tableName()	{		return 't_time';	}		public $starttime;	public $endtime;   				/**	 * @return array validation rules for model attributes.	 */	public function rules()	{		// NOTE: you should only define rules for those attributes that		// will receive user inputs.		return array(			array('id', 'safe'),			//array('id', 'numerical', 'integerOnly'=>true),			array('time', 'safe'),						// The following rule is used by search().			// Please remove those attributes that should not be searched.			array('id, time', 'safe', 'on'=>'search'),		);	}	/**	 * @return array relational rules.	 */	public function relations()	{		// NOTE: you may need to adjust the relation name and the related		// class name for the relations automatically generated below.		return array(		);	}	/**	 * @return array customized attribute labels (name=>label)	 */	public function attributeLabels()	{		return array(		'id'=> '编号',        'time'=> '时间',		);	}	/**	 * Retrieves a list of models based on the current search/filter conditions.	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.	 */	public function search()	{		// Warning: Please modify the following code to remove attributes that		// should not be searched.		$criteria=new CDbCriteria;		$criteria->compare('id',$this->id);		$criteria->compare('time','>='.$this->starttime);		$criteria->compare('time','<='.$this->endtime);		//是否开启模糊搜索,在后面true中实现		return new CActiveDataProvider($this, array(			'criteria'=>$criteria,		    			//'pagination'=>array('pagesize'=>$pagesize?pagesize:$page),		    //'pagination'=>array('pagesize'=>$page),		));	}}
登录后复制




视图
<form method="post" ><!--时间查询-->开始时间<?php     $this->widget('zii.widgets.jui.CJuiDatePicker',array(                         'language'=>'zh_cn', 						'name'=>'starttime', 						'value'=>$query['start'] ? $query['start'] : Date('Y-m-d'), 						'options'=>array( 									'showAnim'=>'fold', 									'showOn'=>'both', 									'buttonImage'=>Yii::app()->request->baseUrl.'/images/calendar.gif', 									//'maxDate'=>'new Date()', 									'buttonImageOnly'=>true, 									'dateFormat'=>'yy-mm-dd', 						), 						'htmlOptions'=>array( 									'style'=>'height:18px', 									'maxlength'=>8, 						),     )); 	//echo $value;exit;	//http://blog.csdn.net/dreamzml/article/details/8227646?> 结束时间<?php     $this->widget('zii.widgets.jui.CJuiDatePicker',array(                         'language'=>'zh_cn', 						'name'=>'endtime', 						'value'=>$query['end'] ? $query['end'] : Date('Y-m-d'), 						'options'=>array( 									'showAnim'=>'fold', 									'showOn'=>'both', 									'buttonImage'=>Yii::app()->request->baseUrl.'/images/calendar.gif', 									//'maxDate'=>'new Date()', 									'buttonImageOnly'=>true, 									'dateFormat'=>'yy-mm-dd', 						), 						'htmlOptions'=>array( 									'style'=>'height:18px', 									'maxlength'=>8,             ),     )); ?> <!--$criteria->compare('create_time','<='.$this->endtime),//创建早间小于等于指定时间  这个名字name就是widget里面的name的数据库字段,将它放到搜索页面中指定前端名字,和后端数据库的字段在模型module里面指定compare的条件即可实现时间搜索-->	<input type="submit"  name="search" value="搜索"/></form>
登录后复制



控制器

	public function actionTimefliter()	{			$model=new Timefliter();		$this->render('timefliter',array('model'=>$model,));	}
登录后复制


回复讨论(解决方案)

提交之后又没把获取的日期值赋值给模型,那在search方法中怎么会过滤

怎么复制给模型

可以用另外一种解决方案,自己新建一个dataprovinder 然后sql语句查询时间之间的记录

$model->startTime=$_POST['starttime'];    //传值给模型$model->endTime=$_POST['endtime'];  
登录后复制


谢谢楼上两位,都搞定了,两种方法都是可行的,不过第二种比较慢
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
<🎜>掩盖:探险33-如何获得完美的色度催化剂
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1677
14
CakePHP 教程
1430
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? 说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型? PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型? Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP和Python:解释了不同的范例 PHP和Python:解释了不同的范例 Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

在PHP和Python之间进行选择:指南 在PHP和Python之间进行选择:指南 Apr 18, 2025 am 12:24 AM

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP和Python:深入了解他们的历史 PHP和Python:深入了解他们的历史 Apr 18, 2025 am 12:25 AM

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

为什么要使用PHP?解释的优点和好处 为什么要使用PHP?解释的优点和好处 Apr 16, 2025 am 12:16 AM

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。

PHP和框架:现代化语言 PHP和框架:现代化语言 Apr 18, 2025 am 12:14 AM

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHP的影响:网络开发及以后 PHP的影响:网络开发及以后 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles