达到提交限制后,隐藏RSFormPro表单的各个部分

时间:2019-02-16 11:56:59

标签: php jquery mysql joomla count

我在Joomla CMS上使用RsForm Pro,并且在那里创建了一个表单。在我的表单中,我有5个带有复选框的部分。我想显示达到特定限制的单个部分(假设最多20个用户),在达到提交限制后,需要禁用表单部分。

我发现了提交表单的限制,但是如果20个用户仅在表单中提交了一个部分(而不是复选框),则其他用户将无法再提交4个部分。换句话说,除非所有部分都达到限制,否则我不想禁用所有部分。

这是限制提交表格的代码:

// Define the maximum number of submissions. For this example we'll use 25.
$max = 25;

// Get a database connection.
$db = JFactory::getDbo();

// Setup the query. This query counts the number of submissions for the current form.
// $formId contains the ID of the current form.
$db->setQuery("SELECT COUNT(`SubmissionId`) FROM #__rsform_submissions WHERE `FormId`='".(int) $formId."'");
$submissions = $db->loadResult();

if ($submissions >= $max) {
    $formLayout = 'Sorry, we have no more spaces on this time. Please wait next registration. Thank you!.';
}

已解决

$limit25 = 25;
$limit21 = 21;
$limit20 = 20;

$db = JFactory::getDbo();

$db->setQuery("SELECT COUNT(`FieldName`) FROM `nqm2i_rsform_submission_values` WHERE `nqm2i_rsform_submission_values`.`FieldName` = 'Bus_Blockchain_March 11th'");

$first_submission_value = $db->loadResult();

if ($first_submission_value >= $limit25) {
    echo '<style>.rsform-block-bus-blockchain-march-11th { display:none;}</style>';
}

$db->setQuery("SELECT COUNT(`FieldName`) FROM `nqm2i_rsform_submission_values` WHERE `nqm2i_rsform_submission_values`.`FieldName` = 'Bus_Blockchain_March 13'");

$secont_submission_value = $db->loadResult();

if ($second_submission_value >= $limit25) {
    echo '<style>.rsform-block-bus-blockchain-march-13 { display:none;}</style>';
}

$db->setQuery("SELECT COUNT(`FieldName`) FROM `nqm2i_rsform_submission_values` WHERE `nqm2i_rsform_submission_values`.`FieldName` = 'Bus_Blockchain_March 18'");

$third_submission_value = $db->loadResult();

if ($third_submission_value >= $limit25) {
    echo '<style>.rsform-block-bus-blockchain-march-18 { display:none;}</style>';
}

$db->setQuery("SELECT COUNT(`FieldName`) FROM `nqm2i_rsform_submission_values` WHERE `nqm2i_rsform_submission_values`.`FieldName` = 'evening_QA'");

$fourth_submission_value = $db->loadResult();

if ($fourth_submission_value >= $limit20) {
    echo '<style>.rsform-block-evening-qa {  display:none;}</style>';
}

$db->setQuery("SELECT COUNT(`FieldName`) FROM `nqm2i_rsform_submission_values` WHERE `nqm2i_rsform_submission_values`.`FieldName` = 'evening_QA_2'");

$fifth_submission_value = $db->loadResult();

if ($fifth_submission_value >= $limit21) {
    echo '<style>.rsform-block-evening-qa-2 { display:none;}</style>';
}

if(
    $first_submission_value >= $limit25
    && $secont_submission_value >= $limit25
    && $third_submission_value >= $limit25
    && $fourth_submission_value >= $limit20
    && $fifth_submission_value >= $limit21
) {
    $formLayout = 'Sorry, we have no more spaces on this time. Please wait next registration. Thank you!';
}

1 个答案:

答案 0 :(得分:1)

我看到您已编辑问题以显示解决方案。与其要求您发布解决方案作为答案(您应该这样做),不如让您做得更好-我花了一些时间来重构代码,实现Joomla的查询构建方法,使其更高效,更清洁,更直接,更易于管理。

最重要的是,因为所有数据库交互都可以通过单个查询执行,所以应该做到。

由于您的字段名称与您的类名称无缝关联,因此“查找数组”或“映射数组”将使我的代码段能够可靠地动态传递您所需的结果。您无需调整超过$formId$field_maxes

代码:(使用我自己的rsform pro表在本地测试)

$formId = 3;  // or whatever the correct formId value is
$field_maxes = [
    'Bus_Blockchain_March 11th' => 25,
    'Bus_Blockchain_March 13' => 25,
    'Bus_Blockchain_March 18' => 25,
    'evening_QA' => 20,
    'evening_QA_2' => 21
];

$classes_to_hide = [];                    // initiate as empty array
$formLayout = '';                         // initiate as empty string

$db = JFactory::getDbo();

$quoted_fields = implode(',', $db->q(array_keys($field_maxes)));  // create quoted comma-separated values

$query = $db->getQuery(true)
    ->select("FieldName, COUNT(1) AS " . $db->qn("Count"))
    ->from("#__rsform_submission_values")
    ->where([
        "FormId = " . (int) $formId,
        "FieldName IN ($quoted_fields)"
    ])
    ->group("FieldName")
    ->order("FIELD ($quoted_fields)");

// if you wish to see the rendered query, uncomment the next line (do not show to public)
// JFactory::getApplication()->enqueueMessage("<div>" . $query->dump() . "</div>", "notice");

try                                           // listen for syntax errors
{
    $db->setQuery($query);
    if (!$results = $db->loadAssocList())     // declare and check for empty result set
    {
        echo "No Results in Form";
    }
    else
    {
        foreach ($results as $row)            // iterate rows
        {
            if ($row['Count'] >= $field_maxes[$row['FieldName']]) {  // if fieldname has reached limit
                $classes_to_hide[] = ".rsform-" . str_replace(['_', ' '], '-', $row['FieldName']);
            }
        }

        if ($tally = sizeof($classes_to_hide))  // declare and check if not empty
        {
            echo "<style>" , implode(", ", $classes_to_hide) , " {display:none;}</style>";  // apply css styling to one or more designated classes
            if ($tally == sizeof($field_maxes))  // if all fieldnames are full
            {
                $formLayout = 'Sorry, we have no more spaces on this time. Please wait next registration. Thank you!';
            }
        }
    }
}
catch (Exception $e)
{
    JFactory::getApplication()->enqueueMessage("Query Syntax Error", "error");
    // if you have a syntax error and wish to see the message, uncomment the next line (do not show to public)
    //JFactory::getApplication()->enqueueMessage($e->getMessage(), "error");

}