クラスは、[次へ]見つかった場所でコンテンツを展開することによって機能します。しかし、問題は、nextがdivでラップされている<div>[next]</div>
ため、wysiwygエディターを使用しているためになります。最初のページで[次へ]を見つけると、</div>
後は2ページ目などに持ち越され、レイアウトが妨げられます。
したがって、テキストを展開するクラス:
function splitText($pageCont){
$this->textarray = explode('[next]', $pageCont);
$this->numPages = count($this->textarray);
}
とテキストのブロックをエコーするクラス
function getContent(){
if($this->page != 0){
echo $this->pageCont;
} else {
echo $this->textarray[0];
}
}
問題は、これを防ぐ方法、またはメインdiv内のdivがページレイアウトに影響を与えないようにする方法です。今のようにdivが唾を吐くと、右の列が右下に押し出されます
kuh-chanによる編集を含む完全なコード
class Pagebreak {
private $page; //page number
private $numPages; //number of total pages
private $textarray = array(); // array of conents
private $pageCont; //current page content
public function __construct(){
if (!isset($_GET['page'])) {
$this->page = 0;
} else {
$this->page = $_GET['page'];
}
}
public function splitText($pageCont){
$this->textarray = preg_split('/(?<!\<div\>)\[next\]/', $pageCont);
$this->numPages = count($this->textarray);
}
public function setPage(){
$this->pageCont = $this->textarray[$_GET['page'] -1];
}
public function getContent(){
if($this->page != 0){
echo $this->pageCont;
} else {
echo $this->textarray[0];
}
}
public function getPageLinks(){
echo "<p class=\"article_page_div\">\n";
//prev page
//if page number is more then 1 show previous link
if ($this->page > 1) {
$prevpage = $this->page - 1;
echo "<a class=\"article_paging\" href=\"?post=".$_GET['post']."&page=$prevpage\">". 'Prev</a> ';
}
//page numbers
for($i = 1; $i <= $this->numPages ; $i++){
if($this->numPages > 1){ // if more then 1 page show links
if(($this->page) == $i){ //if page number is equal page number in loop don't link it
echo "$i\n ";
} else {
if($this->page == 0){ //if no page numbers have been clicked don't link first page link
if($i == 1){
echo "$i\n ";
} else { // link the rest
echo "<a class=\"article_paging\" href=\"?post=".$_GET['post']."&page=$i\">$i</a>\n ";
}
} else { // link pages
echo "<a class=\"article_paging\" href=\"?post=".$_GET['post']."&page=$i\">$i</a>\n ";
}
}
}
}
//next page
//if page number is less then the total number of pages show next link
if ($this->page <= $this->numPages - 1) {
if($this->page == 0){ //if no page numbers have been clicked minus 2 from the next page link
$nextpage = $this->page + 2;
} else {
$nextpage = $this->page + 1;
}
echo "<a class=\"article_paging\" href=\"?post=".$_GET['post']."&page=$nextpage\">". 'Next</a>';
}
echo "</p>\n";
}
}
次に、このクラスを呼び出すには、次を使用します。
$paginate = new Pagebreak();
$paginate->splitText($storyCont);
$paginate->setPage();
$paginate->getContent();
$paginate->getPageLinks();