mm
This commit is contained in:
parent
bf98f4354c
commit
58ba8436b5
206
AXIOS/page.php
206
AXIOS/page.php
@ -22,200 +22,130 @@ $arrMusic = [
|
|||||||
'6 месяцев',
|
'6 месяцев',
|
||||||
];
|
];
|
||||||
|
|
||||||
class Page
|
class Page {
|
||||||
{
|
public string $html = "";
|
||||||
public string $html;
|
public string $feed = "";
|
||||||
public string $feed;
|
public string $paginator = "";
|
||||||
public string $paginator;
|
|
||||||
public string $button;
|
|
||||||
// ... другие свойства
|
// ... другие свойства
|
||||||
|
|
||||||
public function __construct() {}
|
public function __construct() {} // Пустой конструктор
|
||||||
|
|
||||||
public function __toString(): string
|
public function __toString(): string {
|
||||||
{
|
return $this->html;
|
||||||
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;
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Клиентский код
|
interface PageBuilder {
|
||||||
$builder = new ConcreteCarBuilder();
|
public function setFeed();
|
||||||
$car = $builder->setModel('Audi A6')->setColor('Синий')->setEngine('V6')->build();
|
public function setPaginator();
|
||||||
echo $car . PHP_EOL;
|
// ... другие методы для установки свойств
|
||||||
|
public function build();
|
||||||
|
}
|
||||||
|
|
||||||
$director = new CarDirector();
|
//implements PageBuilder {
|
||||||
$car2 = $director->constructCar($builder);
|
class MusicPageBuilder implements PageBuilder {
|
||||||
echo $car2;
|
|
||||||
|
|
||||||
|
private Page $page;
|
||||||
|
|
||||||
exit();
|
|
||||||
|
|
||||||
define('LIMIT', 5);
|
|
||||||
define('OFFSET', 0);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// class Paginator
|
|
||||||
// {
|
|
||||||
const LIMIT = 5;
|
const LIMIT = 5;
|
||||||
const OFFSET = 0;
|
const OFFSET = 0;
|
||||||
private $data = [];
|
private $data = [];
|
||||||
private $limit = null;
|
private $limit = Null;
|
||||||
private $offset = null;
|
private $offset = Null;
|
||||||
private $feed = "";
|
private $flag = 'ajax';
|
||||||
private $paginator = "";
|
|
||||||
public function __construct($data)
|
|
||||||
{
|
public function __construct($data, $flag = 'ajax' ) {
|
||||||
|
|
||||||
|
$this->page = new Page();
|
||||||
|
$this->flag = $flag;
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
$this->limit = isset($_GET['limit']) ? $_GET['limit'] : self::LIMIT;
|
$this->limit = isset($_GET['limit'])? $_GET['limit'] : self::LIMIT;
|
||||||
$this->offset = isset($_GET['offset']) ? $_GET['offset'] : self::OFFSET;
|
$this->offset = isset($_GET['offset']) ? $_GET['offset']: self::OFFSET;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setFeed()
|
public function setFeed(){
|
||||||
{
|
|
||||||
$portial = array_slice($this->data, $this->offset, $this->limit);
|
$portial = array_slice($this->data, $this->offset, $this->limit);
|
||||||
$this->feed = '<ul id="feed">';
|
$this->page->feed ='<ul id="feed">';
|
||||||
foreach ($portial as $value) {
|
foreach ( $portial as $val ) {
|
||||||
$this->feed .= "<li>$value</li>";
|
$this->page->feed .= "<li> $val</li>";
|
||||||
}
|
}
|
||||||
$this->feed .= '';
|
$this->page->feed .= '</ul>';
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setPaginator()
|
public function setPaginator(){
|
||||||
{
|
|
||||||
$len = sizeof($this->data);
|
$len = sizeof($this->data);
|
||||||
$countPage = intdiv($len, $this->limit);
|
$contPage = intdiv($len, $this->limit);
|
||||||
$this->paginator = '';
|
for ($i = 0; $i < $contPage; $i++ ) {
|
||||||
for ($i = 0; $i < $countPage; $i++) {
|
$this->page->paginator .= '<a href="portial.php?offset='.self::LIMIT*$i.
|
||||||
$this->paginator .= '<a href= "./portial.php?offset=' . self::LIMIT * $i . '&limit=' . self::LIMIT . '">' . $i . '</a> ';
|
'&limit='. self::LIMIT .'">'. $i. '</a> ';
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function build()
|
public function build(){
|
||||||
{
|
$feed = $this->page->feed;
|
||||||
echo <<<abyr
|
$paginator = $this->page->paginator;
|
||||||
|
$pg = ($this->flag == 'ajax')?$feed: $paginator;
|
||||||
|
$this->page->html = <<< EOF
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Pangination</title>
|
<title>Document</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
$this->feed
|
$pg
|
||||||
<div id="paginator">
|
|
||||||
$this->paginator
|
|
||||||
</div>
|
|
||||||
<button type="button" click="getPage">Пам-пам</button>
|
|
||||||
|
|
||||||
|
<button id="paginator" data-id=1 type="button" ">Далее...</button>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
console.log('start script')
|
|
||||||
let parent = document.getElementById('paginator')
|
|
||||||
parent.addEventListener('click', getPage)
|
|
||||||
async function getFetch(url) {
|
async function getFetch(url) {
|
||||||
const response = await fetch(url, { method:'get'} )
|
const response = await fetch(url, { method:'get'} )
|
||||||
// return await response.json();
|
|
||||||
return await response.text();
|
return await response.text();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// parent = document.getElementById('paginator')
|
|
||||||
// console.log(parent)
|
|
||||||
parent.addEventListener('click' , getPage )
|
|
||||||
|
|
||||||
async function getPage(event) {
|
async function getPage(event) {
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
// let num_page = event.target.getAttribute('data-id')
|
|
||||||
|
let num_page = event.target.getAttribute('data-id')
|
||||||
|
console.log(num_page)
|
||||||
let limit = 5
|
let limit = 5
|
||||||
let url = "page.php?offset" + num_page + "&limit=" + limit
|
url = 'portial.php?offset=' + limit*num_page +'&limit=' + limit
|
||||||
console.log(url)
|
console.log(url)
|
||||||
let data = await getFetch(url)
|
let data = await getFetch(url)
|
||||||
|
if (!data) event.target.remove()
|
||||||
feed = document.getElementById('feed')
|
feed = document.getElementById('feed')
|
||||||
feed.innerHTML = feed.innerHTML + data
|
feed.innerHTML = feed.innerHTML + data
|
||||||
console.log(data)
|
event.target.setAttribute('data-id', parseInt(num_page) + 1)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const parent = document.getElementById('paginator')
|
||||||
|
parent.addEventListener('click' , getPage )
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
abyr;
|
|
||||||
|
EOF;
|
||||||
|
return $this->page;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Клиентский код
|
||||||
|
$builder = new MusicPageBuilder($arrMusic);
|
||||||
|
$page = $builder->setFeed()->setPaginator()->build();
|
||||||
|
echo $page . PHP_EOL;
|
||||||
|
|
||||||
// define('LIMIT', 5);
|
// $p = new Paginator($arrMusic);
|
||||||
// define('OFFSET', 0);
|
// $p->renderFeed();
|
||||||
|
// $p->renderPaginator();
|
||||||
|
// $p->renderPage();
|
||||||
|
|
||||||
// $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