cinek Posted November 2, 2010 Share Posted November 2, 2010 I have a pagination file. It lists all pages on the site. Now, I'm storing all data in a text file which I get through an array. How can I link both together, so that only 5 items are displayed on every page?here's the code where I echo stuff out onto the page: $i = 0; for($i = 0; $i < $noarr; $i++) { $no = $contents[$i][0]; $a= $contents[$i][1]; $b= $contents[$i][2]; $c= $contents[$i][3]; //$dir = opendir("a/".$a."/"); echo "<img src='a/".$a."/1.jpg' />"; echo "asdf: $b\n"; echo "saf: <a href='$link'>$a</a> <br />"; } here's the pagination code: <?php $page = (isset($_GET['page']) ? (int)$_GET['page'] : 1); $projects = 5; $data = file_get_contents("config.txt"); $rows = explode("\n", $data); $noOfPages = ceil(count($rows) / $projects); $index = 1; $pagination = ''; if($page !== 1 && $noOfPages > 1) { $pagination .= "<a href='?page=".($page-1)."'><<Back</a> "; } while($index <= $noOfPages) { $pagination .= '<a '.(($index === $page) ? 'id="this_page"' : '').' href="?page='.$index.'">'.$index.'</a> | '; $index++; } $pagination = substr($pagination, 0, -2); if($page < $noOfPages) { $pagination .= ' <a href="?page='.($page+1).'">Next Page>></a>'; } echo $pagination; $index = ($page === 1) ? 0 : ($page*$projects) - $projects; $end = $index + $projects; ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted November 2, 2010 Share Posted November 2, 2010 for($i = $start; $i < $start + $per_page; $i++)$start is the array index to start showing, and $per_page is how many to show per page. You'll also want to use isset inside the loop to make sure the array element is actually set (in case the page has less than 5 items). Link to comment Share on other sites More sharing options...
cinek Posted November 2, 2010 Author Share Posted November 2, 2010 ok, this is more or less this: $i = 0; for($i = 0; $i < $noarr; $i++) { $no = $contents[$i][0]; $a= $contents[$i][1]; $b= $contents[$i][2]; $c= $contents[$i][3]; //$dir = opendir("a/".$a."/"); echo "<img src='a/".$a."/1.jpg' />"; echo "asdf: $b\n"; echo "saf: <a href='$link'>$a</a> <br />"; } now this echos everything out. I think I need to echo it from the pagination file. Problem is, I also need to specify the variables, so echoing the rows, does not help at all :)this is what I came up with (at the end of the pagination file) while($index < $end) { if (isset($rows[$index])) { echo $rows[$index]; } $index++; } it lists the 5 rows(lines from the text file), but since they are not assigned to any variables like below, it's no use $i = 0; for($i = 0; $i < $noarr; $i++) { $no = $contents[$i][0]; $a= $contents[$i][1]; $b= $contents[$i][2]; $c= $contents[$i][3]; //$dir = opendir("a/".$a."/"); echo "<img src='a/".$a."/1.jpg' />"; echo "asdf: $b\n"; echo "saf: <a href='$link'>$a</a> <br />"; } EDIT: nvm, got it Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.