csv分页在php中不起作用
在我的csv文件(“food.csv”)中,数据按“项目、描述、数量、费率、类型”的顺序存储,共30行5列。为了获得匹配查询和分页的输出,我准备了一个外部分页文件“pagination.php”,它工作正常,主页面显示错误。我已经查了很多次了,但还是没能找出错误。如果分页已取消链接,则输出正常 “food.csv”包含:(外部) pagination.php(外部)csv分页在php中不起作用,php,csv,pagination,Php,Csv,Pagination,在我的csv文件(“food.csv”)中,数据按“项目、描述、数量、费率、类型”的顺序存储,共30行5列。为了获得匹配查询和分页的输出,我准备了一个外部分页文件“pagination.php”,它工作正常,主页面显示错误。我已经查了很多次了,但还是没能找出错误。如果分页已取消链接,则输出正常 “food.csv”包含:(外部) pagination.php(外部) 人力资源{ 边界:1px#ccc; 边框样式:无实体无; 利润率:20px0; } a{ 背景:#BF3E18; 颜色:#FFF
人力资源{
边界:1px#ccc;
边框样式:无实体无;
利润率:20px0;
}
a{
背景:#BF3E18;
颜色:#FFFFFF;
填充:5px0 5px19px;
边距:0 1px 0;
文字装饰:无;
}
a:悬停{
背景:#3C6491;
颜色:#FFFFFF;
}
a、 精选{
颜色:#FFFFFF;
背景:822C0F;
文字装饰:下划线;
字体大小:粗体;
}
.数字{
线高:20px;
字距:4px;
}
柯尔先生{
边框:1px实心#ddd;
填充:3倍;
}
生成什么错误?出现空白页。看来pagination.php的链接可能是错误的。
ITEM,DESC,QTY,RATE,TYPE
100,MANGO,10,200,NEW
110,ORANGE,13,50,OLD
150,MANGO,20,400,NEW
.....
....
.....
<html>
<head>
<style>
hr {
border: 1px #ccc;
border-style: none none solid none;
margin: 20px 0;
}
a {
background: #BF3E18;
color: #FFFFFF;
padding: 5px 0 5px 19px;
margin: 0 0 1px 0;
text-decoration: none;
}
a:hover {
background: #3C6491;
color: #FFFFFF;
}
a.selected {
color: #FFFFFF;
background: #822C0F;
text-decoration: underline;
font-weight:bold;
}
.numbers {
line-height: 20px;
word-spacing: 4px;
}
.curr{
border:1px solid #ddd;
padding:3px;
}
</style>
</head>
<body>
<?php
class pagination
{
var $page = 1; // Current Page
var $perPage = 10; // Items on each page, defaulted to 10
var $showFirstAndLast = false; // if you would like the first and last page options.
function generate($array, $perPage = 10)
{
// Assign the items per page variable
if (!empty($perPage))
$this->perPage = $perPage;
// Assign the page variable
if (!empty($_GET['page'])) {
$this->page = $_GET['page']; // using the get method
} else {
$this->page = 1; // if we don't have a page number then assume we are on the first page
}
// Take the length of the array
$this->length = count($array);
// Get the number of pages
$this->pages = ceil($this->length / $this->perPage);
// Calculate the starting point
$this->start = ceil(($this->page - 1) * $this->perPage);
// Return the part of the array we have requested
return array_slice($array, $this->start, $this->perPage);
}
function links()
{
// Initiate the links array
$plinks = array();
$links = array();
$slinks = array();
// Concatenate the get variables to add to the page numbering string
if (count($_GET)) {
$queryURL = '';
foreach ($_GET as $key => $value) {
if ($key != 'page') {
$queryURL .= '&'.$key.'='.$value;
}
}
}
// If we have more then one pages
if (($this->pages) > 1)
{
// Assign the 'previous page' link into the array if we are not on the first page
if ($this->page != 1) {
if ($this->showFirstAndLast) {
$plinks[] = ' <a href="?page=1'.$queryURL.'">«« First </a> ';
}
$plinks[] = ' <a href="?page='.($this->page - 1).$queryURL.'">« Prev</a> ';
}
// Assign all the page numbers & links to the array
for ($j = 1; $j < ($this->pages + 1); $j++) {
if ($this->page == $j) {
$links[] = ' <a class="selected">'.$j.'</a> '; // If we are on the same page as the current item
} else {
$links[] = ' <a href="?page='.$j.$queryURL.'">'.$j.'</a> '; // add the link to the array
}
}
// Assign the 'next page' if we are not on the last page
if ($this->page < $this->pages) {
$slinks[] = ' <a href="?page='.($this->page + 1).$queryURL.'"> Next » </a> ';
if ($this->showFirstAndLast) {
$slinks[] = ' <a href="?page='.($this->pages).$queryURL.'"> Last »» </a> ';
}
}
// Push the array into a string using any some glue
return implode(' ', $plinks).implode($this->implodeBy, $links).implode(' ', $slinks);
}
return;
}
}
?>
<ul>
<? foreach($items as $item): ?>
<li><?= $item ?></li>
<? endforeach ?>
</ul>
<? if($prev_page): ?>
<a href="pager.php?page=<?= $prev_page ?>"> << </a>
<? endif ?>
<? for($i = 1; $i <= $qty_pages; $i++): ?>
<a href="pager.php?page=<?= $i ?>" class="<?= ($i == $curr_page) ? 'curr' : '' ?>"><?= $i ?></a>
<? endfor ?>
<? if($next_page): ?>
<a href="pager.php?page=<?= $next_page ?>"> >> </a>
<? endif ?>
</body>
</html>
<?php
// Include the pagination class
include 'pagination.php';
// Create the pagination object
$pagination = new pagination;
// get data
while (!feof($handle )){
$data = fgetcsv($handle, 4096, ",");
if($data[1] =="MANGO"){
// If we have an array with items
if (count($data)) {
// Parse through the pagination class
$dataPages = $pagination->generate($data, 10);
// If we have items
if (count($dataPages) != 0) {
// Create the page numbers
// Loop through all the items in the array
foreach ($dataPages as $dataArray) {
// Show the information about the item
$output .= sprintf( "<b>Fruit Name: %s. </b><br>", $data[1]);
$output .= sprintf( "Quantity Avaliable: %d Kgs @ %d USD each. <br>", $data[2], $data[3]);
$output .= sprintf( "Item code: %d (Stock: %s) <br><hr><br>", $data[4],$dara[0]);
echo $output;
}
// print out the page numbers beneath the results
echo $pageNumbers = '<div class="numbers">'.$pagination->links().'</div>';
}
}
?>