PHP 默认值和打印问题

PHP Default value and Print Issues

我目前正在尝试使我的 php 在与表单相同的页面上正确显示值并计算总体及格评分。我的计算遵循这样的格式:

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

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

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

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

通过率 = ((C+Y+T+I)/6) *100

保存表单值的 table 应该有一个默认值 0 并且在它下面应该根据及格者评分打印差、好和极好的总体评分。

如您所见,我遇到了一些问题。 我的问题是:

及格率不默认为 0。

我的总体评价不打印是差、好还是好。它只打印 Poor

我希望有人能解释一下。我已经排查了几个小时了。不幸的是没有运气。 ps(总评分打印两次看我想用哪个版本)

此外,该代码段不会 运行 php,除非您的伴奏上有 php。需要一个服务器才能看到它,即使是我猜的片段

更新:我用 20 而不是 4 来划分 Y 值。这修正了我的计算,但没有修正整体评级。

第二次更新:我添加了以下行:

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

这样我就可以保留 table 的默认设置。

我的最后一期现在是显示正确的差、好或好标签的总体评级

<?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>

我为解决问题所做的工作:

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";
    }
}
//}
}
}

在这种情况下,您可以使用我的小型库 ValueResolver,例如:

$value = ValueResolver::resolve('', 'default value'); // returns 'default value' because first argument is empty

并且不要忘记使用命名空间 use LapaLabs\ValueResolver\Resolver\ValueResolver;

还有类型转换的能力,例如如果你的变量值应该是integer,那么使用这个:

$id = ValueResolver::toInteger('6 apples', 1); // returns 6
$id = ValueResolver::toInteger('There are no apples', 1); // returns 1 (used default value)

查看 docs 以获取更多示例