我已经创建了一个函数,其中我已经采用了if和else-if梯形图,当前两个条件为false时,它转到第三个条件。第三个条件包含两个日期和一个categoryid,但查询显示空白答案。
我的功能是:
public function getfeestransonedate()
{
$odate=array_key_exists('odate',$_GET) ? date('Y-m-d',strtotime($_GET["odate"])) : null;
$sdate=array_key_exists('sdate',$_GET) ? date('Y-m-d',strtotime($_GET["sdate"])) : null;
$edate=array_key_exists('edate',$_GET) ? date('Y-m-d',strtotime($_GET["edate"])) : null;
$pdate=array_key_exists('pdate',$_GET) ? date('Y-m-d',strtotime($_GET["pdate"])) : null;
$qdate=array_key_exists('qdate',$_GET) ? date('Y-m-d',strtotime($_GET["qdate"])) : null;
$categoryid=array_key_exists('categoryid',$_GET) ? : null;
//echo $categoryid; die;
try {
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->SELECT('#__expense_transaction.expense_category_id, #__expense_transaction.date, #__expense_transaction.ammount as tamount, #__expense_transaction.transaction_details, #__expense_transaction.id, #__expenses_category_master.category_name');
$query->FROM('#__expense_transaction, #__expenses_category_master');
//var_dump(array($sdate,$edate,$odate));
if($sdate && $edate) {
$query->where('#__expense_transaction.date BETWEEN "'.$sdate.'" AND "'.$edate.'" AND #__expenses_category_master.id = #__expense_transaction.expense_category_id');
// echo $query; die;
} elseif($odate) {
$query->where('#__expense_transaction.date = "'.$odate.'" AND #__expenses_category_master.id = #__expense_transaction.expense_category_id');
//echo $query; die;
}
elseif($pdate && $qdate) {
$query->where('#__expense_transaction.date BETWEEN "'.$pdate.'" AND "'.$qdate.'" AND #__expense_transaction.expense_category_id = "'.$categoryid.'" AND #__expenses_category_master.id = #__expense_transaction.expense_category_id');
//echo $query; die;
} else {
echo 'Swapnil';
}
// echo $query; die;
$db->setQuery((string)$query);
$this->feestrans_data = $db->loadObjectList();
if ($error = $db->getErrorMsg()) {
throw new Exception($error);
}
}
catch (JException $e) {
if ($e->getCode() == 404) {
// Need to go thru the error handler to allow Redirect to work.
JError::raiseError(404, $e->getMessage());
}
}
}
告诉我,我对第三个条件的查询是对还是错。
答案 0 :(得分:-1)
尝试编写elseif(condition)
而不是else if(condition)
。
更多信息:http://www.php.net/manual/en/control-structures.elseif.php
首先需要检查$ _GET [key]是否存在,然后才将其转换为日期值。否则,它将返回PHP的默认日期值(1970-01-01),而不是null
false
,正如您所料。
所以你应该这样做:
$odate=array_key_exists('odate',$_GET) ? date('Y-m-d',strtotime($_GET["odate"])) : null;
$sdate=array_key_exists('sdate',$_GET) ? date('Y-m-d',strtotime($_GET["sdate"])) : null;
$edate=array_key_exists('edate',$_GET) ? date('Y-m-d',strtotime($_GET["edate"])) : null;