php项目实战

来源:互联网 发布:金汇宝软件 编辑:程序博客网 时间:2024/05/20 01:10

1.在数据库字段中,查找某个值,find_in_set.

public function actionSurverylst($pageno)

{
try {

//$model = new UserInfo();
$usr = Yii::app()->session["userlogin"] ; //
$pagesize = 15;
$start = ($pageno)*$pagesize;
//Yii::log($usr->compid."----------compid------------------",CLogger::LEVEL_INFO);and endtime>now()
//根据用户所属部门,获取符合规则的调查题号
/*
$ids = SurveryDept::model()->findAll("find_in_set(".$usr->deptid.", scope)");
//判断是否为空
if(!empty($ids))
{
$idString = '';
foreach($ids as $item)
{
$idsArr[] = $item->attributes['surveryid'];
}
$idString = implode(',', $idsArr);
*/
//根据用户所属企业,获取符合规则的调查列表


$data = CompanySurvery::model()->findAll("(find_in_set(".$usr->deptid.", scope) and eid=".$usr->compid.") or (usertype=0 and eid=".$usr->compid.") and endtime>'".date('Y:m:d H:i:s',time())."' order by id desc limit $start,$pagesize");


$result = array();
if(!empty($data)){
foreach($data as $item)
{
$result[] = $item->attributes;
}
echo json_encode($result);
}else{
   print_r(0);   
}


}
catch (Exception $e)
{
print_r($e);
}
}


2.问题2,json_decode为空,检查原因,双引号,转义成了"。使用   $answer=htmlspecialchars_decode($answer);
public function actionSaveanswer()
{
date_default_timezone_set("UTC");
$answer = $_POST["submitData"];
$resultCount = 0;
if(!empty($answer))
{
$answers = json_decode($answer);
if(is_array($answers))
{
$tran = SurveryHistory::model()->dbConnection->beginTransaction();
try {
foreach ($answers as $his)
{

$hisrec = new SurveryHistory();
$hisrec->answerid = $his->answerid;
$hisrec->asnswer_timer = date('Y-m-d h:i:s');
$hisrec->userid = $his->userid;
$hisrec->questionid = $his->questionid;
$hisrec->surveryid = $his->surveryid;
$hisrec->save();

$sql = "update syberos_electronicsurvery_option set answernum=answernum+1 where id=:id";
$command = Yii::app()->db->createCommand($sql);
$command->bindParam(":id",$his->answerid,PDO::PARAM_INT);
$command->execute();

$resultCount++;
}
$tran->commit();
}
catch (Exception $e)
{
$resultCount =0;
$tran->rollback();
}
}
}
print_r($resultCount);
}