Cześć,
Mam taki problem w projekcie, pojawia się błąd niezdefiniowanej zmiennej - slowa w pliku losuj.php
Dlaczego jak odpalę podstronę losuj.php ręcznie poprzez adres ( https://localhost/pai/?page=losuj) to wszystko działa natomiast jeśli dołączę tą podstronę za pomocą funkcji include do index to wywala błąd niezdefiniowanej zmiennej :(
Gorąca prośba o pomoc.
index.php
<!DOCTYPE html>
<html>
<?php include(dirname(__DIR__).'/head.html'); ?>
<body>
<?php include(dirname(__DIR__).'/navbar.html'); ?>
<div class="container bg">
<br><br><br>
<div class="row">
<div class="col-md-12 offset-md-3" style="margin: auto; background: white; padding: 20px; box-shadow: 10px 10px 5px #888">
<div class="panel-heading">
<h2>Nauka słówek</h2>
<p style="font-style: italic;">Słowo: <?php include("views/SlowoController/losuj.php") ?></p>
</div>
<hr>
<form action="?page=camera" method="POST">
<button type="submit" style="border-radius: 0px;" class="btn btn-lg btn-block btn-outline-success">Zrób zdjęcie</button>
</form>
</div>
</div>
</div>
</body>
</html>
losuj.php
<!DOCTYPE html>
<html>
<?php include(dirname(__DIR__).'/head.html'); ?>
<body>
<div class="container">
<div class="row">
<h4 class="mt-4">Slowo:</h4>
<table class="table table-hover">
<thead>
<tr>
<th> <?php foreach ($slowa as $slowo) {
echo "<tr><td>{$slowo['slowo']}</td></tr>";
}
?>
</th>
</tr>
</thead>
</table>
</div>
</div>
</body>
</html>
Slowo.php
<?php
require_once __DIR__.'/../Database.php';
class Slowo{
private $s;
public function __construct($s){
$this->s = $s;
}
public function getSlowo(){
return $this->s;
}
public function setSlowo($s): void{
$this->s = $s;
}
}
SlowpMapper.php
<?php
require_once 'Slowo.php';
require_once __DIR__.'/../Database.php';
class SlowoMapper{
private $database;
public function __construct(){
$this->database = new Database();
}
public function getSlowa(){
try {
$stmt = $this->database->connect()->prepare('SELECT slowo FROM slowa WHERE id_slowo = 2');
$stmt->execute();
$slowo = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $slowo;
}
catch(PDOException $e) {
die();
}
}
}
SlowoController.php
```<?php
require_once "AppController.php";
require_once __DIR__.'/../model/Slowo.php';
require_once __DIR__.'/../model/SlowoMapper.php';
class SlowoController extends AppController{
public function __construct(){
parent::__construct();
}
public function SlowoLst(){
$mapper = new SlowoMapper();
$slowo = $mapper ->getSlowa();
$this->render('losuj', ['slowa' => $slowo]);
}
}