Jump to content

Need help with my code


klums
Go to solution Solved by PaulRyan,

Recommended Posts

Hello everyone! I've just started learning php and I've been doing alot of different tasks to test myself and what I've learned. I'm currently trying to do this exercise and I think I got it sorted, at least most of it but I need your help sorting out of my coding is good, and how to finish the last task ;)

 

Here is what I need to do:

 

XXX AS is a company with a number of employees. The company wants a webpage that shows a table of employee data with information regarding their names, gender, date of birth, position, position start date, gross salary, tax amount, pension and amount of net salary.

 

 

1: Create your own list of employees and store employee data in an array.

2: Tax amount is based on gross salary amount as following:

 

less than 100k is tax free

100,000 <200,000 is 10 % tax

200,000 <300,000 is 20%

300,000 <500,000 is 35%

>500,000 is 45%

 

3: 2.5 % pension is to be decuted from all gross salary

4: Show all momentary amounts with the two decimal places and thousand separators.

 

This is what I've done so far:

<?php

/*****GROSS SALARY*****
Creates the variabel containing the gross salary data for each employee*/

/*Aron*/
$gross_salary1 = 635000.00;
/*Britney*/
$gross_salary2 = 420000.00;
/*Daniel*/
$gross_salary3 = 260000.00;
/*Jessica*/
$gross_salary4 = 350000.00;
/*Peter*/
$gross_salary5 = 250000.00;
 /*Keith*/
$gross_salary6 = 90000.00;

/*****PENSION*****
Creates the variabel to deduct pension*/
$pension = 0.025;

/*****TAXATION*****
/* Creates if statements that runs trough the salaries and deducts tax rate accordingly for each employee*/

/* Aron and taxes*/
if ($gross_salary1 >= 500000) {
$tax1=0.45; }
if ($gross_salary1 >= 300000 and $gross_salary1 < 500000) {
$tax1=0.35; }
if ($gross_salary1 >= 200000 and $gross_salary1 < 300000) {
$tax1=0.2; }
if ($gross_salary1 >= 100000 and $gross_salary1 < 200000) {
$tax1=0.1; }
if ($gross_salary1 < 100000) {
$tax1=0; }

/* Britney and taxes*/
if ($gross_salary2 >= 500000) {
$tax2=0.45; }
if ($gross_salary2 >= 300000 and $gross_salary2 < 500000) {
$tax2=0.35; }
if ($gross_salary2 >= 200000 and $gross_salary2 < 300000) {
$tax2=0.2; }
if ($gross_salary2 >= 100000 and $gross_salary2 < 200000) {
$tax2=0.1; }
if ($gross_salary2 < 100000) {
$tax2=0; }

/* Daniel and taxes*/
if ($gross_salary3 >= 500000) {
$tax3=0.45; }
if ($gross_salary3 >= 300000 and $gross_salary3 < 500000) {
$tax3=0.35; }
if ($gross_salary3 >= 200000 and $gross_salary3 < 300000) {
$tax3=0.2; }
if ($gross_salary3 >= 100000 and $gross_salary3 < 200000) {
$tax3=0.1; }
if ($gross_salary3 < 100000) {
$tax3=0; }

/* Jessica and taxes*/
if ($gross_salary4 >= 500000) {
$tax4=0.45; }
if ($gross_salary4 >= 300000 and $gross_salary4 < 500000) {
$tax4=0.35; }
if ($gross_salary4 >= 200000 and $gross_salary4 < 300000) {
$tax4=0.2; }
if ($gross_salary4 >= 100000 and $gross_salary4 < 200000) {
$tax4=0.1; }
if ($gross_salary4 < 100000) {
$tax4=0; }

/* Peter and taxes*/
if ($gross_salary5 >= 500000) {
$tax5=0.45; }
if ($gross_salary5 >= 300000 and $gross_salary5 < 500000) {
$tax5=0.35; }
if ($gross_salary5 >= 200000 and $gross_salary5 < 300000) {
$tax5=0.2; }
if ($gross_salary5 >= 100000 and $gross_salary5 < 200000) {
$tax5=0.1; }
if ($gross_salary5 < 100000) {
$tax5=0; }

/* Keitd and taxes*/
if ($gross_salary6 >= 500000) {
$tax6=0.45; }
if ($gross_salary6 >= 300000 and $gross_salary6 < 500000) {
$tax6=0.35; }
if ($gross_salary6 >= 200000 and $gross_salary6 < 300000) {
$tax6=0.2; }
if ($gross_salary6 >= 100000 and $gross_salary6 < 200000) {
$tax6=0.1; }
if ($gross_salary6 < 100000) {
$tax6=0; }

/*****ABC ARRAY*****
/*Sorts all the employees in a multiarray, and also does the tax-pension-salary*/

$abc = array(
'e_1'=>array
('Aron','M','1930/01/25','Manager','1998/01/01',number_format($gross_salary1,2,".",","),number_format($gross_salary1*$tax1,2,".",","),number_format($gross_salary1*$pension,2,".",","),number_format($gross_salary1-$gross_salary1*$pension-$gross_salary1*$tax1,2,".",",")),
'e_2'=>array
('Britney','F','2001/05/06','Researcher','2001/03/15',number_format($gross_salary2,2,".",","),number_format($gross_salary2*$tax2,2,".",","),number_format($gross_salary2*$pension,2,".",","),number_format($gross_salary2-$gross_salary2*$pension-$gross_salary2*$tax2,2,".",",")),
'e_3'=>array
('Daniel','M','2003/01/15','Officer','2003/12/06',number_format($gross_salary3,2,".",","),number_format($gross_salary3*$tax3,2,".",","),number_format($gross_salary3*$pension,2,".",","),number_format($gross_salary3-$gross_salary3*$pension-$gross_salary3*$tax3,2,".",",")),
'e_4'=>array
('Jessica','F','2002/11/21','Officer','2007/02/20',number_format($gross_salary4,2,".",","),number_format($gross_salary4*$tax4,2,".",","),number_format($gross_salary4*$pension,2,".",","),number_format($gross_salary4-$gross_salary4*$pension-$gross_salary4*$tax4,2,".",",")),
'e_5'=>array
('Peter','M','1998/01/07','Assisant','2009/09/06',number_format($gross_salary5,2,".",","),number_format($gross_salary5*$tax5,2,".",","),number_format($gross_salary5*$pension,2,".",","),number_format($gross_salary5-$gross_salary5*$pension-$gross_salary5*$tax5,2,".",",")),
'e_6'=>array
('Keitd','M','2003/07/25','Intern','2012/06/27',number_format($gross_salary6,2,".",","),number_format($gross_salary6*$tax6,2,".",","),number_format($gross_salary6*$pension,2,".",","),number_format($gross_salary6-$gross_salary6*$pension-$gross_salary6*$tax6,2,".",",")));

/*Sorts the employee array by the names of the employees by default when loading the page*/
array_multisort($abc,SORT_ASC);

/*****TABLE&SUCH******
/*defines the border scale  */

echo '<table border="7">';

/*Prints out a table witd all the information headlines regarding the table and its employees*/
echo '<tr><td><center><strong><big>','Name','</strong></big></center></td>'
.'<td><center><strong><big>', 'Gender','</strong></big></center></td>'
.'<td><center><strong><big>', 'DOB','</strong></big></center></td>'
.'<td><center><strong><big>', 'Position','</center></strong></big></td>'
.'<td><center><strong><big>', 'Start Date','</strong></big></center></td>'
.'<td><strong><big>','Gross Salary','</strong></big></td>'
.'<td><center><strong><big>', 'Tax','</center></strong></big></td>'
.'<td><center><strong><big>', 'Pension','</center></strong></big></td>'
.'<td><center><strong><big>', 'Net Salary','</strong></big></center></td>';

/*Puts the data of the employees into the table*/
foreach ($abc as $abct)
{
echo
'<tr><td><div align="left">', $abct[0],'</div></td>'
.'<td><center>', $abct[1],'</center></td>'
.'<td><center>', $abct[2],'</center></td>'
.'<td><div align="left">', $abct[3],'</div></td>'
.'<td><center>', $abct[4],'</center></td>'
.'<td><div align="right">', $abct[5],'</div></td>'
.'<td><div align="right">', $abct[6],'</div></td>'
.'<td><div align="right">', $abct[7],'</div></td>'
.'<td><div align="right">', $abct[8],'</div></td>' . '</tr>';
}
echo '</table>';/*Ends the table*/

 ?>

 

Any help on sorting the code better would be appreciated ;) Again I'm not the best..

 

AND I would love help with the last task which is:

 

5: The page should show the listing of the data in a sorted order depending on a selected sort field and sort order (ascending and descending). The default sorting should be by name.

 

 

Thanks guys you're awsome <3

Link to comment
Share on other sites

Your code can be dramatically reduced, yes. Your doing a lot of code repetition.

 

When you start to name variables like $var1, $var2, $var3 etc you should be using arrays.

 

When you start to do repetitive code such as lines 26 to 113 you should be using loops.

 

So if I want to list the numbers 1 to 1 million. Im not going to sit here and type all the numbers 1 to 1 million. I'm going to use a loop.

for($i = 0; $i < 1000000; $i++) {
   echo "$i<br />";
}

In 3 lines of code I have generated 1 million lines!

Edited by Ch0cu3r
Link to comment
Share on other sites

  • Solution

I agree with Ch0cu3r, you have far too much repetition in your code. You have also miscalculated the amount on the pension, it is 2.5% not 25%. So the multiplier would be 0.025 not 0.25.

 

I have restructured your code as follows, I haven't done anything.

<?PHP

  //### Function to return tax amount
  function tax_amount($salary) {
    if($salary >= 500000) {
      return $salary*0.45;
    } else if($salary >= 300000) {
      return $salary*0.35;
    } else if($salary >= 200000) {
      return $salary*0.2;
    } else if($salary >= 100000) {
      return $salary*0.1;
    } else {
      return 0;
    }  
  }

  //### Set pension contribution amount
  $pensionContribution = 0.025;

  //### Employee data in an array
  $employees = array(array('name' => 'Aron',    'gender' => 'm', 'dob' => '1930-01-25', 'position' => 'manager',    'start_date' => '1998-01-01', 'gross_salary' => 635000.00),
                     array('name' => 'Britney', 'gender' => 'f', 'dob' => '2001-05-06', 'position' => 'researcher', 'start_date' => '2001-03-15', 'gross_salary' => 420000.00),
                     array('name' => 'Daniel',  'gender' => 'm', 'dob' => '2003-01-15', 'position' => 'officer',    'start_date' => '2003-12-06', 'gross_salary' => 260000.00),
                     array('name' => 'Jessica', 'gender' => 'f', 'dob' => '2002-11-21', 'position' => 'officer',    'start_date' => '2007-02-20', 'gross_salary' => 350000.00),
                     array('name' => 'Peter',   'gender' => 'm', 'dob' => '1998-01-07', 'position' => 'assistant',  'start_date' => '2009-09-06', 'gross_salary' => 250000.00),
                     array('name' => 'Keith',   'gender' => 'm', 'dob' => '2003-07-25', 'position' => 'intern',     'start_date' => '2012-06-27', 'gross_salary' => 90000.00)
                    );
                
  //### Iterate over employee data                  
  foreach($employees AS $employee) {
 
    //### Add tax amount, pension amount and net salary
    $employee['tax_amount']     = tax_amount($employee['gross_salary']);
    $employee['pension_amount'] = ($employee['gross_salary']*$pensionContribution);
    $employee['net_salary']     = (($employee['gross_salary']-$employee['pension_amount'])-$employee['tax_amount']);
 
    //### Display employee data
    echo '<pre>';
    print_r($employee);
    echo '</pre>';
  }
 
?>

As you can see, it is a lot easier to read and see what data is where. I also created a function to return tax amount.

I calculated the tax amount, pension amount and net salary during the while loop so it's less code to write.

Edited by PaulRyan
Link to comment
Share on other sites

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.