根据条件保留所有先前的数据

时间:2015-01-19 11:05:07

标签: php

我试图根据计数器显示两个产品名称。我已宣布会话变量' counter'。如果单击该按钮,我想显示第一个产品名称,如果再次单击该按钮,我想显示两个产品名称。

  

戴尔

     

Mac

但是,如果我第二次点击该按钮,则第二个产品名称将替换第一个产品名称;我想要展示这两种产品。

我该如何解决这个问题?

我的代码:

<?php
session_start();
include('connect.php');

// if counter is not set, set to zero
if (!isset($_SESSION['counter'])) {
    $_SESSION['counter'] = 0;
}

// if button is pressed, increment counter
if (isset($_POST['button'])) {
    ++$_SESSION['counter'];
    // Get data according to id 

    $userid2 = $_POST['id2'];
    $query3 = "select * from product where  id='" . $userid2 . "'";
    $result3 = mysql_query($query3) or die(mysql_error());
    $data3 = mysql_fetch_array($result3);

    if ($_SESSION['counter'] == 1) {
        echo $data3['name'] . "<br>";
    }

    if ($_SESSION['counter'] == 2) {
        echo $data3['name'];
    }
}

// reset counter
if (isset($_POST['reset'])) {
    $_SESSION['counter'] = 0;
}

$query2 = "select * from product ";
$result = mysql_query($query2) or die(mysql_error());

$data = mysql_fetch_array($result);

$sr = $data['sr'];
$name = $data['name'];
$price = $data['price'];
$qunt = $data['quantity'];

$total += $data['price'];

while ($data = mysql_fetch_array($result)) {
    ?>
    <form method="POST" action="">
        <table border="1">
            <tr>
                <?PHP
                echo "<tr><td>" . $data['id'] . "</td><td>" . $sr . "</td><td >" . $name . "</a></td><td>" . $price . "</td><td>" . $qunt . "</td> ";
                ?>
            <tr>
        </table>
        <input type="hidden" name="id2" value="<?php echo $data['id'] ?>"/>

        <input type="hidden" name="counter" value="<?php echo $_SESSION['counter']; ?>"/>
        <input type="submit" name="button" value="Counter"/>
        <input type="submit" name="reset" value="Reset"/>

        <br/><?php echo $_SESSION['counter']; ?>
    </form>
<?php }
?>

2 个答案:

答案 0 :(得分:0)

只需更改if块,就像这样

if($_SESSION['counter']==1 || $_SESSION['counter']==2) {
  echo $data3['name']."<br>";
}

这样第一个产品将在两种情况下显示

<强>更新

我会这样做,不要在会话中存储计数器值,将产品ID存储为array,然后您可以使用count()来获取会话中的产品数量< / p>

通过这种方式,您可以在数据库中查询存储的ID,例如

$query3 = "select * from product where id IN (1,2);

并在页面上列出

注意:不要使用mysql_* api,不推荐使用PDO,它的安全性和支持许多数据库;

答案 1 :(得分:0)

当您提出第二次请求时,您将失去当前状态;您无法再访问第一个产品名称。解决此问题的一种方法是添加第二个会话变量以跟踪产品,如下所示:

<?php
session_start();
include('connect.php');

// if counter is not set, set to zero
if (!isset($_SESSION['counter']) || !isset($_SESSION['products'])) {
    $_SESSION['counter'] = 0;
    // Create a new array in your session
    $_SESSION['products'] = array();
}

// if button is pressed, increment counter
if (isset($_POST['button'])) {
    ++$_SESSION['counter'];
    // Get data according to id

    $userid2 = $_POST['id2'];
    $query3 = "select * from product where  id='" . $userid2 . "'";
    $result3 = mysql_query($query3) or die(mysql_error());
    $data3 = mysql_fetch_array($result3);

    // Add to your products session array
    $_SESSION['products'][] = $data3['name'];

    // Reduce to two products
    $_SESSION['products'] = array_slice($_SESSION['products'], -2, 2);

    // Print each product in your products array, max of two
    foreach($_SESSION['products'] as $name) {
        echo $name . "<br />";
    }
}

// reset counter
else if (isset($_POST['reset'])) {
    $_SESSION['counter'] = 0;
    $_SESSION['products'] = array();
}

$query2 = "select * from product ";
$result = mysql_query($query2) or die(mysql_error());

$data = mysql_fetch_array($result);

$sr = $data['sr'];
$name = $data['name'];
$price = $data['price'];
$qunt = $data['quantity'];

$total += $data['price'];

while ($data = mysql_fetch_array($result)) {
    ?>
    <form method="POST" action="">
        <table border="1">
            <tr>
                <?= "<tr><td>" . $data['id'] . "</td><td>" . $sr . "</td><td >" . $name . "</a></td><td>" . $price . "</td><td>" . $qunt . "</td> " ?>
            <tr>
        </table>
        <input type="hidden" name="id2" value="<?= $data['id'] ?>"/>

        <input type="hidden" name="counter" value="<?= $_SESSION['counter'] ?>"/>
        <input type="submit" name="button" value="Counter"/>
        <input type="submit" name="reset" value="Reset"/>

        <br/><?= $_SESSION['counter']; ?>
    </form>
<?php }
?>

要记住的一点:mysql_ *函数的使用是deprecated,坦率地说,在新开发中使用是危险的。您不能将预准备语句与这些函数一起使用,这意味着任何人都可能严重污染您的数据库。阅读更多相关信息here