如何让分页在 PHP 中抛出 csv 文件?

我有一个 csv 文件,里面有一些数据。当用户在搜索栏中键入内容时,它会返回相关数据但这些结果太多,我想分页。我不知道,google了很多但找不到方法。请帮忙。


$filename = "Database.csv";

            $delimiter = ",";

            if (!file_exists($filename) || !is_readable($filename))

                return false;


            if ($delimiter == ',') {

                $csv = array_map('str_getcsv', file($filename));

            } else {

                $lines = file($filename);

                $line_num = count($lines);

                $dm = [];


                $csv = array_map('str_getcsv', $lines, array_pad($dm, $line_num, $delimiter));

            }


            array_walk($csv, function (&$row) use ($csv) {

                $row = array_combine($csv[0], $row);

            });


            array_shift($csv);

            $total_row_of_csv = count($csv);

            $total_row = 0;

            for ($i = 0; $i < $total_row_of_csv; $i++) {

                if (preg_match("/$value/i", $csv[$i]['Catagory'])) {                                                       

                        echo "<tr>";

                        echo "<td>" . $csv[$i]['Ref'] . "</td>";

                        echo "<td>" . $csv[$i]['Catagory'] . "</td>";

                        echo "<td>" . $csv[$i]['JobTitle'] . "</td>";

                        echo "<td>" . $csv[$i]['Description'] . "</td>";

                        echo "<td>" . $csv[$i]['Salary'] . "</td>";                        

                        echo "<td>" . $csv[$i]['Nature'] . "</td>";

                        echo "</tr>";                    

                        $total_row++;

                    }                                                          

                }


慕码人8056858
浏览 110回答 1
1回答

PIPIONE

获取:index.php?page=2&search=purchasing清除 page=1 以进行新搜索这个功能是用get方法完成的,而不是post,你可以通过适当的方式来做post<?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // you can make this as post as you want&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // i have made this as a get param function&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $value = !empty($_GET['search']) ? $_GET['search'] : '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filename = "Database.csv";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $delimiter = ",";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!file_exists($filename) || !is_readable($filename))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($delimiter == ',') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $csv = array_map('str_getcsv', file($filename));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $lines = file($filename);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $line_num = count($lines);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dm = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $csv = array_map('str_getcsv', $lines, array_pad($dm, $line_num, $delimiter));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_walk($csv, function (&$row) use ($csv) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row = array_combine($csv[0], $row);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_shift($csv);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total_row_of_csv = count($csv);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $countForPage = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($i = 0; $i < $total_row_of_csv; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var_dump($csv[$i]['Catagory']);die();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // didint get the $value so commented id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (preg_match("/$value/i", $csv[$i]['Catagory'])) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $countForPage ++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $countForPage;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $numPerPage = 10;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $numCurrPage = !empty($_GET['page']) ? $_GET['page'] : 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $numFromCnt = $numPerPage * ($numCurrPage - 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $numLastCnt = $numFromCnt + $numPerPage ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total_row = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<table>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($i = 0; $i < $total_row_of_csv; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var_dump($csv[$i]['Catagory']);die();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // didint get the $value so commented id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (preg_match("/$value/i", $csv[$i]['Catagory'])) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //echo $total_row .">=". $numFromCnt ."&&". $total_row ."<". $numLastCnt; //die();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($total_row >= $numFromCnt && $total_row < $numLastCnt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $csv[$i]['Ref'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $csv[$i]['Catagory'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $csv[$i]['JobTitle'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $csv[$i]['Description'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $csv[$i]['Salary'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $csv[$i]['Nature'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$total_row++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //echo $total_row;die();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</table>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $links = generateLinks($countForPage, $numCurrPage, $numPerPage, $value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $links;&nbsp; &nbsp; &nbsp; &nbsp; function generateLinks($total_row_of_csv, $numCurrPage, $numPerPage, $search){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pagLink = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($numCurrPage > 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pagLink .= "<li class='active'><a href='csv_pagination.php?page="&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .($numCurrPage-1)."&search=".$search."'>Prev</a></li>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // for ($i=1; $i<=$total_row_of_csv; $i++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp;if ($i==$numCurrPage) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$pagLink .= "<li class='active'><a href='csv_pagination.php?page="&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.$i."'>".$i."</a></li>";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp;} else&nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$pagLink .= "<li><a href='csv_pagination.php?page=".$i."'>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;".$i."</a></li>";&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($numCurrPage < ($total_row_of_csv/$numPerPage)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pagLink .= "<li class='active'><a href='csv_pagination.php?page="&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .($numCurrPage+1)."&search=".$search."'>Next</a></li>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $pagLink;&nbsp; &nbsp; &nbsp; &nbsp; }?>
打开App,查看更多内容
随时随地看视频慕课网APP