How To Split A Line From An Array Into Two Variables Using PHP
Here is the script as I am using it now:
<?php
echo '<html><body>';
// Data from a flat file
$dataArray = file('text.dat');
// Get the current page
if (isset($_REQUEST['page'])) {
$currentPage = $_REQUEST['page'];
} else {
$currentPage = 'some default value';
}
// Pagination settings
$perPage = 3;
$numPages = ceil(count($dataArray) / $perPage);
if(!$currentPage || $currentPage > $numPages)
$currentPage = 0;
$start = $currentPage * $perPage;
$end = ($currentPage * $perPage) + $perPage;
// Extract ones we need
foreach($dataArray AS $key => $val)
{
if($key >= $start && $key < $end)
$pagedData[] = $dataArray[$key];
}
foreach($pagedData AS $item)
echo '<a href="/'. $item .'/index.php">'. $item .'</a><br>';
if($currentPage > 0 && $currentPage < $numPages)
echo '<a href="?page=' . ($currentPage - 1) . '">« Previous page</a><br>';
if($numPages > $currentPage && ($currentPage + 1) < $numPages)
echo '<a href="?page=' . ($currentPage + 1) . '" class="right">Next page »</a><br>';
echo '</body></html>';
?>
And here is the contents of text.dat
Fun
Games
Toys
Sports
Fishing
开发者_高级运维Pools
Boats
Now, my question is, What if text.dat looked like this?:
Fun||http://site.com/page11.html
Games||http://site.com/page12.html
Toys||http://site.com/page13.html
Sports||http://site.com/page16.html
Fishing||http://site.com/page18.html
Pools||http://site.com/page41.html
Boats||http://site.com/page91.html
I would like to know what to change in the script so I can basically do this instead:
foreach($pagedData AS $item1 and $item2)
echo '<a href="'. $item2 .'">'. $item1 .'</a><br>';
Try:
foreach($pagedData AS $item) {
$item = explode('||', $item);
echo '<a href="/'. $item[1] .'/index.php">'. $item[0] .'</a><br>';
}
精彩评论