Jump to content


Photo

how to split page after 10 entries


  • Please log in to reply
9 replies to this topic

#1 mr_sarge

mr_sarge
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 18 February 2006 - 08:16 AM

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:
<?php

$folder = opendir($whichSection . "./user/" . $who . "/"); 
$counter = 0; 
while($file = readdir($folder)){ 
   if($file != '.' && $file != '..'){ 
      $counter++; 
   } 
} 
closedir($folder); 
?>

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):
<?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" -->

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

#2 LazyJones

LazyJones
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 18 February 2006 - 01:57 PM

One solution could be read file into an array with file() function, then each page fetch the lines you need from that array.

#3 mr_sarge

mr_sarge
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 18 February 2006 - 04:47 PM

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

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 18 February 2006 - 05:19 PM

This (untested) might help:
<?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> ";
}
?>

Legend has it that reading the manual never killed anyone.
My site

#5 mr_sarge

mr_sarge
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 18 February 2006 - 07:22 PM

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.

#6 mr_sarge

mr_sarge
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 21 February 2006 - 01:04 AM

someone know how to make Andy's script to work?

Thanks

#7 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 21 February 2006 - 01:07 AM

My fault. The for loop opener was missing the opening { so try this ending:

$total_pages = ceil($counter/10);
for ($i=1;$i<$total_pages;$i++) {
    echo "<a href='". $_SERVER['SCRIPT_NAME']. "?page=". $i. "'> ". $i. "</a> ";
}
?>

Legend has it that reading the manual never killed anyone.
My site

#8 mr_sarge

mr_sarge
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 21 February 2006 - 12:14 PM

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!

#9 mr_sarge

mr_sarge
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 24 February 2006 - 01:42 AM

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?:

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



Thanks again!

#10 mr_sarge

mr_sarge
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 26 February 2006 - 09:52 PM

someone has any idea please?

Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users