逻辑查询
您好,
我们有2个逻辑
a)最多联系客户:标记对客户联系次数的限制。我们已将供应商与客户联系的最长时间设置为2。有这个PHP代码
$max = 2;
$listing = JRequest::getInt('listing');
if($listing) {
$db = JFactory::getDBO();
$db->setQuery("SELECT COUNT(`SubmissionId`) FROM #__rsform_submission_values WHERE `FormId`='".(int) $formId."' AND `FieldName`='listing' AND `FieldValue`='".$listing."' ");
$nrSub = $db->loadResult();
if ($nrSub >= $max) {
$formLayout = '<p><em>Sorry, This customer has already been contacted maximum number of times</em></p>';
}
}
b)最大供应商可以通过表单与客户联系的次数
$max = 10;
$user = JFactory::getUser();
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('COUNT('.$db->qn('Username').')')
->from($db->qn('#__rsform_submissions'))
->where($db->qn('FormId').'='.$db->q($formId))
->where($db->qn('Username').'='.$db->q($user->get('username')));
$db->setQuery($query);
$counter = $db->loadResult();
if ($counter >= $max){
$formLayout = '<p style="color:blue;">You have already contacted 10 customers </p>';
}
两者都运作良好 - 现在我们必须设置一个条件: - 如果经销商再次尝试联系同一位客户,他也应该显示错误消息
猜猜它将基于4个参数
计数提交并将其与Max Parameter比较为1
Formid
清单
用户名
尝试此代码 - 但似乎不尊重
$max = 1;
$user = JFactory::getUser();
$listing = JRequest::getInt('listing');
if($listing) {
$db = JFactory::getDBO();
$db->setQuery("SELECT COUNT('.$db->qn('Username').') FROM #__rsform_submission_values WHERE `FormId`='".(int) $formId."' AND `FieldName`='listing' AND `FieldValue`='".$listing."' ");
$nrSub = $db->loadResult();
if ($nrSub >= $max) {
$formLayout = '<p><strong>Sorry, You have already contacted this customer</strong></p>';
}
}
以便它匹配 - 如果用户名已经联系过表单的列表ID - 那么应该显示错误消息
可能是需要在代码中添加的用户名参数 - 1
有人可以提供帮助,帮助和建议 - 如何设置逻辑查询
答案 0 :(得分:0)
如果您使用COUNT我认为您也应该应用GROUP BY,例如:
SELECT COUNT('.$db->qn('Username').') FROM #__rsform_submission_values WHERE `FormId`='".(int) $formId."' AND `FieldName`='listing' AND `FieldValue`='".$listing."' GROUP BY '.$db->qn('Username').'"