使用foreach循环提交后保持复选框?

My Code is:

<form method="POST" action="" style="margin-top: 30px;">
<div class="form-group" style="display: inline-block;">
    <label for="Name" class="label-control" >Device Price</label>
    <input type="text" name="price" class="form-control">
</div>

<div class="row" style="margin-bottom: 20px;"> 
    <div class="col-md-3">      
        <?php
        $r = 0;
        $sql = "SELECT * FROM Subscribes ";
        $result = $db->query($sql);
        while ($row = $result->fetch_assoc()) {
            if ($r == 5) {
                echo "</div><div class='col-md-3'>";
                $r = 0;
            }
            $r++;
            $p = $row['price'];

            echo "<label style='font-size: 16px; margin: 20px;'>";
            echo "<input name='checkbox[]' type='checkbox' ";
            echo "value=" . $p . " >" . $row['sub_name'];

            if ($p == 0) {
                echo "<label style='color:#f00'> Free </label>";
            }
            echo "</label>";
        }
        ?>

    </div>            
</div> 

<div class="row">       
    <div class="col-md-12">
        <div class="form-group" style="margin-left: 35%;">
            <button  class="btn btn-info" name="sum" style="font-size: 24px;">TOTAL</button>
            <label style="margin-left: 6%; font-size: 24px;">
                <?php
                if (isset($_POST['sum'])) {
                    $price = $_POST['price'];
                    $price = $price + $price * .52;
                    $total = $price;
                    if (!empty($_POST['checkbox'])) {

                        foreach ($_POST['checkbox'] as $value) {
                            $total = $total + $value;
                        }
                    }
                    echo $total;
                }
                ?></label>
        </div>
    </div>
</div>

You can put a checked property to a checkbox:<input type="checkbox" checked>"

In your case, you have to check the POST data in the first block aswell and add the checked property to checkboxes that have been submitted.

echo "<input name='checkbox[]' type='checkbox' ";
echo "value=" . $p;

//if value is in the array of submitted checkboxes, add the checked property
if(array_search($p ,$_POST["checkbox"]) >= 0) echo "checked"; 

echo ">" . $row['sub_name'];

You can almost just check posted value to see if checked[$r] is on, but in the case where the user leaves some of the boxes unchecked, those don't get posted.

So, you need a way to identify each checkbox. You could do this by naming them like this when you echo them:

$id = $row['id']; // or whatever your id is
echo "<input name='checkbox[$id][]' type='checkbox' value=$p >"

Now, when the user submits the form, you know exactly which checkboxes should be checked. So you can simply add the checked attributed when you render them:

echo "<input name='checkbox[$id][]' type='checkbox' value=$p $checked>"

So altogether:

<?php
    $r = 0;
    $sql = "SELECT * FROM Subscribes ";
    $result = $db->query($sql);

    while ($row = $result->fetch_assoc()) {
        if ($r == 5) {
            echo "</div><div class='col-md-3'>";
            $r = 0;
        }

        $r++;
        $p = $row['price'];
        $id = $row['id'];
        $checked = null;

        if(array_key_exists('checkbox', $_POST) && array_key_exists($id, $_POST['checkbox']){ 
            $checked = "checked";
        }

        echo "<label style='font-size: 16px; margin: 20px;'>";
        echo "<input name='checkbox[$id][]' type='checkbox' value=$p $checked>" . $row['sub_name'];

        if ($p == 0) {
            echo "<label style='color:#f00'> Free </label>";
        }
        echo "</label>";
    }
?>