php - 数组中的表单和对象
问题描述
我正在学习 PHP 中的 OOP,并且在练习中,我不知道如何将表单数据发送到对象并将对象输入到数组中。我浏览了很多 Youtube 教程和论坛,但我找不到或了解太多。
练习首先要求一个类来管理超市的产品,其属性是数字键、描述、价格和库存。它还要求我定义一个带有参数的构造函数作为方法。
<?php
class Product{
private $key;
private $description;
private $price;
private $stock;
public function __construct($key, $description, $price, $stock){
$this->key = $key;
$this->description = $description;
$this->price = $price;
$this->stock = $stock;
}
public function setKey($key){
$this->key = $key;
}
public function getKey(){
return $this->key;
}
public function setDescription($description){
$this->description = $description;
}
public function getDescription(){
return $this->description;
}
public function setPrice($price){
$this->price = $price;
}
public function getPrice(){
return $this->price;
}
public function setStock($stock){
$this->stock = $stock;
}
public function getStock(){
return $this->stock;
}
}
然后它要求我使用该类来声明一个对象数组并使用 POST 方法控制多达 50 种产品的库存。此外,该程序必须有一个包含以下项目的菜单:添加产品、删除产品、列出产品、按密钥编号排序产品和退出。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<form method="post">
<label for="key">Enter product key</label></br>
<input type="number" name="key" id="key" required></br>
<label for="description">Enter product description</label></br>
<input type="text" name="description" id="description" required></br>
<label for="price">Enter product price</label></br>
<input type="text" name="price" id="price" required></br>
<label for="stock">Enter the stock of the product</label></br>
<input type="number" name="stock" id="stock" required></br>
<button type="submit" name="add" id="add">Add product</button>
<button type="submit" name="baja" id="baja">Remove product</button>
<button type="submit" name="list" id="list">List product</button>
<button type="submit" name="Sort" id="Sort">Sort product</button>
<button type="submit" name="exit" id="exit">Exit</button>
</form>
</body>
</html>
这是问题所在:我不知道如何在不删除以前的对象的情况下将对象插入数组中,也不知道如何打印所有输入的对象。
<?php
if (strlen(session_id()) < 1) {
session_start();
}
include_once("product.php");
if(isset($_POST["add"])){
$_SESSION["quantity"] = $_SESSION["quantity"] +1;
$quantity = $_SESSION["quantity"];
if($quantity<=50){
$oproduct = new Product($_POST["key"], $_POST["description"], $_POST["price"],
$_POST["stock"]);
$oproduct->setKey($_POST["key"]);
$oproduct->setDescription($_POST["description"]);
$oproduct->setPrice($_POST["price"]);
$oproduct->setStock($_POST["stock"]);
$_SESSION["prod"]= $oproduct;
print_r($_SESSION["prod"]);
}
}
解决方案
动态地将新产品附加[]
到数组中:
$_SESSION["prod"][] = $oproduct;
key
或者,如果它是唯一的,您可以使用:
$_SESSION["prod"][$_POST["key"]] = $oproduct;
此外,您似乎已经在此处添加了信息:
$oproduct = new Product($_POST["key"], $_POST["description"], $_POST["price"], $_POST["stock"]);
那你为什么要这样做?
$oproduct->setKey($_POST["key"]);
$oproduct->setDescription($_POST["description"]);
$oproduct->setPrice($_POST["price"]);
$oproduct->setStock($_POST["stock"]);
推荐阅读
- python - Pandas 基于 2 列删除重复项,有时会颠倒
- php - 有没有办法使用 PHP 表单循环浏览数据库中的多个记录?
- java - 我是否正确使用了 Scanner 和 File 类?
- reactjs - 如何将数据传递给功能组件?
- javascript - 使用点击生物创建团队页面
- html - 有没有办法让子 div 的背景扩展到父 div 之外?
- mongodb - 在MongoDB中按某个子数组元素对父记录进行排序
- c++ - 我需要转换一些代码,以便它与输入和输出文件文本一起使用
- haskell - let vs do in monadic values
- scala - 如何连接两个数据框并在火花中添加字段