Jump to content

[SOLVED] Help with loops / page numbers


All4172

Recommended Posts

What I'm trying to do, is look through a directory, grab all the files, store it into a variable, then divide the total number of files by 25 (the amount I want on each page) then echo it onto each page.  I have everything set up thus far, however, I seem to be only able to get the 1st page to show the 1-25 and each page thereafter shows up blank. 

 

I think the problem is where I call  if ($_GET["page"]==$single_page).  I've tried changing that to WHILE but it ends up looping till I get a 30 second timeout.  Any ideas or suggestions?

 

<?php 

$i = 0;
$t = 0;

if ($handle = opendir('/home/webpath/www/mydir')) {

    // List all the files
    while (false !== ($file = readdir($handle))) {
    if(preg_match('/(\.php)$/i',$file))
{

$file1 = str_replace(".php", '', $file);



$list_array[$i] = '<a href="http://www.example.com/mydir/'.$file.'">'.$file1.'</a><p>';
$i++;

}}}

closedir($handle);

echo "Total files: ".$i."<P>";

$pages = $i / 25;

$pages = (ceil($pages));

sort($list_array);

$single_page = 1;


while ($pages >= $single_page) 
   {
    if ($_GET["page"]==$single_page) 
     {
      $min_num = ($single_page - 1) * 25;
      $max_num = $single_page * 25;
      echo "Min: ".$min_num."<p>";
      echo "Max: ".$max_num."<p>";
        while ($t >= $min_num && $t < $max_num) 
           {
             echo "<p><li>".$list_array[$t];
             $t++;
            }
      }
$single_page++;
    }

?>

Link to comment
https://forums.phpfreaks.com/topic/46820-solved-help-with-loops-page-numbers/
Share on other sites

You are setting as 0 here

 

    if ($_GET["page"]==$single_page)

    {

      $min_num = ($single_page - 1) * 25;

 

$single_page is set to 1 on the initial call and then you subtract that 1 to make it 0

 

I do not see how you are passing the variable from page to page to make the $single_page increase it's value to the next page.

I see what your saying.  I've modified it to:

 

if ($single_page == "1") {
$min_num = 1;}
else {
$min_num = ($single_page - 1) * 25;}

 

Also after doing further tests, if I change it from while ($t >= $min_num && $t < $max_num) to while ($t < $max_num)  it'll work.  Seems that when the script from page 2 on it sees $t as 0 again and doesn't not executue the while statement at all.  I was under the assumption it would loop through till the min start, and then loop out until the max.  Hmm any ideas?

<?php
$d = dir('./');
$path = $d->path;
//read all files
while ( ($item = $d->read()) !== false) {
	if (is_file($path.$item)) {
		$items[] = $item;
	}
}

//calc page and pages
$pages = ceil(count($items) / 25);
$p = isset($_GET['p']) ? $_GET['p']:1;
if ($p < 1 || $p > $pages) $p = 1;

echo $pages.' pages. You are on page '.$p.'<br><br>';

$p--;
$p = $p * 25;

$p_end = $p+25;
if ($p_end > count($items)) $p_end = (count($items)-1);

//echo files
for ($i=$p; $i<=$p_end; $i++) {
	echo $items[$i].'<br>';
}
?>

you can use it by writing ?p=1 and so on

I gave $d an dir-object. Look here: http://www.php.net/class.dir

 

That thing with the ? is like an if-structure.

An example:

<?php
   $var = true;
   if ($var === true) {
      echo "it's true";
   }else {
      echo "it's false";
   }
?>

That's the same as

<?php
   echo ($var === true) ? "it's true":"it's false";
?>

Structure:

(expression) ? accords : else;

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.