Add counter for specific field values

Status
Not open for further replies.

jlee

Member
Hello,

I have a form that someone fills in order to participate in a class.
Some of the fields are these:
First Name | Last Name | Class Title | Class_unique_id

The First Name and Last Name are filled by the user.
The Class Title and Class_unique_id are filled by me from the URL like this:
/index.php/aitisi-seminariwn?Class_Title=The Class Name&Class_Unique_id=class_cs_1.

Now i want to add a counter that checks the value of the class_unique_id,
if it is equal with an already existing id then it increases a counter for that specific class_unique_id.
else it sets it to 1.

Example: Let's say that i have 4 users that want to participate in maths class with unique id = maths_1.
It would work like this:
look if maths_1 id exists. If exist then find the latest entry count (in our case would be 4) and increase it by one.
else put 1 on entry count.

Any ideas?
 
I want something like an internal counter but for specific values of ids.
example of a table:
First Name | Last Name | Class Name | Class_uid | uid_counter
JOHN | JOHN | MATHS | MATHS_1 | 1
HELEN | HELEN | MATHS | MATHS_1 | 2
JOHN | JOHN | MATHS | MATHS_1 | 3
JOHN | CHRIS | MATHS | MATHS_1 | 4
JOHN | JOHN | MATHS | PHYSICS_1 | 1
HELEN | HELEN | MATHS | PHYSICS_1 | 2
JOHN | JOHN | MATHS | PHYSICS_1 | 3
 
Here the code to put inside uid_counter calc element.

PHP:
$class_uid = '{yourtable___class_uid}';
 
$uid_counter = JFactory::getDBO();$uid_counter->setQuery("SELECT uid_counter FROM yourtable WHERE id = '{yourtable___id}'");$uid_counter = $uid_counter->loadResult();
 
$maths_1 = JFactory::getDBO();$maths_1->setQuery("SELECT uid_counter FROM yourtable WHERE class_uid = 'MATHS_1' ORDER BY id DESC LIMIT 1");$maths_1 = $maths_1->loadResult()+1;
$physics_1 = JFactory::getDBO();$physics_1->setQuery("SELECT uid_counter FROM yourtable WHERE class_uid = 'PHYSICS_1' ORDER BY id DESC LIMIT 1");$physics_1 = $physics_1->loadResult()+1;
 
$isnew = $this->getFormModel()->isNewRecord();
 
IF ($isnew && $class_uid == 'MATHS_1'){ return $maths_1; }
IF ($isnew && $class_uid == 'PHYSICS_1'){ return $physics_1; }
ELSE { return $uid_counter; }

Only Calc on Save = Yes
Ajax Calculation = Yes
 
I modify the code and this new code better than before.

PHP:
$class1 = '{yourtable___Class_uid_raw}';
$class2 = JFactory::getDBO();$class2->setQuery("SELECT uid_counter FROM yourtable WHERE Class_uid = '$class1' ORDER BY id DESC LIMIT 1");$class2 = $class2->loadResult()+1;
 
$uid_counter = JFactory::getDBO();$uid_counter->setQuery("SELECT uid_counter FROM yourtable WHERE id = '{yourtable___id}'");$uid_counter = $uid_counter->loadResult();
 
$title1 = JFactory::getDBO();$title1->setQuery("SELECT Class_uid FROM yourtable ORDER BY id DESC LIMIT 1");$title1 = $title1->loadResult();
$title2 = JFactory::getDBO();$title2->setQuery("SELECT Class_uid FROM yourtable WHERE Class_uid = '$class1'");$title2 = $title2->loadResult();
 
$isnew = $this->getFormModel()->isNewRecord();
 
IF ($isnew && $title2 == '') { return '1'; }
IF ($isnew && $class1 == $title1) { return $class2; }
ELSE { return $uid_counter; }
 
Thanks for your help!
It worded at first but after I added some more field something goes wrong.
I can see that it understands if the id is new and puts "1" to counter. But something is not working correctly.
The thing is that it worked perfectly until i added a number of fields.
:(
 
I deleted the older values and it worked. So i close this thread as solved! Thank you very much! You code was very usefull!!!!!!!!!!!
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top