Search code examples
phphtmlformsprintingrating

PHP Default value and Print Issues


I am currently trying to make my php, on the same page as the form, display the values correctly and calculate the overall passer rating. My calculations follow the formatting this way:

C = ((# completions/#attempts) *100 -30)/20

Y= ((# yards/#attempts) -3)/4

T = ((#touchdowns/#attempts) *20)

I = 2.375 - (#interceptions/#attempts) * 35

Pass Rating = ((C+Y+T+I)/6) *100

The table that holds the values from the form should have a default value of 0 and below it should print the overall rating of Poor, Good and Great depending on the passer rating.

As you see I have a few issues. My issues are:

The Passing Rating doesn't default to 0.

My Overall Rating doesn't print whether it is Poor, Good, or Great. It only prints Poor

I was hoping someone can explain this. I have been troubleshooting it for hours now. No luck sadly. ps(overall rating printed twice to see which version I want to use)

Also the snippet won't run the php unless you have php on your comp. Need a server to see it even with the snippet I guess

UPDATE: I put 20 instead of 4 to divide in Y value. That fixed my calculations but not the overall rating.

SECOND UPDATE: I added the line:

if(($_POST['First'] != '') && ($_POST['Last'] != ''))

so that I can keep my defaults for the table.

My last issues is now the Overall Rating displaying the correct label of either Poor, Good or Great

<?php

$first = "";
$last = "";
$completions = 0;
$attempts = 0;
$yards = 0;
$touchdowns = 0;
$interceptions = 0;
$TotalScores = 0;

if(isset($_POST['First'])) {
        $first = $_POST['First'];
}

if(isset($_POST['Last'])){

        $last = $_POST['Last'];
}

if(isset($_POST['completions'])) {

	$completions = $_POST['completions']; 
}
if(isset($_POST['attempts'])) {
        $attempts = $_POST['attempts'];
}

if(isset($_POST['yards'])){

        $yards = $_POST['yards'];
}

if(isset($_POST['touchdowns'])) {

	$touchdowns = $_POST['touchdowns']; 
}


if(isset($_POST['interceptions'])) {

        $interceptions = $_POST['interceptions'];
}


function rating ($com, $att, $yards, $touchd, $inter){

	//$C = 0;
	//$Y = 0;
	//$T = 0;
	//$I = 0;
	$passRating = 0;

	

	$C = ((($com /$att)*100)-30) / 20;
	$Y = (($yards/$att)-3)/4;
	$T = ($touchd/$att)*20;
	$I = 2.375 - (($inter/$att)*35);

	$passRating = (($C + $Y + $T + $I)/6)*100;

	return $passRating;
}


if(is_numeric($completions) && is_numeric($attempts) && is_numeric($yards)
    && is_numeric($touchdowns) && is_numeric($interceptions)) {

    //if(($_POST['completions'] >0) && ($_POST['attempts'] >0) && ($_POST['yards'] >0)
    //      && ($_POST['touchdowns'] >0) && ($_POST['interceptions'] >0) ){


if(($_POST['First'] != '') && ($_POST['Last'] != '')){

    $TotalScore = rating($completions, $attempts, $yards,
                            $touchdowns, $interceptions);
    //echo $TotalScore;
    if ($TotalScore < 85 && $TotalScore >0){
    $score = "Poor";
    }

    elseif($TotalScore >=85 && $TotalScore <90){
    $score = "Mediocre";
    }
    elseif ($TotalScore >=90 && $TotalScore <95){
    $score = "Good";
    }

    elseif ($score >= 95){
    $score = "Great";
    }
    }
//}
}
else {

    $score = "Invalid Input!";
    //echo $TotalScore;
}

?>
.form-container {

	padding-right: 20px;



}

fieldset {

	width: 200px;
	height: 30px;	
	padding: 5px;


}

input {
	padding-bottom: 5px;

}



#text-container {
	margin-top: 100px;
	width: 1260px;
	height: 400px;
	background-color: white;
	text-align: left;
	margin-left: auto;
	margin-right: auto;
	border-radius: 10px;
	
}

#text-container p {

	margin-left: 30px;
	font-size: 20px;

}

#text-container h1 {

	margin-left: 30px;
	color: #4EA24E;
	padding-top: 10px;

}

#paragraph {

	position: absolute;
	width: 1350px;
	height: 600px;
	border: 1px solid black;
	margin-left: 500px;
	margin-top: 60px;


}


.signup {

	float: right;
	height: 600px;
	width: 500px;
	border: 1px solid black;
	background-color: blue;


}

#form-box {

	margin-top: 10px;
	width: 550px;
	height:600px;
	maring-left: 0;
	float: left;
	/*background-color: #B2D1F0;*/
	/*border-radius: 30px;*/
	/*box-shadow: 0 0 10px black;*/
}

#form-box label {

	float: left;
    	width: 200px;
    	text-align: right;
    	margin-right: 10px;
    	white-space: nowrap;
    	overflow: hidden;
    	text-overflow: ellipsis;
    	-o-text-overflow: ellipsis;
	font-size: 20px;
	margin-bottom: 30px;
	margin-left: 20px;
}

#form-box input[type="text"] {

	margin-bottom: 30px;
	height: 20px;
	width: 200px;
	font-size: 15px;
	margin-left: 10px;
	box-shadow: 0 0 5px black;

}
.numInput input[type="text"] {

        margin-bottom: 30px;
        height: 20px;
        width: 50px;
        font-size: 15px;
        margin-left: 10px;
        box-shadow: 0 0 5px black;
}
#form-box button {
	margin-bottom: 30px;
	height: 35px;
	width: 100px;
	font-size: 25px;
	margin-right: 100px;
	float: right;
	background-color: #4EA24E;
	color: orange;
	border-radius: 5px;
	text-shadow: 0 0 10px black;
	box-shadow: 0 0 10px black;
	font-family: Rockwell, 'Courier Bold', serif

}

#form-box button:hover {

	color: gold;

}

#form-box h1{

	text-align: left;
	margin-right: 65px;
	color: #4EA24E;
	font-size: 35px;
	margin-bottom: 0;
	text-shadow: 0 0 1px black;
	margin-left: 30px;
	}

#form-box h2{

	text-align: right;
	margin-right: 85px;
	color: #114611;
	}

#calcContainer {
	clear: both;
	width: 200px;
	height: 500px;
	float: left;
	margin-top: 600px;
	border: 1px solid black;
}

.table {
	margin-top: 20px;

}
.table td, .table tr {
	border: 1px solid black;
	width: 150px;

}
.table h3 {
	margin-top: 40px;

}
/*table {
	margin-top: 200px;
}

td , tr{
	border: 1px solid black;
	width: 150px;
}
*/
span {
	margin-left: 40px;
}


#screen {


}

html {
	margin: 0;
	padding: 0;
	min-width: 960px;
	max-width: 1000px;
	background: url(bubbles.jpg) no-repeat;
	height: 100%;
	background-size: 960px 960px;
	//margin-bottom: 100px;
}



#footer {
	width:100%;
	height:100px !important;
	border-top:4px solid black;
	background-color:orange;
	//position: relative;
	//bottom: 0;
	margin-bottom: 0 auto;
	//position: fixed;
	z-index: 10;
	clear: both;
	margin-top: 500px;
	margin-left: 30px;
}
#footer-inner {
	width:80%;
	margin:0 auto 0 auto;
	height:inherit;
}

body {
	margin-bottom: 100px;
	margin-right: 30px;
	padding: 0;
	width: 100%;
	height: 100%;
}

h1.name{

	/*font-family: Lato, 'Courier Bold', sanserif;*/
	font-family: 'KOMIKAX_';
	src: url(KOMIKAX_.tff);
	font-weight: bold;
	font-variant: small-caps;
	color: "red";
	margin-left: 30px;
	text-shadow: 0 0 1px black;

}

#header {
	margin-left: 30px;
	width:100%;
}

#gradient {
    	height: 65px;
	/* IE 10 */
	background-image: -ms-linear-gradient(top, black 0%, orange 100%);

	/* Firefox */
    	background-image: -moz-linear-gradient(top, black, orange); 

	/* Safari  & Chrome */
    	background-image: -webkit-gradient(linear,left bottom,left top, color-stop(0, orange),color-stop(1, black)); 

	box-shadow: inset 0 0 15px black;
}



#nav1 {

list-style: none;

}

#nav2 {

list-style: none;

}

.nav a {

	text-decoration: none; /*remove underline*/
	text-transform: uppercase;
	color: white;
	font-family: Rockwell, 'Courier Bold', serif;
	font-size: 20px;
	padding-bottom: 15px;
	
}

.nav li {

	display: inline;
	float: left;
	padding: 10px;
	

}

.nav a:visited {
        text-decoration: none;
        color: #fff;
    }
.nav a:hover {
        text-decoration: none;
        color: 	black;
	background-color:transparent;

    }
.nav a:active {
        text-decoration: none;
        color: #19A3FF;
	
    }

.container {

	margin-left: 30px;
	height: 560px;
	background-color: black;
	width: 1000px;
	border-radius: 3px;
	float: left;
}

.text-left {

	float: left;
	padding-left: 30px;
}

.text-right {

	float: right;
	padding-right: 55px;
}

.text-center {

	float: center;
	margin: auto 0;
}

.MainImage {

	background-image: url(http://cdn2.sportngin.com/attachments/photo/2021/8243/football_large.jpg);	

	height: 300px;
	background-repeat: no-repeat;
	width:99.8%;
	-webkit-background-size: cover;
	-moz-background-size: cover;
	-o-background-size: cover;
	background-size: 100%;
	padding-bottom: 30px;
	display: block;
	border: 1px solid;
	margin-left: 30px;
	opacity: 0.9;
     	filter: alpha(opacity=90); /* For IE8 and earlier */
}


h1.title {

	color: white;
	padding-left: 30px;
	padding-top: 10px;
	font-size: 60px;
	font-family: Rockwell, 'Courier Bold', serif;
	font-variant: small-caps;
	font-weight: bold;
	text-shadow: 0 0 3px black;
	margin-bottom: 0;
}



#sub {

	color: white;
	padding-left: 80px;
	font-size: 30px;
	font-family: Rockwell, 'Courier Bold', serif;
	font-variant: small-caps;
	text-shadow: 0 0 8px black;
	

}

/*.highlight {

	/*text-shadow: 0 0 10px #E6FFFF;*/
	text-shadow: 0 0 10px rgba(255,255,255,1) , 0 0 20px rgba(255,255,255,1) , 0 0 30px rgba(255,255,255,1) , 0 0 40px #ff00de , 0 0 70px #ff00de , 0 0 80px #ff00de , 0 0 100px #ff00de ;

	filter: glow(color=#E6FFFF, strength=3);
	color: red;
	

}*/

#sidebar {

	height: 1200px;
	width: 400px;
	float: left;
	background-color: #99CC99;
	margin-top: 50px;
	font-size: 25px;
	margin-right: 0;
}

#main-container {

	width: 1260px;
	height: 230px;
	margin-top: 30px;
	postion: relative;
	margin-left: auto;
	margin-right: auto;
	margin-bottom: 0;
}

#main-container2 {

	width: 1260px;
	height: 230px;
	postion: relative;
	margin-left: auto;
	margin-right: auto;
	margin-top: 0;
		

}


#columns {

	float: left;
	width: 370px;
    	height: 230px;
    	background-color: #ECF2F8;
    	text-align: center;
 	display: inline-block;
	vertical-align: top;
	margin-left: 20px;
	border-radius: 10px;
	box-shadow: 0 0 10px black;
	padding-left: 10px;
	padding-right: 10px;
	border: 1px solid black;


}

#columns-image {

	foat: left;
	width: 390px;
	border: 1px solid black;
    	height: 230px;
 	display: inline-block;
	margin-left: 18px;
	border-radius: 5px;
	border: 1px solid black;

}

#bar-left {

	height: 230px;
	width: 30px;
	background-color: blue;
	float: left;
	margin-right: 20px;
	margin-top: 30px;
	margin-left: 0;


}

#bar-right {

	height: 230px;
	width: 30px;
	background-color: blue;
	float: left;


}

#bullet {

	list-style-Type: none;
	padding: 0 0 4px 23px;
	background: ur(http://www.computerhope.com/arrow.gif) no-repeat left top;

}
<!DOCTYPE html>

<html>


<head>
	<link rel = "stylesheet" href = "stylesheet.css" type = "text/css">
	<link rel = "stylesheet" href = "formstylesheet.css" type = "text/css">
	<meta http-equiv="X-UA-Compatible" content="IE=80" />
</head>


<div id = "screen">




<body>


<h1 class = "name"><font color = "orange" font size = "20px"> Passer Ratings |  </font><font size = "12" font color = "#4EA24E"> Monitor Your Results to Improve!</font></h1>

	<div id = "header">
		<div id = "gradient">
			<div class = "nav">

		<!-- container-fluid gives full width container of whole viewport -->

		<div class = "container-fluid">

		<ul id = "nav1" class= "text-left">
			<li><a href = "main.html"><strong>Home</a></li>
			<li><a href = "#">About Us</a></li>
			<li><a href = "#">Teach</a></li>
			<li><a href = "#">Score Board</strong></a></li>
		</ul>

		<ul id = "nav2" class = "text-right">
			<li><a href = "#"><strong>Contact</strong></a></li>
		</ul>

		</div><!-- end container-fluid-->
			</div><!--end nav-->
		</div>
	</div> <!-- end header -->



	<div id = "Main">

			<div class = "MainImage">

		<h1 class = "title"> Knowing your Strengths and Weaknesses..<br></h1>
		<p id = "sub"><font color= "#4DFFFF"><strong> Makes</strong>
				</font> a great player... </p>

			</div><!-- end MainImage-->
	
		<form id ="form-box" action = 'passrating.php' method = 'post'>
		<h1>Calculate Passer Rating<br><br> 
		<h2>Submit to Review the information </h2>

		
    		<label>First Name </label>
    		<input type="text" name = 'First' placeholder='First'/><br/>
    		<label>Last Name:</label>
    		<input type="text" name = 'Last' placeholder='Last'/><br/>
		<label>Pass Completions</label>
    		<input type="text" name = 'completions' value = 0 class = 'numInput'><br/>
		<label>Pass Attempts:</label>
                <input type="text" name = 'attempts' value = 0><br/>
                <label>Total Passing Yards:</label>
                <input type="text" name = 'yards' value = 0><br/>
                <label>Touchdowns:</label>
                <input type="text" name = 'touchdowns' value = 0><br/>
                <label>Interceptions:</label>
                <input type="text" name = 'interceptions' value = 0><br/>
		<button type="reset" value="Reset">Reset</button>
		<button type="submit" value="Submit">Submit</button>

		</form>

<div class='calcContainer'>

  <table class='table' action = 'passrating.php' method = 'post'>
  <h3>Totals for Calculations</h3>
    <tr> Test Case:<?php echo "\t" .$first. "\t" .$last; ?></tr>
    <tr>
      <td>Pass Completions </td>
      <td width = "20px"><span value = 0><?php echo $completions; ?></td>
    </tr>
    <tr>
      <td>Pass Attempts </td>
      <td><span value = 0><?php echo $attempts; ?></td>
    </tr>
    <tr>
      <td>Total Passing Yards </td>
      <td><span value = 0><?php echo $yards; ?></td>
    </tr>
    <tr>
      <td>Touchdowns </td>
      <td><span value =0><?php echo $touchdowns; ?></td>
    </tr>
    <tr>
      <td>Interceptions: </td>
      <td><span value = 0 ><?php echo $interceptions; ?></td>
           </tr>
    <tr>
      <td>Passing Rating: </td>
      <td><span value = 0 ><?php echo $TotalScore; ?></td>
   </table>

	<p value = " ">The Overall Rating is: <?php echo $score; ?></p>

	<p value = ""><?php echo "The Overall Rating is: " .$score. "</br>"; ?></p> 

</div>

</div>
</body>
</html>

<!DOCTYPE html>

<html>


<head>
    <link rel = "stylesheet" href = "stylesheet.css" type = "text/css">
    <link rel = "stylesheet" href = "formstylesheet.css" type = "text/css">
    <meta http-equiv="X-UA-Compatible" content="IE=80" />
</head>


<div id = "screen">




<body>


<h1 class = "name"><font color = "orange" font size = "20px"> Passer Ratings |  </font><font size = "12" font color = "#4EA24E"> Monitor Your Results to Improve!</font></h1>

    <div id = "header">
        <div id = "gradient">
            <div class = "nav">

        <!-- container-fluid gives full width container of whole viewport -->

        <div class = "container-fluid">

        <ul id = "nav1" class= "text-left">
            <li><a href = "main.html"><strong>Home</a></li>
            <li><a href = "#">About Us</a></li>
            <li><a href = "#">Teach</a></li>
            <li><a href = "#">Score Board</strong></a></li>
        </ul>

        <ul id = "nav2" class = "text-right">
            <li><a href = "#"><strong>Contact</strong></a></li>
        </ul>

        </div><!-- end container-fluid-->
            </div><!--end nav-->
        </div>
    </div> <!-- end header -->



    <div id = "Main">

            <div class = "MainImage">

        <h1 class = "title"> Knowing your Strengths and Weaknesses..<br></h1>
        <p id = "sub"><font color= "#4DFFFF"><strong> Makes</strong>
                </font> a great player... </p>

            </div><!-- end MainImage-->

        <form id ="form-box" action = 'passrating.php' method = 'post'>
        <h1>Calculate Passer Rating<br><br> 
        <h2>Submit to Review the information </h2>


            <label>First Name </label>
            <input type="text" name = 'First' placeholder='First'/><br/>
            <label>Last Name:</label>
            <input type="text" name = 'Last' placeholder='Last'/><br/>
        <label>Pass Completions</label>
            <input type="text" name = 'completions' value = 0 class = 'numInput'><br/>
        <label>Pass Attempts:</label>
                <input type="text" name = 'attempts' value = 0><br/>
                <label>Total Passing Yards:</label>
                <input type="text" name = 'yards' value = 0><br/>
                <label>Touchdowns:</label>
                <input type="text" name = 'touchdowns' value = 0><br/>
                <label>Interceptions:</label>
                <input type="text" name = 'interceptions' value = 0><br/>
        <button type="reset" value="Reset">Reset</button>
        <button type="submit" value="Submit">Submit</button>

        </form>

<div class='calcContainer'>

  <table class='table' action = 'passrating.php' method = 'post'>
  <h3>Totals for Calculations</h3>
    <tr> Test Case:<?php echo "\t" .$first. "\t" .$last; ?></tr>
    <tr>
      <td>Pass Completions </td>
      <td width = "20px"><span value = 0><?php echo $completions; ?></td>
    </tr>
    <tr>
      <td>Pass Attempts </td>
      <td><span value = 0><?php echo $attempts; ?></td>
    </tr>
    <tr>
      <td>Total Passing Yards </td>
      <td><span value = 0><?php echo $yards; ?></td>
    </tr>
    <tr>
      <td>Touchdowns </td>
      <td><span value =0><?php echo $touchdowns; ?></td>
    </tr>
    <tr>
      <td>Interceptions: </td>
      <td><span value = 0 ><?php echo $interceptions; ?></td>
           </tr>
    <tr>
      <td>Passing Rating: </td>
      <td><span value = 0 ><?php echo $TotalScore; ?></td>
   </table>

    <p value = " ">The Overall Rating is: <?php echo $score; ?></p>

    <p value = ""><?php echo "The Overall Rating is: " .$score. "</br>"; ?></p> 

</div>

</div>
</body>
</html>

Solution

  • What I did to fix my issues:

    if(is_numeric($completions) && is_numeric($attempts) && is_numeric($yards) 
    && is_numeric($touchdowns) && is_numeric($interceptions)) {
    
    //if(($_POST['completions'] >0) && ($_POST['attempts'] >0) && ($_POST['yards'] >0)
    //  && ($_POST['touchdowns'] >0) && ($_POST['interceptions'] >0) ){
    
    if(($_POST['First'] != '') && ($_POST['Last'] != '')){  
    
    //  $TotalScore = rating($completions, $attempts, $yards, 
    //              $touchdowns, $interceptions);
    
     if(($_POST['completions'] <0) || ($_POST['attempts'] <0) || ($_POST['yards'] <0)
              || ($_POST['touchdowns'] <0) || ($_POST['interceptions'] <0) ){
    
          $score =  "</br></br><strong>Invalid Input!</strong></br>Please Provide non-Negative Numbers.";
     }
    
    //echo $TotalScore; 
    else {
    
        $TotalScore = rating($completions, $attempts, $yards,
                                $touchdowns, $interceptions);
    
        if($TotalScore < 0) {
    
         $score =  "</br></br><strong>Invalid Results</strong></br>Please review over your scores.          The Passing Rating shouldn't be negative.";
    
        }
    
    
        if($TotalScore > 0 && $TotalScore <85){
            $score = "Poor";
        }
    
        elseif($TotalScore >=85 && $TotalScore <90){
            $score = "Mediocre";
        }
        elseif ($TotalScore >=90 && $TotalScore <95){
            $score = "Good";
        }
    
        elseif ($TotalScore >= 95){
            $score = "Great";
        }
    }
    //}
    }
    }