All4172 Posted April 13, 2007 Share Posted April 13, 2007 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++; } ?> Quote Link to comment Share on other sites More sharing options...
rcorlew Posted April 13, 2007 Share Posted April 13, 2007 $single_page is set up as being a 0, and 0*25 =0 You should try setting it as 1 and see what happens. Quote Link to comment Share on other sites More sharing options...
All4172 Posted April 13, 2007 Author Share Posted April 13, 2007 $single_page is set up as being a 0, and 0*25 =0 You should try setting it as 1 and see what happens. I have it as $single_page = 1; above the while statement in the code unless you meant something else is set to 0 above? Quote Link to comment Share on other sites More sharing options...
rcorlew Posted April 13, 2007 Share Posted April 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
All4172 Posted April 13, 2007 Author Share Posted April 13, 2007 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? Quote Link to comment Share on other sites More sharing options...
Lumio Posted April 13, 2007 Share Posted April 13, 2007 <?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 Quote Link to comment Share on other sites More sharing options...
All4172 Posted April 13, 2007 Author Share Posted April 13, 2007 Thanks for that. That did the trick with a few minor adjustments. I'll defantly have to study what you put together as some the things such as $d->path; and isset($_GET['p']) ? $_GET['p']:1; (the ? in the middle) I'm not familiar with. Quote Link to comment Share on other sites More sharing options...
Lumio Posted April 13, 2007 Share Posted April 13, 2007 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; Quote Link to comment 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.