Help Me Add a Simple Calculation to the Application Form Mod

Discussion forum for MOD Writers regarding MOD Development.
Locked
User avatar
victory1
Registered User
Posts: 935
Joined: Sun Oct 10, 2010 6:47 pm
Contact:

Help Me Add a Simple Calculation to the Application Form Mod

Post by victory1 »

I'm using the Application form Mod here (http://www.phpbb.com/customise/db/mod/application_form/) to redeem points on my board.
I want to take the price value enter by the user and calculate how many points it will cost them. The points are rounded to the nearest .50 and each .50 cost 100 points ($.50 = 100 points)

Example, if user enters $3.20 then it's rounded to $3.00 and that should be 600 points.
if user enters $3.55 then it's rounded to $3.50 and that should be 700 points

For the example above
I want to add a line in the automatic posting at the end that says:
This request will cost you 600 SRF Cash!
This request will cost you 700 SRF Cash!

I think I have the formula down but not sure how to pull a value from the form

Code: Select all

$average = price; // that one stumped me since price should be the user input answer
$round_to = 0.5; // that's what I want to rounded it to the nearest half
$rounded = round($average / $round_to) * $round_to;
$srfpoints = ($rounded / $round_to) * 100;
$srfpoints = srfcash;
The application is for members that want to apply for a staff position. I had to change all the fields to make it work for me since I wanted to change it to a form were my members can redeem e-books.
I made 2 sub-forums to accommodate the form, a link that leads to the actual form and a regular sub-forum were the user answers are written.
Screenshot
http://i.imgur.com/JhwBn.jpg
- If you click on the link Redeem Form Link, it will bring the application form up where you can input your answers. It looks like this (remember, I changed the fields):
http://i.imgur.com/DPg9T.jpg
- The answers will then be written in the Redeem Status forum (you can set up your permission for that forum for staff only if you choose). It looks like this:
http://i.imgur.com/kYB3v.jpg

Note: So for the request on the screen-shot, the $7.99 would be rounded to $8.00 so after Request Description, I want it to say:
This request will cost you 1600 SRF Cash!
User avatar
dellsystem
Former Team Member
Posts: 3879
Joined: Sat Apr 09, 2005 8:54 pm
Location: Montreal
Name: Wendy
Contact:

Re: Help Me Add a Simple Calculation to the Application Form

Post by dellsystem »

I'd imagine you could get the user input value for the price using request_var('price', 0.0) (with the second argument casting it to a float, which seems to be what you want assuming that the $ dollar sign is not an expected part of the input). Have you tried that?

For the actual rounding, your formula looks good although if you got rid of the * $round_to at the end of the third line and the division by $round_to in the fourth line it will probably work the same :)

Also, what is the line $srfpoints = srfcash; supposed to do?
Former moderator and website team member | My MODs, and more (GitHub)
User avatar
victory1
Registered User
Posts: 935
Joined: Sun Oct 10, 2010 6:47 pm
Contact:

Re: Help Me Add a Simple Calculation to the Application Form

Post by victory1 »

I already have the request_var (price). I need to be able to pull that answer for the last field. Here's the code so far. So now I need to add my computation and call it as the last field using price. That's where I'm lost.

Code: Select all

// Let's set the configuration, this is the ID of the forum where the post goes to
$forumid_send = 32;

$submit = (isset($_POST['submit'])) ? true : false;

	if ($submit)
	{

// Setting the variables we need to submit the post to the forum where all the applications come in
$apply_subject  = sprintf($user->lang['APPLICATION_SUBJECT'], $user->data['username']);
$apply_post     = sprintf($user->lang['APPLICATION_MESSAGE'], $user->data['username'], request_var('postion', '', true), utf8_normalize_nfc(request_var('title', '', true)), utf8_normalize_nfc(request_var('author', '', true)), utf8_normalize_nfc(request_var('price', '', true)), utf8_normalize_nfc(request_var('bookstore', '', true)), utf8_normalize_nfc(request_var('booklink', '', true)), utf8_normalize_nfc(request_var('why', '', true)));

// variables to hold the parameters for submit_post
$poll = $uid = $bitfield = $options = ''; 

generate_text_for_storage($apply_post, $uid, $bitfield, $options, true, true, true, true, true, true, true);

$data = array( 
	'forum_id'		=> $forumid_send,
	'icon_id'		=> false,

	'enable_bbcode'		=> true,
	'enable_smilies'	=> true,
	'enable_urls'		=> true,
	'enable_sig'		=> true,

	'message'		=> $apply_post,
	'message_md5'	=> md5($apply_post),
				
	'bbcode_bitfield'	=> $bitfield,
	'bbcode_uid'		=> $uid,

	'post_edit_locked'	=> 0,
	'topic_title'		=> $apply_subject,
	'notify_set'		=> false,
	'notify'			=> false,
	'post_time' 		=> 0,
	'forum_name'		=> '',
	'enable_indexing'	=> true,
);

// Sending the post to the forum set in configuration above
submit_post('post', $apply_subject, '', POST_NORMAL, $poll, $data);
It's being call in the language file application.php in the mods folder

Code: Select all

'APPLICATION_MESSAGE'			=> 'SRF Cash Redeem Application from a member by the user name of [b] %1$s[/b].<br /><br />[b]Would like to[/b]: %2$s<br />[b]Book Title[/b]: %3$s<br />[b]Authors Name[/b]: %4$s<br />[b]Book Price[/b]: %5$s<br />[b]Bookstore Name[/b]: %6$s<br />[b]Link to Book[/b]: %7$s<br /><br />[b]Description of request:[/b]<br /> %8$s<br />This request will cost you %9$s SRF Cash!',
User avatar
AGC
Registered User
Posts: 292
Joined: Sat Sep 15, 2007 10:26 pm
Contact:

Re: Help Me Add a Simple Calculation to the Application Form

Post by AGC »

victory1 wrote:I think I have the formula down but not sure how to pull a value from the form

Code: Select all

    $average = price; // that one stumped me since price should be the user input answer
    $round_to = 0.5; // that's what I want to rounded it to the nearest half
    $rounded = round($average / $round_to) * $round_to;
    $srfpoints = ($rounded / $round_to) * 100;
    $srfpoints = srfcash;
The round function do it for you, so you don't need to multiple by half.
So if the price is 3.3 it'll be rounded to 3 if it's 3.55 it'll be rounded to 4.
Then you need to multiple it by 200 not 100.
(phpBB 2.0.x): []Admin Topics List[] MOD. << Description & Download. , []Highlight Author[] MOD.

Donate-paypal
(phpBB 3.0.x)[ver] MODs: [RC1] >Import old *.pak files MOD. + . . .[8] > ACP - Modules Quick Access (MQA) MOD. + . . . [7-PL1] > Multi Smile (actions) MOD
User avatar
victory1
Registered User
Posts: 935
Joined: Sun Oct 10, 2010 6:47 pm
Contact:

Re: Help Me Add a Simple Calculation to the Application Form

Post by victory1 »

Thanks. I'm more in need of where and how to plugin the formula and call it.
User avatar
victory1
Registered User
Posts: 935
Joined: Sun Oct 10, 2010 6:47 pm
Contact:

Re: Help Me Add a Simple Calculation to the Application Form

Post by victory1 »

When I plug my formula in the application.php, I'm getting a debug error that price is an undeclared variable. Also nothing is writing anymore, I get a 0 in the body of the post.
User avatar
RMcGirr83
Former Team Member
Posts: 22016
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: Help Me Add a Simple Calculation to the Application Form

Post by RMcGirr83 »

You will probably get more help if you post up your entire php file instead of posting snippets of the file.
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
User avatar
victory1
Registered User
Posts: 935
Joined: Sun Oct 10, 2010 6:47 pm
Contact:

Re: Help Me Add a Simple Calculation to the Application Form

Post by victory1 »

RMcGirr83 wrote:You will probably get more help if you post up your entire php file instead of posting snippets of the file.
You're right. Here's the entire thing. I have not plugged in the calculation yet.

Code: Select all

<?php
/**
* @package application.php
* @copyright (c) JimA http://beta-garden.com 2009
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/application');

// You need to login before being able to send out an application
if ($user->data['user_id'] == ANONYMOUS)
{
    login_box('', $user->lang['LOGIN_APPLICATION_FORM']);
}

include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);

// Let's set the configuration, this is the ID of the forum where the post goes to
$forumid_send = 32;

$submit = (isset($_POST['submit'])) ? true : false;

	if ($submit)
	{

// Setting the variables we need to submit the post to the forum where all the applications come in
$apply_subject  = sprintf($user->lang['APPLICATION_SUBJECT'], $user->data['username']);
$apply_post     = sprintf($user->lang['APPLICATION_MESSAGE'], $user->data['username'], request_var('postion', '', true), utf8_normalize_nfc(request_var('title', '', true)), utf8_normalize_nfc(request_var('author', '', true)), utf8_normalize_nfc(request_var('price', '', true)), utf8_normalize_nfc(request_var('bookstore', '', true)), utf8_normalize_nfc(request_var('booklink', '', true)), utf8_normalize_nfc(request_var('why', '', true)));

// variables to hold the parameters for submit_post
$poll = $uid = $bitfield = $options = ''; 

generate_text_for_storage($apply_post, $uid, $bitfield, $options, true, true, true, true, true, true, true);

$data = array( 
	'forum_id'		=> $forumid_send,
	'icon_id'		=> false,

	'enable_bbcode'		=> true,
	'enable_smilies'	=> true,
	'enable_urls'		=> true,
	'enable_sig'		=> true,

	'message'		=> $apply_post,
	'message_md5'	=> md5($apply_post),
				
	'bbcode_bitfield'	=> $bitfield,
	'bbcode_uid'		=> $uid,

	'post_edit_locked'	=> 0,
	'topic_title'		=> $apply_subject,
	'notify_set'		=> false,
	'notify'			=> false,
	'post_time' 		=> 0,
	'forum_name'		=> '',
	'enable_indexing'	=> true,
);

// Sending the post to the forum set in configuration above
submit_post('post', $apply_subject, '', POST_NORMAL, $poll, $data);

$message = $user->lang['APPLICATION_SEND'];
$message = $message . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.$phpEx") . '">', '</a>');
trigger_error($message);
}

$template->assign_block_vars('navlinks', array(
  'FORUM_NAME'    => $user->lang['REDEEM'],
  'U_VIEW_FORUM'  => append_sid("{$phpbb_root_path}viewforum.php?f=31"),
));

$template->assign_block_vars('navlinks', array(
  'FORUM_NAME'    => $user->lang['APPLICATION'],
  'U_VIEW_FORUM'  => append_sid("{$phpbb_root_path}application.$phpEx"),
));

page_header($user->lang['APPLICATION_PAGETITLE']);

$template->assign_vars(array(
	'PROCESS_APPFORM'	=> append_sid("{$phpbb_root_path}application.$phpEx"),
	));
	
$template->set_filenames(array(
    'body' => 'appform_body.html',
));

page_footer();

?>
Last edited by victory1 on Fri Jun 03, 2011 11:55 am, edited 1 time in total.
User avatar
RMcGirr83
Former Team Member
Posts: 22016
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: Help Me Add a Simple Calculation to the Application Form

Post by RMcGirr83 »

You are treating price as a string instead of an integer. Calculations are done on integers, not on strings

Place this before your submit check

Code: Select all

$price = request_var('price', 0);
$round_to = 0.5; // that's what I want to rounded it to the nearest half
$rounded = round($price / $round_to) * $round_to;
$srfpoints = ($rounded / $round_to) * 100;
and use this for the $apply_post

Code: Select all

$apply_post     = sprintf($user->lang['APPLICATION_MESSAGE'], $user->data['username'], request_var('postion', '', true), utf8_normalize_nfc(request_var('title', '', true)), utf8_normalize_nfc(request_var('author', '', true)), $price, utf8_normalize_nfc(request_var('bookstore', '', true)), utf8_normalize_nfc(request_var('booklink', '', true)), utf8_normalize_nfc(request_var('why', '', true)), $srfpoints);
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
User avatar
victory1
Registered User
Posts: 935
Joined: Sun Oct 10, 2010 6:47 pm
Contact:

Re: Help Me Add a Simple Calculation to the Application Form

Post by victory1 »

Thank you RMcGirr83! I just had to change this $price = request_var('price', 0); to $price = request_var('price', '', true); because I was getting a 0 value instead of the user's input. Once I did that, it all worked perfectly! :D
User avatar
RMcGirr83
Former Team Member
Posts: 22016
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: Help Me Add a Simple Calculation to the Application Form

Post by RMcGirr83 »

You should validate your inputs otherwise someone can enter into price "something like this" which will cause unexpected results and as I said price should be treated as an int, by changing it to ' ' you are treating it as a string and price doesn't need multibyte which is what the "true" does the way you have it now.
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
User avatar
victory1
Registered User
Posts: 935
Joined: Sun Oct 10, 2010 6:47 pm
Contact:

Re: Help Me Add a Simple Calculation to the Application Form

Post by victory1 »

RMcGirr83 wrote:You should validate your inputs otherwise someone can enter into price "something like this" which will cause unexpected results and as I said price should be treated as an int, by changing it to ' ' you are treating it as a string and price doesn't need multibyte which is what the "true" does the way you have it now.
Thanks, I fixed it. Because my users are entering a price with a decimal points, it was not working. So I added a float variable and then it worked ($price = request_var('price', 0.0));.
Locked

Return to “[3.0.x] MOD Writers Discussion”