axios
This commit is contained in:
parent
7ec20a6d35
commit
bf98f4354c
260
AXIOS/page.php
260
AXIOS/page.php
@ -1,8 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
define('LIMIT', 5);
|
|
||||||
define('OFFSET', 0);
|
|
||||||
|
|
||||||
$arrMusic = [
|
$arrMusic = [
|
||||||
'Открываем',
|
'Открываем',
|
||||||
'набор',
|
'набор',
|
||||||
@ -25,77 +22,200 @@ $arrMusic = [
|
|||||||
'6 месяцев',
|
'6 месяцев',
|
||||||
];
|
];
|
||||||
|
|
||||||
$len = sizeof($arrMusic);
|
class Page
|
||||||
|
{
|
||||||
|
public string $html;
|
||||||
|
public string $feed;
|
||||||
|
public string $paginator;
|
||||||
|
public string $button;
|
||||||
|
// ... другие свойства
|
||||||
|
|
||||||
// $limit = $_GET['limit'] or LIMIT;
|
public function __construct() {}
|
||||||
$limit = isset($_GET['limit']) ? $_GET['limit'] : LIMIT;
|
|
||||||
|
|
||||||
// $offset = $_GET['offset'] or OFFSET;
|
public function __toString(): string
|
||||||
$offset = isset($_GET['offset']) ? $_GET['offset'] : OFFSET;
|
{
|
||||||
|
return "Модель: {$this->feed}, Цвет: {$this->paginator}, Двигатель: {$this->button}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
interface PageBuilder
|
||||||
|
{
|
||||||
|
public function setFeed(): PageBuilder;
|
||||||
|
public function setPaginator(): PageBuilder;
|
||||||
|
// public function setBuild(string $engine): PageBuilder;
|
||||||
|
// ... другие методы для установки свойств
|
||||||
|
public function build(): Page;
|
||||||
|
}
|
||||||
|
// ConcreteBuilder
|
||||||
|
class MusicPageBuilder implements PageBuilder
|
||||||
|
{
|
||||||
|
private Car $car;
|
||||||
|
|
||||||
$portial = array_slice($arrMusic, $offset, $limit);
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->car = new Car();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setModel(string $model): CarBuilder
|
||||||
|
{
|
||||||
|
$this->car->model = $model;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setColor(string $color): CarBuilder
|
||||||
|
{
|
||||||
|
$this->car->color = $color;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setEngine(string $engine): CarBuilder
|
||||||
|
{
|
||||||
|
$this->car->engine = $engine;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... другие методы для установки свойств
|
||||||
|
|
||||||
|
public function build(): Car
|
||||||
|
{
|
||||||
|
return $this->car;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Director (необязательный)
|
||||||
|
class CarDirector
|
||||||
|
{
|
||||||
|
public function constructCar(CarBuilder $builder): Car
|
||||||
|
{
|
||||||
|
return $builder->setModel('BMW X5')->setColor('Черный')->setEngine('V8')->build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Клиентский код
|
||||||
|
$builder = new ConcreteCarBuilder();
|
||||||
|
$car = $builder->setModel('Audi A6')->setColor('Синий')->setEngine('V6')->build();
|
||||||
|
echo $car . PHP_EOL;
|
||||||
|
|
||||||
|
$director = new CarDirector();
|
||||||
|
$car2 = $director->constructCar($builder);
|
||||||
|
echo $car2;
|
||||||
|
|
||||||
|
|
||||||
|
exit();
|
||||||
|
|
||||||
|
define('LIMIT', 5);
|
||||||
|
define('OFFSET', 0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// class Paginator
|
||||||
|
// {
|
||||||
|
const LIMIT = 5;
|
||||||
|
const OFFSET = 0;
|
||||||
|
private $data = [];
|
||||||
|
private $limit = null;
|
||||||
|
private $offset = null;
|
||||||
|
private $feed = "";
|
||||||
|
private $paginator = "";
|
||||||
|
public function __construct($data)
|
||||||
|
{
|
||||||
|
$this->data = $data;
|
||||||
|
$this->limit = isset($_GET['limit']) ? $_GET['limit'] : self::LIMIT;
|
||||||
|
$this->offset = isset($_GET['offset']) ? $_GET['offset'] : self::OFFSET;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFeed()
|
||||||
|
{
|
||||||
|
$portial = array_slice($this->data, $this->offset, $this->limit);
|
||||||
|
$this->feed = '<ul id="feed">';
|
||||||
|
foreach ($portial as $value) {
|
||||||
|
$this->feed .= "<li>$value</li>";
|
||||||
|
}
|
||||||
|
$this->feed .= '';
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPaginator()
|
||||||
|
{
|
||||||
|
$len = sizeof($this->data);
|
||||||
|
$countPage = intdiv($len, $this->limit);
|
||||||
|
$this->paginator = '';
|
||||||
|
for ($i = 0; $i < $countPage; $i++) {
|
||||||
|
$this->paginator .= '<a href= "./portial.php?offset=' . self::LIMIT * $i . '&limit=' . self::LIMIT . '">' . $i . '</a> ';
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function build()
|
||||||
|
{
|
||||||
|
echo <<<abyr
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Pangination</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
$this->feed
|
||||||
|
<div id="paginator">
|
||||||
|
$this->paginator
|
||||||
|
</div>
|
||||||
|
<button type="button" click="getPage">Пам-пам</button>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
<script>
|
||||||
|
console.log('start script')
|
||||||
|
let parent = document.getElementById('paginator')
|
||||||
|
parent.addEventListener('click', getPage)
|
||||||
|
async function getFetch(url) {
|
||||||
|
const response = await fetch(url, { method:'get'} )
|
||||||
|
// return await response.json();
|
||||||
|
return await response.text();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// parent = document.getElementById('paginator')
|
||||||
|
// console.log(parent)
|
||||||
|
parent.addEventListener('click' , getPage )
|
||||||
|
|
||||||
|
async function getPage(event) {
|
||||||
|
event.stopPropagation()
|
||||||
|
event.preventDefault()
|
||||||
|
// let num_page = event.target.getAttribute('data-id')
|
||||||
|
let limit = 5
|
||||||
|
let url = "page.php?offset" + num_page + "&limit=" + limit
|
||||||
|
console.log(url)
|
||||||
|
let data = await getFetch(url)
|
||||||
|
feed = document.getElementById('feed')
|
||||||
|
feed.innerHTML = feed.innerHTML + data
|
||||||
|
console.log(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
</script>
|
||||||
|
abyr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// define('LIMIT', 5);
|
||||||
|
// define('OFFSET', 0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// $len = sizeof($arrMusic);
|
||||||
|
|
||||||
|
// $portial = array_slice($arrMusic, $offset, $limit);
|
||||||
// print($len);
|
// print($len);
|
||||||
|
|
||||||
$str = '<ul id="feed">';
|
|
||||||
foreach ($portial as $value) {
|
|
||||||
$str .="<li>$value</li>";
|
|
||||||
}
|
|
||||||
$str .= '';
|
|
||||||
|
|
||||||
$countPage = intdiv($len , $limit);
|
|
||||||
|
|
||||||
// var_dump($countPage);
|
// var_dump($countPage);
|
||||||
|
$p = new Paginator($arrMusic);
|
||||||
$paginator = '';
|
$p->renderFeed();
|
||||||
for ($i = 0; $i < $countPage; $i++) {
|
$p->renderPaginator();
|
||||||
$paginator .= '<a href= "./portial.php?offset='.LIMIT*$i.'&limit='.LIMIT.'">'.$i.'</a> ';
|
$p->rederPage();
|
||||||
}
|
|
||||||
|
|
||||||
echo <<<abyr
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Pangination</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
$str
|
|
||||||
<div id="paginator">
|
|
||||||
$paginator
|
|
||||||
</div>
|
|
||||||
<button type="button" click="getPage">Пам-пам</button>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
||||||
<script>
|
|
||||||
console.log('start script')
|
|
||||||
let parent = document.getElementById('paginator')
|
|
||||||
parent.addEventListener('click', getPage)
|
|
||||||
async function getFetch(url) {
|
|
||||||
const response = await fetch(url, { method:'get'} )
|
|
||||||
// return await response.json();
|
|
||||||
return await response.text();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// parent = document.getElementById('paginator')
|
|
||||||
// console.log(parent)
|
|
||||||
parent.addEventListener('click' , getPage )
|
|
||||||
|
|
||||||
async function getPage(event) {
|
|
||||||
event.stopPropagation()
|
|
||||||
event.preventDefault()
|
|
||||||
let url = event.target.getAttribute('href')
|
|
||||||
let data = await getFetch(url)
|
|
||||||
feed = document.getElementById('feed')
|
|
||||||
feed.innerHTML = feed.innerHTML + data
|
|
||||||
console.log(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
</script>
|
|
||||||
abyr;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user