I use RsForm Pro on Joomla CMS, and there I have created a form. In my form, I have 5 sections with a checkbox. I want to display individual sections up to a certain limit (let's say a max of 20 users), after the submission limit has been reached the form section needs to be disabled.
I found a limit for submit form, but if 20 users submit only one section in the form (not for checkbox), another 4 can't be available for other users. In other words, I don't want to disable all sections unless all sections have reached there limit.
This is code for limit submit form:
// 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!.';
}
EDIT SOLVED:
$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!';
}
I see that you have edited your question to reveal your solution. Rather than to ask you to post your solution as an answer (which you should have done), I'll do you one better -- I've taken the time to refactor your code, implement Joomla's query building methods, make it more efficient, cleaner, more direct, and easier to manage.
Most importantly, because all of the database interactions can be performed with a single query, they should be.
Because your field names seamlessly relate to your classnames, a "lookup array" or "mapping array" will allow my snippet to reliably deliver your desired results dynamically. You will never need to adjust more than $formId
and $field_maxes
.
Code: (tested locally with my own 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");
}