来自文件的行作为变量

时间:2018-04-03 18:25:26

标签: php loops output

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Style-Type" content="text/css" /> 
        <title>squared.php</title>
        <link href="/library/skin/tool_base.css" type="text/css" rel="stylesheet" media="all" />
        <link href="/library/skin/durham/tool.css" type="text/css" rel="stylesheet" media="all" />
        <script type="text/javascript" src="/library/js/headscripts.js"></script>
        <style>body { padding: 5px !important; }</style>
    </head>
    <body>
        <!DOCTYPE html>

    <html>
        <head>
            <title>Squared</title>
            <link rel ="stylesheet" type="text/css" href="sample.css">
        <h1> Squared  </h1>
    </head>
    <?php
    //Save the three values from the file
    $beginningNum = $_POST['beginningNum'];
    $endingNum = $_POST['endingNum'];
    $incrementer = $_POST['incrementer'];

    //Open the file and read the numbers
    $dataFilePointer = fopen("output.txt", "r");

    $beginningNum = fgets($dataFilePointer);
    $endingNum = fgets($dataFilePointer);
    $incrementer = fgets($dataFilePointer);


    fclose($dataFilePointer);

    // Loop through beginning with beginningNum until you get to endingNum and increment by incrementer
    // The first statement says "Start x at the entered beginningNum
    // The second statement says "Loop until $x gets to the endingNum value
    // The third statement says "Increment $x by the incrementer after every loop (also known as counter)
    // Check to make sure each value is a numeric then do the for loop, if not - print an error message
    if (is_numeric($beginningNum) and is_numeric($endingNum) and is_numeric($incrementer)) {
        for ($x = $beginningNum; $x <= $endingNum; $x += $incrementer) {

            //Square $x for every increment of it using pow
            $squaredNum = pow($x, 2);

            // Print $squaredNum to the user
            print("<p> The Final Results are $squaredNum.</p>");
        }
    } else {
        print("<p>Error.</p>");
        print("<a href=\"squared.html\">Return to Form.</a>");
    }
    ?>

这是我到目前为止所拥有的。我想写一个squared.php文件,该文件从squared.html接收文件名。它应该从该文件中读取三个值:

  1. 文件中的第一行将包含起始值

  2. 文件中的第二行将包含结束值

  3. 文件中的第三行将包含更新计数器的值(通常会增加,但并非总是如此)

  4. **等待星期三的指示:

         Read these 3 values, and confirm that they are numeric values using the `php` function `is_numeric()`.
    
         If not, display an error message and offer the user a link to return to the `HTML`.
    

    如果是有效输入,则代码应包含以下逻辑:

    使用for循环,它将使用开始,结束和更新值来显示那些平方值。

    例如,如果用户输入:(我将使用其他值测试您的代码)

    起始值:5

    结束价值:20

    增量:5

    您的结果应该是:

    5平方为25

    10平方是100

    15平方是225

    20平方是400

0 个答案:

没有答案