<?php
$file = 'data.txt';
$data = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
echo $data[$_GET["times"]];
<?php
$file = 'data.txt';
if (isset($_POST['send'])) {
file_put_contents( $file, $data );
}
$data = file_get_contents( $file);
?>
<html>
<head>
</head>
<body>
<form action='/1.php' method='post'>
<textarea name="data"><?=$data?></textarea>
<input type="submit" name="send" >
</form>
</body>
</html>
1
22
2
class Controller
{
private $repo;
pulic function __construct(MissionRepository $repo)
{
$this->repo = $repo;
}
pulic function excuteMission($id)
{
$mission = $this->repo->find($id);
$mission->next();
$mission->save();
}
}
class Mission
{
private $progress;
public function next(): void
{
$progress = $this->progress + 10;
if($progress > 100) {
throw new DomainExeption('Миссия не может быть выполнена.')
}
if($progress = 100) {
throw new DomainExeption('Миссия уже выполнена.')
}
}
}
$user = $db->prepared_query("SELECT * FROM users WHERE id = ?", [$id])->fetch();
$user = $db->select('*')->from('users')->where('id', $id);
<?php
$json = <<<JSON
{
"issuance": {
"1": {
"data": "01.20",
"quantity": 2
},
"2": {
"data": "01.20",
"quantity": 4
},
"3": {
"data": "04.20",
"quantity": 2
},
"4": {
"data": "04.20",
"quantity": 2
}
}
}
JSON
;
$arr = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
$res = new \stdClass();
$res->issuance = array_values(
array_reduce(
$arr['issuance'],
function ($res, $el) {
$res[$el['data']]['data'] = $el['data'];
$res[$el['data']]['quantity'] = isset($res[$el['data']]['quantity'])
? $res[$el['data']]['quantity'] + $el['quantity']
: $el['quantity'];
return $res;
},
[]
)
);
var_dump(json_encode($res, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT, 512));
pass
$sth->execute(array('login' => $login, pass' => $pass));
Единственный вариант, который мне известен, это через внедрение зависимостей.
Но я не представляю в какой части приложения это делают. Нужно создать подключение к БД в front-controller и поместить в DI-контейнер, и в дальнейшем использовать ? Или как ?
$container->set('db', function () use ($container) {
return new DbConnection($container->get('config')['db']);
});
$container->set('MyService', function () use ($container) {
return new MyService($container->get('db'));
});