Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

[SOLVED] Storing a total 29 Oct 2011 17:25 #376

  • Kevin
  • Kevin's Avatar Topic Author
  • Visitor
  • Visitor
I would appreciate it if someone could help me...

My new item form is for an entry form to a fishing competition.

The user will fill in all their details and save it.
I store the data then redirect to a page to allow them to print it.

On the first form I have some bools for entry options

1. Senior £10
2. Any other species £2.50
3. Optional pool £1
4. Optional raffle £1

I also have a field for total and would like this updated according to the bools that are ticked

Something like

total = 0
if 1. Senior £10 is yes total = total +10 (adding £10 to my total)
if 2. Any other species £2.50 is yes total = total + 2.50 (adding £2.50 to my total)
and so on

Then when I save it, update the total in the database along with the other data

Please Log in or Create an account to join the conversation.

Last edit: by Kevin.

Re: Storing a total 29 Oct 2011 22:29 #377

  • Kevin
  • Kevin's Avatar Topic Author
  • Visitor
  • Visitor
By the way, these are the names for the fields I am using.

senior_entry (this is to increase by 10 if true)

aos_entry (this is to increase by 2.5 if true)

optional_pool (this is to increase by 1 if true)

raffle_tickets (this is to increase by 1 if true)

total_due (This will store the sum of all that have been ticked)

Please Log in or Create an account to join the conversation.

Last edit: by Kevin.

Re: Storing a total 31 Oct 2011 15:24 #390

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
Cook don't implements such formules.

Open your controller related to this table, and add in the save() function, all theses calculs.

Something like this:
$myTotal = 0;

if (isset($data['senior_entry']) && $data['senior_entry'])
    $myTotal += 10;

$data['total'] = $myTotal;



Before the save() model call.

Be careful with checkboxes :
If the controller save function is raised by another call without the senior_data value, it must not detect as value = 0.
So, you must check if $data is set.

But if you use a checkbox in the form, it seems not initialize the data if not checked. Personally, I use an hidden field with the same name, and value = 0, Just before the checkbox input.

I don't know if it is the best method.
Hope you understood.


the best is to
Coding is now a piece of cake

Please Log in or Create an account to join the conversation.

Last edit: by admin.

Re: Storing a total 31 Oct 2011 19:22 #395

  • Kevin
  • Kevin's Avatar Topic Author
  • Visitor
  • Visitor
Great,,, thanks for that, I've been trying to work it out for ages.

I added
$myTotal = 0;

	if (isset($data['senior_entry']) && $data['senior_entry'])
		$myTotal += 1000;
	
	if (isset($data['aos_entry']) && $data['aos_entry'])
		$myTotal += 250;
	
	if (isset($data['optional_pool']) && $data['optional_pool'])
    $myTotal += 100;
	
	if (isset($data['raffle_tickets']) && $data['raffle_tickets'])
    $myTotal += 100;

$data['total_due'] = $myTotal/100;

and now it does all the calculations for me and updates the database.

I had to do in in 1000's then divide it by 100 because it wouldn't accept 2.5

Please Log in or Create an account to join the conversation.

Last edit: by Kevin.

Re: Storing a total 01 Nov 2011 17:45 #399

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
Even if you do this ?
if (isset($data['aos_entry']) && $data['aos_entry'])
		$myTotal += (float)2.5;
Coding is now a piece of cake

Please Log in or Create an account to join the conversation.

Re: Storing a total 01 Nov 2011 20:04 #400

  • Kevin
  • Kevin's Avatar Topic Author
  • Visitor
  • Visitor
cool,,thanks again.

Always learning ;)

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.092 seconds

The j-cook project is one of the best of its kind and it is nice that we all try to contribute in little ways to make life easy for each other.

vlemos (Forum)  

Get Started