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

Hide individual sections of a RSFormPro form when submission limit has been reached

我在 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!';
}

我看到您已经编辑了您的问题以揭示您的解决方案。与其让您 post 您的解决方案作为答案(您应该这样做),不如我会为您做得更好——我已经花时间重构您的代码,实施 Joomla 的查询构建方法,使它更高效、更清洁、更直接、更易于管理。

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

因为您的字段名称与您的类名称无缝关联,"lookup array" 或 "mapping array" 将使我的代码段能够可靠地动态提供您想要的结果。您永远不需要调整超过 $formId$field_maxes.

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

$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");

}