[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

php EASY? write html table from data in folder

Posted on 2007-07-20
9
Medium Priority
?
287 Views
Last Modified: 2011-08-18
Hi all,
this one should be pretty easy.  I have a folder with changing files.  What I need to do is have php look into a folder, get all the file names with a php extension, then write an html table with the values of the folder contents in the table.
ie.
/stuff/
contains a.php, b.php x.php and y.php
so, write contents.html with value
<table width="300" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>a.php</td>
    <td>b.php</td>
    <td>x.php</td>
    <td>y.php</td>
  </tr>
</table>
I would have a sample code, but I have no idea where to start...  thanks in advance.
0
Comment
Question by:newbe101
  • 5
  • 3
9 Comments
 
LVL 33

Accepted Solution

by:
hongjun earned 1000 total points
ID: 19533757
Try this

<?php
$directory = "C:\\ZOrg";

if($dir_handle = opendir($directory)){
      echo "<table width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
      echo "<tr>";

      while($file = readdir($dir_handle))
      {
            if(eregi(".php", $file) && is_file($file))
            {
                  echo "<td>$file</td>";
            }
      }

      echo "</tr>";
      echo "</table>";
}
closedir($dir_handle);
?>


hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 19533762
However, you may wish to rearrange the layout.
I personally feel the layout is not really friendly.

hongjun
0
 

Author Comment

by:newbe101
ID: 19533971
this is on a unix box... so I can change the path to a relative path... correct?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:newbe101
ID: 19534097
OK, I actually need to write the data to a file... here is what I have come up with, but it doesnt' seem to be working....

$directory = "../../build/";
$headerext = ".html";
$headerfile = "../../build/$make$headerext";
$fh = fopen($headerfile, 'w') or die("can't open file");

if($dir_handle = opendir($directory)){
      $header = . "<table width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
      $header = . "<tr>";

      while($file = readdir($dir_handle))
      {
            if(eregi(".php", $file) && is_file($file))
            {
                  $header = . "<td>$file</td>";
            }
      }

      $header = . "</tr>";
      $header = . "</table>";
}
closedir($dir_handle);
$stringData = $header;
fwrite($fh, $stringData);
fclose($fh);
0
 

Author Comment

by:newbe101
ID: 19534263
OK, I fixed it somewhat.  It is now creating the file with the table structure, but it is not including the <td>$file</td> part... here is what I have:

$directory = "../../build/";
$directory2 = "../../build/$make1";
$headerext = ".html";
$headerfile = "../../build/$make1$headerext";

if($dir_handle = opendir($directory2)){
      $header .="<table width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
      $header .="<tr>";

      while($file = readdir($dir_handle))
      {
            if(eregi(".php", $file) && is_file($file))
            {
                  $header .="<td>$file</td>";
            }
      }

      $header .="</tr>";
      $header .="</table>";
}
closedir($dir_handle);

$fh = fopen($headerfile, 'w') or die("can't open file");
$stringData = $header;
fwrite($fh, $stringData);
fclose($fh);
0
 
LVL 13

Assisted Solution

by:MasonWolf
MasonWolf earned 1000 total points
ID: 19534738
You've just about got it - 1 tiny change:

            if(eregi(".php", $file) && is_file($directory2."/".$file))
            {
                  $header .="<td>$file</td>";
            }
0
 

Author Comment

by:newbe101
ID: 19534829
it's not looping.  It's only getting 1 file.
0
 

Author Comment

by:newbe101
ID: 19534846
oops... nevermind... I had to refresh the page... thanks guys.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 19536497
Glad you got things work out :)


hongjun
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question