axios
This commit is contained in:
parent
7ec20a6d35
commit
bf98f4354c
162
AXIOS/page.php
162
AXIOS/page.php
@ -1,8 +1,5 @@
|
||||
<?php
|
||||
|
||||
define('LIMIT', 5);
|
||||
define('OFFSET', 0);
|
||||
|
||||
$arrMusic = [
|
||||
'Открываем',
|
||||
'набор',
|
||||
@ -25,31 +22,132 @@ $arrMusic = [
|
||||
'6 месяцев',
|
||||
];
|
||||
|
||||
$len = sizeof($arrMusic);
|
||||
class Page
|
||||
{
|
||||
public string $html;
|
||||
public string $feed;
|
||||
public string $paginator;
|
||||
public string $button;
|
||||
// ... другие свойства
|
||||
|
||||
// $limit = $_GET['limit'] or LIMIT;
|
||||
$limit = isset($_GET['limit']) ? $_GET['limit'] : LIMIT;
|
||||
public function __construct() {}
|
||||
|
||||
// $offset = $_GET['offset'] or OFFSET;
|
||||
$offset = isset($_GET['offset']) ? $_GET['offset'] : OFFSET;
|
||||
public function __toString(): string
|
||||
{
|
||||
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);
|
||||
// print($len);
|
||||
public function __construct()
|
||||
{
|
||||
$this->car = new Car();
|
||||
}
|
||||
|
||||
$str = '<ul id="feed">';
|
||||
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) {
|
||||
$str .="<li>$value</li>";
|
||||
$this->feed .= "<li>$value</li>";
|
||||
}
|
||||
$this->feed .= '';
|
||||
return $this;
|
||||
}
|
||||
$str .= '';
|
||||
|
||||
$countPage = intdiv($len , $limit);
|
||||
// var_dump($countPage);
|
||||
|
||||
$paginator = '';
|
||||
public function setPaginator()
|
||||
{
|
||||
$len = sizeof($this->data);
|
||||
$countPage = intdiv($len, $this->limit);
|
||||
$this->paginator = '';
|
||||
for ($i = 0; $i < $countPage; $i++) {
|
||||
$paginator .= '<a href= "./portial.php?offset='.LIMIT*$i.'&limit='.LIMIT.'">'.$i.'</a> ';
|
||||
$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">
|
||||
@ -59,9 +157,9 @@ echo <<<abyr
|
||||
<title>Pangination</title>
|
||||
</head>
|
||||
<body>
|
||||
$str
|
||||
$this->feed
|
||||
<div id="paginator">
|
||||
$paginator
|
||||
$this->paginator
|
||||
</div>
|
||||
<button type="button" click="getPage">Пам-пам</button>
|
||||
|
||||
@ -86,7 +184,10 @@ parent.addEventListener('click' , getPage )
|
||||
async function getPage(event) {
|
||||
event.stopPropagation()
|
||||
event.preventDefault()
|
||||
let url = event.target.getAttribute('href')
|
||||
// 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
|
||||
@ -96,6 +197,25 @@ async function getPage(event) {
|
||||
|
||||
</script>
|
||||
abyr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// define('LIMIT', 5);
|
||||
// define('OFFSET', 0);
|
||||
|
||||
|
||||
|
||||
// $len = sizeof($arrMusic);
|
||||
|
||||
// $portial = array_slice($arrMusic, $offset, $limit);
|
||||
// print($len);
|
||||
|
||||
|
||||
|
||||
|
||||
// var_dump($countPage);
|
||||
$p = new Paginator($arrMusic);
|
||||
$p->renderFeed();
|
||||
$p->renderPaginator();
|
||||
$p->rederPage();
|
||||
|
Loading…
Reference in New Issue
Block a user