In daily development work, we often need to process a large amount of CSV file data and display it on the page. PHP provides many powerful functions to help us complete these tasks efficiently, among which array_chunk is a very practical function.
This article will take you step by step through an example showing how to use array_chunk to process CSV file data and display it paginated.
First, we need to understand the basic usage of the array_chunk function. The array_chunk function divides an array into multiple small arrays, and the size of each small array is determined by the specified second parameter.
grammar:
array array_chunk ( array $array , int $size [, bool $preserve_keys = false ] )
$array : The original array that needs to be split.
$size : The size of each array after splitting.
$preserve_keys : Whether to retain the key name of the original array, the default is false , indicating that the reset key name is a continuous integer.
Next, we will use PHP to process CSV files, segment data, and implement the function of pagination display.
First, suppose we have a CSV file that stores user information or other types of data. We need to read this file and convert it into a PHP array.
<?php
// Read CSV document
$file = 'data.csv'; // Assumptions CSV document名为 data.csv
$data = array();
if (($handle = fopen($file, 'r')) !== false) {
while (($row = fgetcsv($handle)) !== false) {
$data[] = $row; // Save each row of data into an array
}
fclose($handle);
} else {
die('document打开失败!');
}
?>
Suppose we want to display 10 records per page, we can use the array_chunk function to split the data into 10 blocks per page.
<?php
// Displayed per page 10 Data
$page_size = 10;
$data_chunked = array_chunk($data, $page_size);
?>
At this point, the $data_chunked array will contain multiple small arrays, each small array containing up to 10 pieces of data.
Next, we need to implement the paging function. By querying URL parameters, determine the current page, and then display the data of the corresponding page.
<?php
// Get the current page number,The default value is 1
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$total_pages = count($data_chunked); // Total page count
// If the page parameters are not valid,The first page is displayed by default
if ($page < 1 || $page > $total_pages) {
$page = 1;
}
// Get the data of the current page
$current_page_data = $data_chunked[$page - 1];
?>
Now, we can display the data on the current page. Assuming each piece of data is a user information, we can display this data on a web page.
<?php
echo "<h2>Current number $page Page data:</h2>";
echo "<table border='1'>";
echo "<tr><th>username</th><th>Mail</th><th>Telephone</th></tr>";
foreach ($current_page_data as $row) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row[0]) . "</td>";
echo "<td>" . htmlspecialchars($row[1]) . "</td>";
echo "<td>" . htmlspecialchars($row[2]) . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Finally, we need to display the pagination navigation at the bottom of the page so that the user can browse other pages.
<?php
// Show paging navigation
echo "<div>";
if ($page > 1) {
echo "<a href='?page=" . ($page - 1) . "'>Previous page</a> ";
}
echo "Current page:$page / $total_pages ";
if ($page < $total_pages) {
echo "<a href='?page=" . ($page + 1) . "'>Next page</a>";
}
echo "</div>";
?>
Combining all the above codes, the complete PHP sample code is as follows:
<?php
// Read CSV document
$file = 'data.csv'; // Assumptions CSV document名为 data.csv
$data = array();
if (($handle = fopen($file, 'r')) !== false) {
while (($row = fgetcsv($handle)) !== false) {
$data[] = $row; // Save each row of data into an array
}
fclose($handle);
} else {
die('document打开失败!');
}
// Displayed per page 10 Data
$page_size = 10;
$data_chunked = array_chunk($data, $page_size);
// Get the current page number,The default value is 1
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$total_pages = count($data_chunked); // Total page count
// If the page parameters are not valid,The first page is displayed by default
if ($page < 1 || $page > $total_pages) {
$page = 1;
}
// Get the data of the current page
$current_page_data = $data_chunked[$page - 1];
// 显示Current page的数据
echo "<h2>Current number $page Page data:</h2>";
echo "<table border='1'>";
echo "<tr><th>username</th><th>Mail</th><th>Telephone</th></tr>";
foreach ($current_page_data as $row) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row[0]) . "</td>";
echo "<td>" . htmlspecialchars($row[1]) . "</td>";
echo "<td>" . htmlspecialchars($row[2]) . "</td>";
echo "</tr>";
}
echo "</table>";
// Show paging navigation
echo "<div>";
if ($page > 1) {
echo "<a href='?page=" . ($page - 1) . "'>Previous page</a> ";
}
echo "Current page:$page / $total_pages ";
if ($page < $total_pages) {
echo "<a href='?page=" . ($page + 1) . "'>Next page</a>";
}
echo "</div>";
?>
Through the above example, we understand how to use PHP's array_chunk function to split the data in a CSV file into multiple small arrays, and combine the paging function to display the data on a web page. In actual applications, in addition to basic paging logic, the user experience can also be improved through AJAX loading data, adding search functions, etc.
I hope this article can help you better process CSV file data in actual projects and realize efficient pagination display function!