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