创建多维数组

iyzzxitl  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(371)

这是我的代码:

<?php
if ($result->num_rows > 0) {
    $total                = 0; // used for displaying the total price at the bottom
    $_SESSION['foodname'] = array();
    while ($row = $result->fetch_assoc()) {
?>
               <tbody>
                    <tr>
                        <td data-th="Product">
                            <div class="row">
                                <div class="col-sm-2 hidden-xs"><br><img src="../images/food/<?php
        echo $row['food_img'];
?>" alt="" class="img-responsive" width="100%"/></div>
                                <div class="col-sm-10">
                                    <h4 style="margin-left:20px;"><br><?php
        echo $row['food_name'];
?></h4>
                                </div>
                            </div>
                        </td>
                        <td data-th="Price"><br>Rs. <?php
        $price = $row['price'];
        echo $price;
?></td>
                           <form action="" method="POST">
                                <td data-th="Quantity"><br>
                                    <input type="number" name="quantity" class="form-control text-center" value="<?php
        $fquantity = $row['food_quantity'];
        echo $fquantity;
?>" min="1" max="<?php
        echo $row['quantity'];
?>">
                                </td>
                                <td data-th="Subtotal" class="text-center"><?php
        $sub_total = ($price * $fquantity);
        echo '<br>$ ' . $sub_total;
        $total = $total + $sub_total;
?>
                               </td>
                                <td class="actions">
                                        <br><input type="submit" class="btn btn-info btn-sm" value="Update Quantity" name="update">
                                    <a class="btn btn-danger btn-sm" href="../CRUD/delete.php?id=<?php
        echo $row['food_id'];
?>&q=<?php
        echo $fquantity;
?>">Remove Item</a>
                                </td>
                            </form>
                    </tr>
                </tbody>
<?php
        $_SESSION['foodname'] = array(
            "name" => $row['food_name'],
            "price" => $row['price']
        );
    }
    print_r($_SESSION['foodname']); // PRINTING ONLY LAST VALUE
}
?>

您可以清楚地看到,下面的代码行是在while循环中编写的,但是我只能看到关于最后一个数据项的详细信息。

$_SESSION['foodname'] = array("name" => $row['food_name'], "price" => $row['price']);

我有没有做错什么事?

osh3o9ms

osh3o9ms1#

更改这部分代码

<?php
        $_SESSION['foodname'] = array(
            "name" => $row['food_name'],
            "price" => $row['price']
        );
    }
    print_r($_SESSION['foodname']); // PRINTING ONLY LAST VALUE
}
?>

用这个

<?php
        $food = array(
            "name" => $row['food_name'],
            "price" => $row['price']
        );
        array_push($_SESSION['foodname'],$food);
    }
    print_r($_SESSION['foodname']); // PRINTING ONLY LAST VALUE
}
?>
iecba09b

iecba09b2#

问题在于你如何设置 $_SESSION 在回路内部。每次执行循环时都要重写。让它们成为一个数组,这样就可以工作了:

//-------------------▾▾
$_SESSION['foodname'][] = array(
    "name" => $row['food_name'],
    "price" => $row['price']
);

请注意 [] 之后 $_SESSION['foodname'] .

相关问题