mr_sarge Posted February 18, 2006 Share Posted February 18, 2006 Hi,I'm new to php and try to make something simple (I think !)I have a folder with PHP file that contain user data (1 file per user)I have a php page that look how many file there is in this folder, and then make an output of thoose file.How can I make this list to stop after 10 line, and than have a link to the page 2, 3, 4, etc. (that will refresh the current page with the next 10 line)Here is the code for the folder check:[code]<?php$folder = opendir($whichSection . "./user/" . $who . "/"); $counter = 0; while($file = readdir($folder)){ if($file != '.' && $file != '..'){ $counter++; } } closedir($folder); ?>[/code]and this is the code that return the content of the php file (I have shortened the file contend, in realty they have 10 entry each that appear in table, etc):[code]<?phpfor ($i=1; $i<=$counter; $i++){$c = "./user/";$p = ".php";require_once $c ."". $i ."". $p ?> <?php echo $user ?><br /> <?php echo $name ?><br /> <?php echo $country ?><br /><?php}?> <!-- and here I want "page: 2 - 3 - 4 - 5 - etc" -->[/code]Is there a way to do that easely? and I don't use database because it will never exeed 45-50 entry that are create and delete each monthThanks and sorry for my bad english, i'm french ;) Quote Link to comment https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/ Share on other sites More sharing options...
LazyJones Posted February 18, 2006 Share Posted February 18, 2006 One solution could be read file into an array with file() function, then each page fetch the lines you need from that array. Quote Link to comment https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/#findComment-11902 Share on other sites More sharing options...
mr_sarge Posted February 18, 2006 Author Share Posted February 18, 2006 Ok , thanks.I've read the array fonction page, but I don't really know how to use it !!could someone give me a hint on how to mkae it or send me an exemple so I can view how it work please?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/#findComment-11908 Share on other sites More sharing options...
AndyB Posted February 18, 2006 Share Posted February 18, 2006 This (untested) might help:[code]<?phpif (isset($_GET['page'])) { $page = $_GET['page'];} else { $page = 1;}$start = 10*($page-1) +1;$end = $start + 10;for ($i=$start;$i<$end;$i++) { $c = "./user/"; $p = ".php"; require_once $c ."". $i ."". $p; echo $user."<br />"; echo $name."<br />"; echo $country."<br />";}$total_pages = ceil($counter/10);for ($i=1;$i<$total_pages;$i++) echo "<a href='". $_SERVER['SCRIPT_NAME']. "?page=". $i. "'> ". $i. "</a> ";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/#findComment-11911 Share on other sites More sharing options...
mr_sarge Posted February 18, 2006 Author Share Posted February 18, 2006 Thanks Andy,The first part work. If I have 4 file it print 4 entries, if 5, print 5, etc.But it stop there. All thing after the "}" of the "For" don't work. I have try to remove the last part of code and finish with "} ?>" but the rest of my html page don't appear. Quote Link to comment https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/#findComment-11917 Share on other sites More sharing options...
mr_sarge Posted February 21, 2006 Author Share Posted February 21, 2006 someone know how to make Andy's script to work?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/#findComment-12046 Share on other sites More sharing options...
AndyB Posted February 21, 2006 Share Posted February 21, 2006 My fault. The for loop opener was missing the opening { so try this ending:[code]$total_pages = ceil($counter/10);for ($i=1;$i<$total_pages;$i++) { echo "<a href='". $_SERVER['SCRIPT_NAME']. "?page=". $i. "'> ". $i. "</a> ";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/#findComment-12047 Share on other sites More sharing options...
mr_sarge Posted February 21, 2006 Author Share Posted February 21, 2006 it's ok, I saw that the openner was not there.But it's the other loop that don't seam to be ok.it's stop after the last user detail, and if I close php there to close my table and put the footer, nothing appear ont the page. the source of the page stop with "canada<br />" that is the last line that appear witch is the last line in the loop.Thanks a lot for your help! Quote Link to comment https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/#findComment-12074 Share on other sites More sharing options...
mr_sarge Posted February 24, 2006 Author Share Posted February 24, 2006 I finally got it worked, thanks !But I decide to change my code, since if you delete a file (ex: 4.php) the page make error !Is there a manner to do the same think (split after 10 entrie) with the new code?:[code]$handle = opendir('./user/'); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { require_once $c ."". $file; ?> <?php echo $user ?><br /> <?php echo $year ?><br /> <?php echo $country ?><br /> <?php } } closedir($handle);[/code]Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/#findComment-12315 Share on other sites More sharing options...
mr_sarge Posted February 26, 2006 Author Share Posted February 26, 2006 someone has any idea please?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/#findComment-12631 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.