Jump to content

Commission calculator


Go to solution Solved by requinix,

Recommended Posts

I am debugging code for an assignment that the instructor has assured me only involves brackets and semicolons, yet the commission rate calculator never runs correctly no matter what I do. It should make the commission rate 0.03 if items sold is over 100 and 0.02 if under. I just want to know how to make this calculator work. I haven't been able to dig up an example of something similar and it's driving me nuts. I would really appreciate it if someone could point out what I am missing. 

 

// Check for form submission:
if (isset($_POST['submitted'])) {
 
// Minimal form validation:
if ( is_numeric($qtySold) && is_numeric($price) ) {
 
// Print the heading:
echo '<h1>Commission</h1>';
 
if ($qtySold >= 100){
($commissionRate == 0.03);
}else{
($commissionRate == 0.02);
 
$commission =  $total * $commissionRate;
 
// Print the results:
echo "<p>Your commission is <b> $commission</b>.";
}
}else{
echo "You forgot to enter values!";
}
}
Link to comment
https://forums.phpfreaks.com/topic/287623-commission-calculator/
Share on other sites

Protip: when developing with PHP, make sure you have your environment set up to warn you about potential (or actual) problems. Open your php.ini, then find and change the values for two settings:

error_reporting = -1
display_errors = on
Then restart your web server.

 

After that, your script should output warnings about how "commissionRate" is undefined. Twice per script, it seems. The problem is that the two lines that try to define it, aren't.

($commissionRate == 0.03);
Putting aside how the parentheses are pointless, == is for comparison. You want = for assignment.

$commissionRate = 0.03;
Change the other one too.

Thanks for the tip, I have tried the error reporting but I get an error for the error_reporting line. I swear the php is just mocking me now. I have tried with and without the ==, =, (, in various combinations just to get something that works. Nothing helped. Here's the latest best working out of many version I have now: 

<?php # Script 3.10 - Errors.php #5

error_reporting = -1
display_errors = on

$page_title = 'Errors';

// Set the variable number
$qtySold = $_POST["qtySold"];
$price = $_POST["price"];
$total = $qtySold * $price;


// Check for form submission:
if (isset($_POST['submitted'])) {

	// Minimal form validation:
	if ( is_numeric($qtySold) && is_numeric($price) ) {
	
		// Print the heading:
		echo '<h1>Commission</h1>';

		if ($qtySold >= 100){
			$commissionRate = 0.03;
		}else{
			$commissionRate = 0.02;

		$commission = $total * $commissionRate;

		// Print the results:
		echo "<p>Your commission is <b> $commission</b>.";
		}
		}
		else
		{
			echo "You forgot to enter values!";
		}			
}

This version doesn't give the "You forgot to enter values!" right off the bat, which is an improvement. It's still giving me 0.03 commission rate for all numbers under 100. I have even tried switching the >= to a <= to get a different result and it spit out the same result. Again, I think it's mocking me. 

Edited by LostinGA
  • Solution

That else you have for the commissionRate stuff? Its closing } is in the wrong place. The indentation is a reminder for it.

if ($qtySold >= 100){
	$commissionRate = 0.03;
}else{
	$commissionRate = 0.02;
// should go right here
Actually what you've described doesn't match up with what the code says, but the brace } thing is a bug.

 

Thanks for the tip, I have tried the error reporting but I get an error for the error_reporting line. I swear the php is just mocking me now. I have tried with and without the ==, =, (, in various combinations just to get something that works. Nothing helped.

The php.ini. Those two things are settings in the php.ini. You know what, don't worry about it.

No luck. I tried this:

// Check for form submission:
if (isset($_POST['submitted'])) {

	// Minimal form validation:
	if ( is_numeric($qtySold) && is_numeric($price) ) {
	
		// Print the heading:
		echo '<h1>Commission</h1>';

		if ($qtySold >= 100){
			$commissionRate = 0.03;
		else{
			$commissionRate = 0.02;
		}
		$commission = $total * $commissionRate; 

and this: 

// Check for form submission:
if (isset($_POST['submitted'])) {

	// Minimal form validation:
	if ( is_numeric($qtySold) && is_numeric($price) ) {
	
		// Print the heading:
		echo '<h1>Commission</h1>';

		if ($qtySold >= 100){
			$commissionRate = 0.03;
		
			$commissionRate = 0.02;
		}else{
		$commission = $total * $commissionRate; 

and this: 

// Check for form submission:
if (isset($_POST['submitted'])) {

	// Minimal form validation:
	if ( is_numeric($qtySold) && is_numeric($price) ) {
	
		// Print the heading:
		echo '<h1>Commission</h1>';

		if ($qtySold >= 100){
			$commissionRate = 0.03;
		}else{
			$commissionRate = 0.02;
//up from below
		}
		$commission = $total * $commissionRate;

		// Print the results: 

All give me the same issues - nothing works for any number over 100 and the wrong commission rate for all numbers under 100. I would happily re-write the code, but we are not supposed to. Thanks again for taking a look at it, it help knowing there isn't a glaring error in it that I am missing. 

 

 

I am debugging code for an assignment that the instructor has assured me only involves brackets and semicolons

So your instructor has given you code which has errors, which is intentional, and it is your task to track down those errors and correct them.

 

The first step you need to do then is to enable error reporting as requinix suggested in his earlier post. If you do have access to the php.ini then you can still enable error reporting by adding these two lines at the top of the script (after the opening <?php tag)

ini_set('display_errors', 1);
error_reporting(E_ALL);

With error reporting enabled PHP will display error messages. By reading the error message you should be able to solve the errors on your own.

In case you are wondering the php was right and I was wrong. I had several versions of this on the server (about 12 or so with different names), I had the php right in a lot of the versions, but the html was still referencing the first version I worked with.  You are right the error messages should have been working and they weren't displaying any errors, which got me thinking and then it hit me that the html was grabbing the first file over and over again.  Thank you for trying to help, I really thought I was going nuts because the instructor told me the php was right and you guys did here as well, yet it wasn't working. I will never leave old versions sitting on the server again. That was a hard way to learn that lesson. Thanks again. :)

Edited by LostinGA
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.