Jump to content

Archived

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

mr_sarge

how to split page after 10 entries

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 ;)

Share this post


Link to post
Share on other sites
One solution could be read file into an array with file() function, then each page fetch the lines you need from that array.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

×

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.