Current location: Hot Scripts Forums » Programming Languages » PHP » showing all pages on a folder on index file.


showing all pages on a folder on index file.

Reply
  #1 (permalink)  
Old 08-10-06, 03:31 AM
itssami itssami is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
showing all pages on a folder on index file.

I have a folder, in that folder there are many pages, for example , page1.php,page2.php , pag3.php .. and i want to keep adding pages in that folder later.Now i want to display the names of all the pages which are in that folder but without extension.. for example, i want to display.like
archive:
page1 (Not page1.php)
page2
page3

and i want that it adds all the pages available in that folder, even which i will add later.
please help me regarding to this.
Thanks
Reply With Quote
  #2 (permalink)  
Old 08-10-06, 03:39 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

<?php


$dir 
'./pages/';
$handle opendir($dir);

while ((
$file readdir($handle)) !== false)
{
     if ( !
in_array($file, array('.''..') ) )
     {
           echo 
basename($file'.php') ."<br />\n";
     }
}

closedir($handle);

?>
Untested but should work.
Reply With Quote
  #3 (permalink)  
Old 08-10-06, 04:05 AM
itssami itssami is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
oh great, thank you very much nico_swd.. will it sort files by name ?
okay i have one more question please.for example i have 20 files in my folder..and it will display all files in one column.
i want to display each 10 files in one column, how can i print column .
for example.
i want to display like

1 11
2 12
3 13
4 ..
5
6
7
8
9
10
Reply With Quote
  #4 (permalink)  
Old 08-10-06, 07:05 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
Nico's code modified to allow alpha sorting

PHP Code:

<?php


$dir 
'./pages/';
$handle opendir($dir);
$file_list = array();

while ((
$file readdir($handle)) !== false)
{
     if ( !
in_array($file, array('.''..') ) )
     {
           
$file_list[] = basename($file'.php');
     }
}

closedir($handle);

asort($file_list);

foreach(
$file_list as $key => $value){
     echo 
"$value <br />\n";
}

?>
__________________
Always sanitise your data


Best regards
Reply With Quote
  #5 (permalink)  
Old 08-10-06, 07:25 AM
itssami itssami is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
thanks, but it shows the output in one column..i want to show the X number of items in one column.. how can i create output in multi columns please?
Reply With Quote
  #6 (permalink)  
Old 08-10-06, 08:03 AM
Barnz1986 Barnz1986 is offline
Aspiring Coder
 
Join Date: Jan 2006
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by itssami
thanks, but it shows the output in one column..i want to show the X number of items in one column.. how can i create output in multi columns please?
You will need to have 2 loops one nested within the other.
Reply With Quote
  #7 (permalink)  
Old 08-10-06, 08:14 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Assuming you only want to grab .php files:
PHP Code:

<?php


$fileArr 
glob'./pages/*.php' );

for ( 
$i 1$i <= count$fileArr ); $i++ )
{
    
$file basename$fileArr$i ], '.php' );
    echo 
"{$i}{$file}<br />\n";
}

?>
No need for multiple loops.
__________________
The toxic ZCE

Last edited by Keith; 08-10-06 at 08:28 AM.
Reply With Quote
  #8 (permalink)  
Old 08-10-06, 08:52 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Here is a way which forms the columns with 10 rows per column -
PHP Code:

<?php 


$dir 
'./pages/'
$handle opendir($dir); 
$file_list = array(); 

while ((
$file readdir($handle)) !== false

     if(!
in_array($file, array('.''..')) AND is_file($file)) 
     { 
           
$file_list[] = basename($file'.php'); 
     } 


closedir($handle); 

natcasesort($file_list); // do a natural sort

$file_list array_values($file_list); //reindex the array 0, 1, 2... so the following loop will work

$rows 10// how many rows in a column
$row = array(); // array to hold each row
$cnt count($file_list); // total number of files
$row_number 0;

for(
$index 0$index $cnt$index++){
    
$row[$row_number] .= "<td>".$file_list[$index]."</td>";
    
$row_number++;
    if(
$row_number >= $rows){
        
$row_number 0// reset for next column
    
}
}

$message "<table border=1>";
foreach(
$row as $value){ 
    
$message .= "<tr>";
    
$message .= $value
    
$message .= "</tr>\n";

$message .= "</table>";
echo 
$message;
?>
In addition to the code added to form the columns, this has two differences from the original code - it only lists files (it does not lists folder names within the folder) and it uses a natural sort (where file1.php, file10.php, file2.php are listed in the proper order file1.php, file2.php file10.php)

Edit: This also correctly orders upper and lowercase file names. With a case sensitive sort and the glob(...) function Page1.php (starts with uppercase) will appear before anything.php (starts with lowercase)
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 08-10-06 at 09:00 AM.
Reply With Quote
  #9 (permalink)  
Old 08-10-06, 09:24 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
A little fix on Mab's code.

This:
PHP Code:

if(!in_array($file, array('.''..')) AND is_file($file)) 

Should be
PHP Code:

if(!in_array($file, array('.''..')) AND is_file($dir $file)) 

Reply With Quote
  #10 (permalink)  
Old 08-10-06, 09:35 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks (I tested this in the same folder with the script and did not catch this for a different folder path.)
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload a file to a fix web folder of a website without using FTP software sujata_ghosh Perl 4 05-10-06 09:10 AM
Checking a file exists lee PHP 3 04-23-06 12:44 AM
I need to create an Index. file LindaBellppo New Members & Introductions 1 09-05-05 04:34 PM
showing realtime percentage of uploading file tallpaul858 PHP 2 06-01-05 02:04 PM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 04:28 PM


All times are GMT -5. The time now is 07:23 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.