Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Help with simple PHP script

Help with simple PHP script
Thread Tools
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
May 18, 2003, 11:05 PM
 
I currently have a PHP script set up to deal with users' contact information. Users enter their info into a form and my PHP script formats the contact info into a neat HTML table. That table gets mailed off to blogger and blogger handles appending the post to a web page. It works well except that it can't be sorted. I knew of that limitation before I started this system so I also made the PHP script write the table out to a file, a new file for each user, the name of the file is the user's full name (last, first) and I have those files saved to one of 3 directories (depending on a category that the user selects in the form).

That works fine but now I need to create a PHP script to take an entire folder of these files that contain tables and display to the browser one long html file of all of the tables, in alphabetical order.

I don't really know PHP (I've been copying and modifying code from various places to get this stuff done so far). So I don't really know where to start for this one. Can anyone help me out? Thanks.
     
Dedicated MacNNer
Join Date: Aug 2002
Status: Offline
Reply With Quote
May 18, 2003, 11:32 PM
 
one way to do this:
1) make an array
2) find all files in the dir. and start a for loop (probably use foreach)
3) open each file and explode by table
4) append to the array
5) sort array
6) print array

just seems logical, I am not sure the exact way for #2 but i can help with the others.
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
May 18, 2003, 11:43 PM
 
OK thanks. I know I can use readfile() to read in a file into a buffer and I can then echo that buffer out. I guess my question is how do I go through every file in a directory, in order?

Your idea would be to make the array and then sort it, but since I want everything sorted by filename, shouldn't I just be able to read them in the right order?
     
Dedicated MacNNer
Join Date: Aug 2002
Status: Offline
Reply With Quote
May 18, 2003, 11:57 PM
 
Code:
while ($fileName = $dir->read()) { if ($fileName != "." && $fileName != "..") { if (is_file($fileName)) { echo "$fileName \n";
take a look at php.net for more nice examples.

also just a personal preferance, i like fopen() more then readfile()
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
May 19, 2003, 12:09 AM
 
Parse error: parse error, unexpected $ in /var/www/html/members/summer/contacts03/test.php on line 9

That's what I get when I run that script as is. What do I need to fix? (it's only 8 lines long so I don't understand the error.
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
May 19, 2003, 12:12 AM
 
ok that was dumb of me. now that i've closed all three braces I get this error:

Fatal error: Call to a member function on a non-object in /var/www/html/members/summer/contacts03/test.php on line 2
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
May 19, 2003, 05:03 AM
 
Originally posted by waffffffle:
ok that was dumb of me. now that i've closed all three braces I get this error:

Fatal error: Call to a member function on a non-object in /var/www/html/members/summer/contacts03/test.php on line 2
He's used an example which calls a class which you've probably not included with your code:

$dir->read();

Perhaps do a search on PHP.net for that line of code and you'll find the missing bits...
Computer thez nohhh...
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
May 19, 2003, 11:31 AM
 
OK so I found the info on dir:

http://www.php.net/manual/sl/class.dir.php

but how do I include it? It is like C? Sorry but PHP is pretty new to me.
     
Dedicated MacNNer
Join Date: Aug 2002
Status: Offline
Reply With Quote
May 19, 2003, 12:01 PM
 
$d = dir("/etc");
where /etc is your starting directory
and make sure to finish with
$d->close();

($d is a handle of class dir if that helps)
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
May 19, 2003, 01:04 PM
 
thanks but I think I got it on my own. I found this script that lists the contents of a directory and I modified it to print out the directory:

<?php
function getDirList ($dirName) {
$d = dir($dirName);
while($entry = $d->read()) {
if ($entry != "." && $entry != "..") {
if (is_dir($dirName."/".$entry)) {
getDirList($dirName."/".$entry);
} else {
$string = "html";
if(strstr($entry,$string)) {
//echo "$entry<br>\n";
$filbuffer = readfile($entry);
echo "$filebuffer<br>\n";
}
}
}
}
$d->close();
}
getDirList(".");
?>

It works nicely. Thanks to everyone for the help.
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
May 21, 2003, 11:58 PM
 
this seemed to be working fine for a while but now all of the new items that are added are getting put at the bottom of the list. Why would this script read the files out of order? At first it looked like a permissions thing (the files were being created by a different owner and with different permissions because I recently moved the entire site to a different server. However I went in and manually fixed the permissions and I still get the same problem. I don't get it.
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:34 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2