Jump to content

how to split page after 10 entries


mr_sarge

Recommended Posts

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]
<?php
for ($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 month

Thanks and sorry for my bad english, i'm french ;)
Link to comment
https://forums.phpfreaks.com/topic/3465-how-to-split-page-after-10-entries/
Share on other sites

This (untested) might help:
[code]<?php
if (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]
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.
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]
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!
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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.