You are viewing our Forum Archives. To view or take place in current topics click here.
Need some PHP help.
Posted:

Need some PHP help.Posted:

Boogeyman_
  • TTG Natural
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
New to PHP programming. This assignment is for one of my college classes.

Goal:

How do I break the boxes so each one of them are on separate lines?

[ Register or Signin to view external links. ]


Using XAMPP

Here is the code that I currently have:



<?php
    $num_boxes = 4;
    $precision = 1;
    $gradetext = '';
                    //minimum=>grade
    $grade_array = array(
                    '90-100'=>'A',
                   
                    '80-89'=>'B',
                   
                    '70-79'=>'C',
                   
                    '60-69'=>'D',
                   
                    '0-59'=>'F'
                    );
function create_form($num_boxes=1)
{
    if(is_int($num_boxes))
    {
        $form = '';
        for($i=0;$i<$num_boxes;$i++)
        {
            $inc = $i+1;
            $val = (isset($_POST['grade'][$i])) ? $_POST['grade'][$i] : '';
            $form .= "<label for='grade$inc'>Grade $inc:</label><input id='grade$inc' name='grade[$i]' type='number' value = '$val' min='0' max='100' />";
        }
        return $form;
    }
}
function get_grade($pc,$grade_array)
{
    foreach($grade_array as $min=>$grade)
    {
        if($pc < $min) continue;
        return $grade;
    }
}
if($_POST)
{
    $GPA = 0;
    $num_posts = count(array_filter($_POST['grade'], "is_numeric"));
    $total = array_sum($_POST['grade']);
    if($num_posts != 0) $GPA = $total/$num_posts;
    $lettergrade = get_grade($GPA,$grade_array);
    $gradetext = "Average Test Score = " . number_format($GPA,$precision) . " ($lettergrade)";
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test Score Calculator</title>
</head>
<center>
<body>
<?php echo $gradetext;?>
<form method="post">
    <?php echo create_form($num_boxes);?>
    <input type="submit" value="Calculate" />
</form>
</body>
</center>
</html>





Last edited by Boogeyman_ ; edited 8 times in total
#2. Posted:
7en
  • V5 Launch
Status: Offline
Joined: Aug 16, 201212Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201212Year Member
Posts: 598
Reputation Power: 29
That is not a problem, that is a goal. So what is your problem? You don't know how to finish your solution? You have all the hard work done, now all you need to do is find a way to print out the result!

You also misspelled filter_input.
#3. Posted:
Boogeyman_
  • TTG Natural
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
7en wrote That is not a problem, that is a goal. So what is your problem? You don't know how to finish your solution? You have all the hard work done, now all you need to do is find a way to print out the result!

You also misspelled filter_input.


Correct, I don't know how to finish my solution.
#4. Posted:
Boogeyman_
  • TTG Natural
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
Any help would be much appreciated.
#5. Posted:
speed
  • Summer 2020
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
[ Register or Signin to view external links. ]
#6. Posted:
Boogeyman_
  • TTG Natural
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
Still need some help with this.

If anyone could help me that would be awesome!
#7. Posted:
Cyimking
  • 2 Million
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Steps..
1) Check if the POST request is set (or see if someone made a post request). (If statement)

1.1) Yes someone made a post request (submitted form). Normally you would clean the input then validate the input but since this seems like a school project I won't cover it. Anyways, now you have to solve your problem. Basic logic means you calculate the grade. The formula for your example is (x1+..+x4) / ( 400). Basic php math.

1.2) Simply echo it. *Check PHP manual.

AKA move this
Answer: <?php echo ($testscore1 + $testscore2 + $testscore3 + $testscore4);
inside the if statement or else you will end up with errors.

2) If No then just do nothing. The program will show the table.
#8. Posted:
Boogeyman_
  • TTG Natural
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
Alright, I figured my code out. My new goal is in the OP.
#9. Posted:
speed
  • Summer 2020
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
<br> is what you're looking for.

It's not "proper", but just change

$form .= "<label for='grade$inc'>Grade $inc:</label><input id='grade$inc' name='grade[$i]' type='number' value = '$val' min='0' max='100' />";


To

$form .= "<label for='grade$inc'>Grade $inc:</label><input id='grade$inc' name='grade[$i]' type='number' value = '$val' min='0' max='100' /><br>";
#10. Posted:
Boogeyman_
  • TTG Natural
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
Status: Offline
Joined: Aug 06, 201113Year Member
Posts: 935
Reputation Power: 36
speed wrote <br> is what you're looking for.

It's not "proper", but just change

$form .= "<label for='grade$inc'>Grade $inc:</label><input id='grade$inc' name='grade[$i]' type='number' value = '$val' min='0' max='100' />";


To

$form .= "<label for='grade$inc'>Grade $inc:</label><input id='grade$inc' name='grade[$i]' type='number' value = '$val' min='0' max='100' /><br>";


Holy crap, thank you! Appreciate it.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.