Page 1 of 1
Loop and sorting prob with read_dir results - v perplexing
Posted: Sat Mar 11, 2006 12:51 pm
by mattcooper
Hi again all!
I have written this nice'n'simple script to look for files in a folder that are related to the current page. If found, the .php extension is trimmed off as well as everything but the last character. If the last character is a number, it is echoed out as a link to display the next page in the series.
Here it is:
Code: Select all
if ($handle = opendir('includes')) {
while (false !== ($file = array(readdir($handle)))) {
$i=0;
foreach($file as $num){
//strip off the .php extension
$stripped=substr($num, 0, -4);
//echo "$stripped\n";
$pagename=$_GET['pagename'];
if(eregi($pagename,$stripped)){
$start=strlen($stripped)-2;
$num=substr($stripped, -1, $start);
if(is_numeric($num)){
echo "<a href=\"$PHP_SELF?pagename={$pagename}_$num\">".$num."</a> ";
}
// exit();
}
}
}
closedir($handle);
I am having two problems. Firstly, there seems to be a problem with the loop, since I get a fatal error (timeout) after 60 seconds - why does the script try to execute itself after all files have been returned? It's stopping the rest of my page from loading... very serious problem.
Secondly, how do I sort the results of the script? It works perfectly but, assuming there are three files to link to, instead of returning 1 2 3, I get 1 3 2. Is there a function to fix this?
Thanks in advance!!
Matt

Posted: Sat Mar 11, 2006 12:59 pm
by feyd
you've got an infinite loop because you're storing the result from
readdir() into an array.
As for sorting the results, store the files/numbers into an array then use
sort()
Alternately, you could use
glob() which will both sort and filter the list (partly) for you.
Posted: Sat Mar 11, 2006 1:16 pm
by mattcooper
I see. I'll attempt to alter the code and will post it back here. Haven't written a script like this before, so it may take some time to figure it out! Thnaks very much...
Posted: Sat Mar 11, 2006 2:02 pm
by mattcooper
OK, I've created an array for the results to sort... but no luck, must be doing it wrong. As for the loop, I don't really know how else to do this... can you give me a hint?
Here's the code as it is now...
Code: Select all
if ($handle = opendir('includes')) {
while (true == ($file = array(readdir($handle)))) {
foreach($file as $num){
//strip off the .php extension
$stripped=substr($num, 0, -4);
//echo "$stripped\n";
$pagename=$_GET['pagename'];
if(eregi($pagename,$stripped)){
$start=strlen($stripped)-2;
$num=substr($stripped, -1, $start);
$numarray=array($num);
glob($numarray);
foreach ($numarray as $num){
if(is_numeric($num)){
echo "<a href=\"$PHP_SELF?pagename=$pagename{$num}\">".$num."</a> ";
}
else{
echo "<a href=\"$PHP_SELF?pagename=$pagename\">1</a> ";
}
}
// exit();
}
}
}
closedir($handle);
}
Thanks again...
Posted: Sat Mar 11, 2006 2:15 pm
by feyd
Code: Select all
foreach(glob('includes' . DIRECTORY_SEPARATOR . basename($_GET['pagenam']) . '*.php') as $file)
{
var_dump($file);
}
Posted: Sat Mar 11, 2006 2:42 pm
by mattcooper
Nope, I'm lost! Took me ages to figure out just how to achieve what you see above, can't figure out how to drop this bit of code in - every way I try it, I get errors...
Bugger. Thanks anyway, fella. I'm sure I'll get there eventually!
Posted: Sat Mar 11, 2006 2:46 pm
by feyd
try my code alone.
Posted: Sun Mar 12, 2006 6:00 am
by mattcooper
sorry I didn't get back to you last night. I'm having trouble with my host at the moment, but hopefully my webspace will be accessible again shortly/ I'm going to have a crack at testing this on my local machine...
I'll let you know the results as soon as I have had a play!
Cheers Feyd.
Posted: Sun Mar 12, 2006 6:13 am
by mattcooper
feyd wrote:try my code alone.
The only way I can get the code to run in any way is with the following:
Code: Select all
if ($handle = opendir('http://localhost/')) {
while (false !== ($file = readdir($handle))) {
foreach(glob('includes' . DIRECTORY_SEPARATOR . basename($_GET['pagename']) . '*.php') as $file)
{
var_dump($file);
}
But this just returns an error because I don't think I have PHP set up properly on my local machine. Here's the error:
Warning: opendir(
http://localhost/) [function.opendir]: failed to open dir: not implemented in c:\program files\apache group\Apache\htdocs\read_dir.php on line 7
Will you sort this code for me for a small fee, as I feel embarrassed to ask for your free help again! I can pay you with PayPal...
Posted: Sun Mar 12, 2006 9:09 am
by feyd
opendir() cannot open a URL. Only local file system paths are supported.
The code I supplied should not be used with any other code except the form (or whatever) is requesting with pagename=something.
Posted: Sun Mar 12, 2006 1:39 pm
by mattcooper
Thank you Feyd, with the help of your code this little function now works perfectly. Here is the final version, for anyone that may be able to benefit from it...
Code: Select all
//Function to look in the includes directory for includes that have more than one page to view
//If so, a link to each will be provided in the format 1 2 3... etc
//function multiple_pages(){
foreach(glob('includes' . DIRECTORY_SEPARATOR . basename($_GET['pagename']) . '*.php') as $file)
{
$num=$file;
$stripped=substr($num, 0, -4);
//echo "$stripped\n";
$pagename=$_GET['pagename'];
if(eregi($pagename,$stripped)){
$start=strlen($stripped)-2;
$num=substr($stripped, -1, $start);
$numarray=array($num);
sort($numarray);
foreach ($numarray as $num){
if(is_numeric($num)){
echo "<a href=\"$PHP_SELF?pagename={$pagename}_$num\">".$num."</a> ";
}
else{
echo "<a href=\"$PHP_SELF?pagename=$pagename\">1</a> ";
}
}
}
}
// End function
//}
A final question about error reporting... if this is set to the highest level as default by my host (and it is!), thereby returning notices I wouldn't see with my other host - where error reporting seems to be slightly more selective - what can I do to stop the error notice when I use "$PHP_SELF". At present, I'm being warned that it's an undefined constant, and it's a pain.
Thanks again!!
Posted: Sun Mar 12, 2006 1:46 pm
by feyd
$_SERVER['PHP_SELF'] ? That has the possibility of not being present either, so it may vary from server to server. If you get errors with PHP_SELF, check what's in $_SERVER as there may be other entries that are the same, or similar.