I have 3 programs that I need to create and I am stuck on all of them. It would be awesome if someone could help me out. I'm sure that they are pretty simple to most people.
1. Create a script that calculates the minimum number of fifty-dollar bills, twenty-dollar bills, ten-dollar bills, one-dollar bills, quarters, dimes, nickels, and pennies that you would need based on any dollar amounts entered. Make sure the program checks that no negative numbers can be entered (otherwise print an invalid message to the screen). Create an HTML document containing a form with one text box for the dollar amount. Use the appropriate decision structure to create the program.
For this one I was able to create the HTML document I just have no idea where to begin on the php code.
2. You can determine whether a year is a leap year by testing if it is divisible by 4. However, years that are also divisible by 100 are not leap years, unless they are also divisible by 400, in which case they are leap years. Write a script that allows a user to enter the year and then determines whether the year is a leap year. Use a PHP document and an HTML document containing a form with a single textbox to enter the year. Print a message to the user stating whether the year they entered is a standard year or a leap year.
For this one I was able to create the HTML document and some of the coding, however my logic is incorrect and I'm not sure if I used the functions properly.
3. Write a script that allows a user to enter a numeric grade and then determine the letter grade. Use a PHP document and an HTML document containing a form with 5 text boxes to enter the numeric grades. Print a message to the user stating whether or not the grade is valid (remember the numeric grade can only be between 0-100), and the letter grade for each numeric grade entered. Use the grade determination criteria from the syllabus. Example if a grade between 90-93 is entered, a letter of A- will display to the screen. If only 4 grades were entered, then only 4 letter grades should be listed to the screen. Extra credit: Determine the users GPA based on the grades entered.
For this one I was able to create the HTML document with the 5 textboxes and submit button but I really have no idea how to start the PHP code.
PHP Beginner
Moderator: General Moderators
- Grizzzzzzzzzz
- Forum Contributor
- Posts: 125
- Joined: Wed Sep 02, 2009 8:51 am
Re: PHP Beginner
what PHP do you have so far for question #2?
Re: PHP Beginner
For #1, you simply need to catch the value entered, $dollars, and then do some simple maths- dividing by 50, 20, 10 and so on. Using if/else allows you to only continue if the value is greater than 0.
To accomplish #2, you need to first determine the number of days in any given year using the date() function, and then again, just maths.
I'm not familiar with the US grade system, so I've guessed a bit. Also, I know what GPA stands for, and I assume it's just the average of the numeric grades entered- apologies if it's not.
Code: Select all
$dollars = $_POST["dollars"];
if($dollars <= 0){
echo"Negative amount entered";
}
else{
print 'Number of $20 bills needed: ' . $dollars / 20;
};
Code: Select all
$year = $_POST["year"];
$days = date("z", mktime(0,0,0,12,31,$year)) + 1;
if($days % 4 == 0 && !($days % 10 == 0)){
echo"This is a leap year";
}
elseif($days % 4 == 0 && !($days % 10 == 0) && $days % 400 == 0){
echo"This is a leap year";
}
elseif($days % 4 == 0 && $days % 10 == 0 && !($days % 400 == 0)){
echo"This is not a leap year";
}
else{
echo"Echo this if I messed something up!";
};
Code: Select all
$grade_1 = $_POST["grade1"];
$grade_2 = $_POST["grade2"];
$grade_3 = $_POST["grade3"];
$grade_4 = $_POST["grade4"];
$grade_5 = $_POST["grade5"];
$numeric_grade = array($grade_1, $grade_2, $grade_3, $grade_4, $grade_5);
foreach($numeric_grade as $numeric_grade){
$letter_grade = $numeric_grade >= 90 ? 'A' : ($numeric_grade >= 80 ? 'B' : ($numeric_grade >= 70 ? 'C' : ($numeric_grade >= 60 ? 'D' : ($numeric_grade >= 50 ? 'E' : 'F'))));
print $numeric_grade . ' is grade ' . $letter_grade . '<br />';
};
print'<br />GPA: ' . array_sum($numeric_grade) / count($numeric_grade);
Last edited by MrRSMan on Thu Jun 17, 2010 1:48 pm, edited 5 times in total.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP Beginner
Your instructor probably wants you to calculate #2 but this is easier:
Code: Select all
if(date('L', strtotime($year))) {
echo "$year IS a leap year";
} else {
echo "$year is NOT a leap year.";
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP Beginner
Oh, and really much shorter to calculate it if you need to, but this differs from the date() function, so using this criteria date() doesn't seem correct (hmmm...) maybe a date() bug:
Code: Select all
if(($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {
echo "$year IS a leap year.";
} else {
echo "$year is NOT a leap year.";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.