Welcome, Guest
Username: Password: Remember me

TOPIC: [SOLVED] Storing a total

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

  • Kevin
  • Kevin's Avatar
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
Last Edit: 31 Oct 2011 19:25 by Kevin.
The administrator has disabled public write access.

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

  • Kevin
  • Kevin's Avatar
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)
Last Edit: 29 Oct 2011 22:30 by Kevin.
The administrator has disabled public write access.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
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
Last Edit: 31 Oct 2011 15:25 by admin.
The administrator has disabled public write access.

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

  • Kevin
  • Kevin's Avatar
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
Last Edit: 31 Oct 2011 19:24 by Kevin.
The administrator has disabled public write access.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Even if you do this ?
if (isset($data['aos_entry']) && $data['aos_entry'])
		$myTotal += (float)2.5;
Coding is now a piece of cake
The administrator has disabled public write access.

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

  • Kevin
  • Kevin's Avatar
cool,,thanks again.

Always learning ;)
The administrator has disabled public write access.
Time to create page: 0.088 seconds

Get Started