簡體   English   中英

Php動態頁面分頁

[英]Php dynamic page pagination

所以我有這個頁面列出了網站上的'.txt'文件內容。 我試圖對它進行分頁,但它不起作用。 我想每頁只有一個故事(一個故事是數據[0]。數據[1])該頁面通過ajax調用到瀏覽器。 這是我的代碼:

<?php
$dataArray = array();
//Number of chars for the string
$num = 500;
$dir = '../php/biralas_tortenetek/';
$willcount = readdir(opendir($dir));
$totfiles = count(readdir(opendir($dir)));
//Check if </div>DIR e</div>xists
if ($handle = opendir($dir)) {
    //Loop over the directory
    while (false !== ($file = readdir($handle))) {
        //Strip out the . and .. files
        if ($file != "." && $entry != "..") {
            //Store file contents
            $filecontent = file_get_contents($dir . $file);
            //Split the content and store in array
            $length = strlen($filecontent);
            $dataArray[] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length ));

        }
    }
    //close the dir
    closedir($handle);
}


?><?php 
$page = isset($_GET['page']) ? $_GET['page']-1 : 0;
echo "<br/>";
for($x=$page*1; $x < $totfiles && $x < ($page+1)*12; $x++)
{
    foreach($dataArray as $data) { ?>
        <div class="visible">
            <?php echo $data[0] . $data[1]; ?>
        </div><?php } ?>
</div>
<?php }
for($page=1; ($page-1)*12 < $totfiles; $page++)
{
    echo "<div class='lapozo'><a onclick='story_changepage($page);' href='../html/blog.php#tortenetek?page=$page'>$page</a></div>"; 
}
?>

同樣,目標是每頁只有一個故事。 謝謝!

第一解決方案

$willcount = readdir(opendir($dir));
$i = 0;
//Check if </div>DIR e</div>xists
if ($handle = opendir($dir)) {
    //Loop over the directory
    while (false !== ($file = readdir($handle))) {
        //Strip out the . and .. files
        if ($file != "." && $entry != "..") {
            //Store file contents
            $filecontent = file_get_contents($dir . $file);
            //Split the content and store in array
            $length = strlen($filecontent);
            // store file as indexed item of array
            $dataArray[$i++] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length ));
        }
    }
    //close the dir
    closedir($handle);
}
// store total files in dir
$totfiles = $i;

$page = isset($_GET['page']) ? $_GET['page']-1 : 0;
echo "<br/>";

for($x=$page*12; $x < $totfiles && $x < ($page+1)*12; $x++) {
    $data = $dataArray[$x];
    ?>
        <div class="visible">
            <?php echo $data[0] . $data[1]; ?>
        </div>
<?php 
}

CUT 2nd Solution://首選它比第一個解決方案使用更少的內存

$willcount = readdir(opendir($dir));
$i = 0;
// get page 
$page = isset($_GET['page']) ? $_GET['page']-1 : 0;

//Check if </div>DIR e</div>xists

if ($handle = opendir($dir)) {
    //Loop over the directory
    while (false !== ($file = readdir($handle))) {
        //Strip out the . and .. files
        if ($file != "." && $entry != "..") {
            $i ++; 
            // if our page not first, skip add
            if ($i <= $page * 12) continue;
            // if we reach end of the page, break
            if ($i > ($page + 1)* 12) break;
            //Store file contents
            $filecontent = file_get_contents($dir . $file);
            //Split the content and store in array
            $length = strlen($filecontent);
            $dataArray[] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length ));
        }
    }
    //close the dir
    closedir($handle);
}
// store total files in dir
$totfiles = $i;

echo "<br/>";

foreach($dataArray as $data) {
    ?>
        <div class="visible">
            <?php echo $data[0] . $data[1]; ?>
        </div>
<?php 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM