The extra bracket after the setCategoryGood method, without the closing bracket before displayAllCategory, like this:
include_once 'good.php'; class category { var $cat_id; var $cat_name; function setCategoryGood($good_name) { if ($good_value==$this->cat_name) { $category=array($good_name=>$price); } } function displayCategory($cat_name){ $this->cat_name=$cat_name; foreach ($category as $good_name=>$price) { echo $good_name." - ".$price; } } function displayAllCategory(){ $all_category = array($cat_name => $category); foreach ($all_category as $cat_name => $category) { foreach ($category as $good_name => $price) { echo $cat_name."</br><li>$good_name - $price</li>"; } } } }
Elena, your code is full of errors. Where are you from in the setCategoryGood method in the line
$category=array($good_name=>$price);
take the price? Maybe it needs to be passed there through a parameter?
You had problems in that you enclose variables in double quotes, so it is not necessary to do so. And also in if instead of two equal, which are used for comparison, they used one =, which serves for assignment and, therefore, your condition will always be true.
Also string
</br><li>$good_name - $price</li>
You had single quotes, which means that the variables inside will not be processed by the interpreter.
You also created the class good, but you didn't use it anywhere.
The right decision for your problem
<?php class Good { protected $name; protected $price; public function __construct($name, $price) { $this->name = $name; $this->price = $price; } public function getName() { return $this->name; } public function getPrice() { return $this->price; } } class Category { protected $id; protected $name; protected $goods; public function __construct($id, $name) { $this->name = $name; $this->id = $name; $this->goods = []; } public function add(Good $good) { $this->goods[] = $good; } public function getName() { return $this->name; } public function getId() { return $this->id; } public function show() { foreach ($this->goods as $good) { echo $good->getName()."-".$good->getPrice()."<br>"; } } } Class Categories { protected $categories; public function __construct() { $this->categories = []; } public function add(Category $category) { $this->categories[] = $category; } public function show() { foreach ($this->categories as $category) { echo "Название категории:".$category->getName()."<br>"; echo "Список товаров: <br>"; $category->show(); echo "<br><br>"; } } } $category = new Category("flowers", 1); $category->add(new Good("flower", 15)); $category->add(new Good("flower", 25)); $category->add(new Good("flower", 32)); $categories = new Categories(); $categories->add($category); $categories->show();
The output will be:
Название категории:1 Список товаров: flower-15 flower-25 flower-32
I wrote an example based on your task so that you can easily understand the OOP.
We have 3 classes:
- Good - class for goods
- Category - class for category
- Categories - class for categories
We have a class Good, which in itself has price and name properties, it’s just like in real life: a product has properties. Next, we have a category class that has the properties id and name. These properties are hidden to change from the outside, and to get them, you must use the functions getName, getId, etc., which are defined in the class.
That is, you can create an instance of the class and display its name:
$good = new Good("flower", 15) echo $good->getName(); //выведет flower
We also have an array inside Category that will store all the products in the category, and we will add products to it using the add method, and we will only add instances of the Good class. Naturally, to remove all the products from the category, you just need to iterate through the array and refer to the Good class getName and getPrice methods that we have added; the show () method deals with this. Ie, to display all products of a category, you need to add them there and call the show () method.
$good = new Good("flower", 15); //создаем товар flower с ценой 15 $category = new Category("flowers", 1); //создаем категорию с названием flowers и id = 1 $category->add($good); //добавляем товар в категорию $category->show(); //выводим все товары
Next, we have the Categories class, which contains instances of the Category class, about a simple array of categories and a method for displaying show (), the method essentially appeals simply to the methods of the category getName and show () to display information on the category.
$good = new Good("flower", 15); //создаем товар flower с ценой 15 $category = new Category("flowers", 1); //создаем категорию с названием flowers и id = 1 $category->add($good); //добавляем товар в категорию $categories = new Categories(); //Создаем обьект категорий $categories->add($category); //Добавляем категорию $categories->show(); //Делаем вывод всех товаров и категорий
I hope you now have a little understanding of the PLO.