3
<html>
//index.php

<form action="" method="post"> 
  <label for="item">Item: </label>
    <select name="item">
        <option value="fruit tea">Fruit Tea</option>
         <option value="strawberries">Strawberries</option>
         <option value="coffee">Coffee</option>
    </select><br />

    <label for="qty">Qty: </label>
        <input type="text" name="qty" />
        <input type="submit" name="submit" value="submit" />
</form>


<?php

    @$qty = $_POST['qty'];
    @$item = $_POST['item'];

    $co = new Checkout( $pricing_rules );
    print $co->scan( $item ). ' has been scanned '.$co->increment(). ' times';

?>
</html>




<?php

/**
* Description of Checkout
*
* @author cookie
*/
class Checkout {

private $pricing_rules = array();
private $pcode;
public $item;


public function __construct( array $pricing_rules ) {
    $this->pricing_rules = $pricing_rules;
}

public function scan( $item ) {

    $this->increment();
    return $item;
}

private function prod_code( $pcode ) {

    $this->pcode = $pcode;
}


public function increment() {
    static $count = 0;
    $count++;
    return $count;
}

public function total() {

}


}

?>

它应该做什么...

如果我点击提交按钮四次,说从下拉列表中选择草莓。该脚本应打印到屏幕上:

草莓已被扫描 1 次

草莓已被扫描 2 次

草莓已被扫描 3 次

草莓已被扫描 4 次

ETC....

随着$count增量的跟踪。

它实际上做了什么:

它打印:

不管我按提交多少次,草莓都被扫描了 2 次。并停在那里。我在这里遗漏了一些东西。

我看到它在做什么 - 在页面加载时,通过这一行对 increment() 进行了两次调用:

print $co->scan( $item ). ' has been scanned '.$co->increment(). ' times';

我试过了:

  if($_SERVER['REQUEST_METHOD'] == 'POST') $co->increment();

此外,这会将 $count 增加一,但停在那里。我在这方面有点挣扎..

帮助

4

3 回答 3

2

static变量仅对当前脚本执行是静态的。每次重新加载页面(提交时)时,您的变量都会丢失。为此,您需要将其存储在$_SESSION. 事实上,有一个与您在$_SESSION基本使用手册中尝试的非常相似的示例。

// Call session_start() at the beginning of your script...
session_start();

// And modify your method to store count in the $_SESSION.
public function increment() {
    // initialize it if not already initialized
    $_SESSION['count'] = !isset($_SESSION['count']) ? 0 : $_SESSION['count'];
    $_SESSION['count']++;
    return $_SESSION['count'];
}
于 2012-09-02T15:49:22.890 回答
1

Every time you submit a form, it's making a new HTTP request to the server, and the script runs afresh. Variables do not retain their values from one run to the next.

If you want to save variables across page reloads, you need to use session variables as Michael Berkowski suggests. However, that's just within a browser session. If you need longer retention, you can use cookies, although the user can delete these. Finally, if you want real permanent memory, you can use a database.

The database is also appropriate if you need to combine data from multiple users. E.g. if user A scans an item and then user B scans the item, and you want to display the combined total.

于 2012-09-02T15:52:45.583 回答
1

除了 Michael Berkowski 的回答之外,我还想指出其他一些事情。$counter如果它应该是一个对象字段,为什么要在函数中声明它。既然它应该是一个对象场,那为什么static呢?如果它是静态的,它将计算Checkout给定点的所有实例。您也可以在内部
调用一次,然后再次调用 increment()scan()

print $co->scan( $item ). ' has been scanned '.$co->increment(). ' times';
//         ^ here $counter == 1                       ^ and here $counter == 2

添加
这样的东西对我来说更有意义

class Checkout{

    private $counter;


    public __construct(){
        $this->counter = 0;
        // ......
    }
    // ..... 

    public function scan(){
        // ....
        $this->counter++;
    }

    public function getCounter(){
        $return $this->counter;
    }

}
于 2012-09-02T16:06:30.213 回答