首页 > 解决方案 > 数组中的表单和对象

问题描述

我正在学习 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"]);
    }
  }

标签: phpoop

解决方案


动态地将新产品附加[]到数组中:

$_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"]);

推荐阅读