View Full Version : PHP query
Gizmo
02-18-2002, 06:21 AM
Hello
I have got a simple bit of code to list all the files in a directory (see below) and I was wondering a couple of things. The files in the directory are created by another programme and are labelled "feb05.htm" "jan06.htm" "aug15.htm" etc.:
1) The script sorts these alphabetically. How can I sort them so they are in calendar order? I.e. Jan then Feb etc.
2) How can I get rid of the .htm extension when the files are listed? I.e. just Jan06 etc.
3) When the list is produced, is it possible to change Jan06 to say January06 (by splitting the Jan and the 06 into two and then altering the Jan using an IF statement) or am I just being absurd?
<?
$p = "results";
$d = opendir($p);
$f = readdir($d);
while($f !== false)
if($f <> "." and $f <> ".." and $f <> "index.htm") {echo(" <a href=results/$f>$f</a> ");};
$f = readdir($d) ;
closedir($d);
?>
any help would be greatly appreciated
Ross
toosweet4u
02-18-2002, 11:48 PM
I don't know about the calendar order stuff, but you can get rid of the .htm extension using the explode function.
<?
explode(".htm", $f);
?>
Gizmo
02-19-2002, 01:52 AM
Thanks for the help toosweet4u
Where would I put that in the above code though?
cheers
Gizmo
02-26-2002, 11:46 PM
Hi again,
Could someone possibly have a look at:
HKCC Bridge Results (http://hkccbridge.rossharding.com)
It's the table down the bottom that is giving me grief.
1) There are 3 blank cells. These are the ".", "..", and something else (directory listings) from the above code. I thought it would ignore them, but instead it creates a blank cell for each. Can I get rid of these???
2) How would I get rid of the .htm ? toosweet mentioned it, but I can't see how to fit that in to what I have already
3) Is there any way to order the files that are displayed? e.g. jan, then feb etc.
4) Is there (probably...) a better way of doing all this?
Sorry for the load. It's a really simple process that I just can't seem to grasp.
thanks for any help
Ross :)
top5mov
02-27-2002, 11:49 AM
start with this much, give it a try. it should do all the string replacements you asked for. sorting by date is more difficult, i'll have to work on that for a bit.
<?php
$p = "results";
$d = opendir($p);
$f = readdir($d);
while($f !== false)
{
if($f != "." and $f != ".." and $f != "index.htm")
{
$prettyname = $f;
eregi_replace(".htm", "", $prettyname);
eregi_replace("jan", "January ", $prettyname);
eregi_replace("feb", "February ", $prettyname);
eregi_replace("mar", "March ", $prettyname);
eregi_replace("apr", "April ", $prettyname);
eregi_replace("may", "May ", $prettyname);
eregi_replace("jun", "June ", $prettyname);
eregi_replace("jul", "July ", $prettyname);
eregi_replace("aug", "August ", $prettyname);
eregi_replace("sep", "September ", $prettyname);
eregi_replace("oct", "October ", $prettyname);
eregi_replace("nov", "November ", $prettyname);
eregi_replace("dec", "December ", $prettyname);
echo(" <a href='results/$f'>$prettyname</a> ");
}
$f = readdir($d) ;
}
closedir($d);
?>
Gizmo
02-27-2002, 08:54 PM
Thanks top5mov, I will give it a try tonight. I THINK I may have solved the blank cell problem, so that just leaves the ordering by date. I thought the server would order them for me (i.e. by the date the file was uploaded), but no such luck
Gizmo
02-28-2002, 06:43 AM
no luck top5mov... it just displayed it the same as usual
http://hkccbridge.rossharding.com/indextrial.php
is it anything to do with the fact that it cannot find, say, 'jan' as it is 'jan' with '28' attached onto the end?
NOPE, it's not that, just tried with 'jan.html' and still the same...
any ideas? thanks again for the help
I got rid of the extra cells though...
Gizmo
03-12-2002, 07:56 AM
Unfortunately, the code mentioned by top5mov didn't work for me somehow. If anyone is curious, I ended up doing the following:
$p = "results";
$d = opendir($p);
$f = readdir($d);
while($f !== false)
{
$prettyname = $f;
$ending = eregi_replace(".htm", "", $prettyname);
if($f != "." and $f != ".." and $f != "index.htm") {
echo("<td bgColor=white valign='center' align='center'>"); echo(" <a href=results/$f>$ending</a> ");};
$f = readdir($d) ;
}
closedir($d);
i.e. I only did 1 of the two things. I settled for removing the .htm instead of renaming jan etc. to January etc. And even then I only managed to do it by assigning the eregi_replace to a variable ($ending). Don't know why...
I also tried:
$p = "results";
$d = opendir($p);
$f = readdir($d);
while($f !== false)
{
if($col ==1) {
echo("<tr>");
}
$prettyname = $f;
$start = eregi_replace("jan", "January ", $prettyname); }
$start = eregi_replace("feb", "February ", $prettyname);
$start = eregi_replace("mar", "March ", $prettyname);
$start = eregi_replace("apr", "April ", $prettyname);
$start = eregi_replace("may", "May ", $prettyname);
$start = eregi_replace("jun", "June ", $prettyname);
$start = eregi_replace("jul", "July ", $prettyname);
$start = eregi_replace("aug", "August ", $prettyname);
$start = eregi_replace("sep", "September ", $prettyname);
$start = eregi_replace("oct", "October ", $prettyname);
$start = eregi_replace("dec", "December ", $prettyname);
$ending = eregi_replace(".htm", "", $prettyname);
if($f != "." and $f != ".." and $f != "index.htm") {
echo("<td bgColor=white valign='center' align='center'>"); echo(" <a href=results/$f>$start$ending</a> ");};
$f = readdir($d) ;
}
closedir($d);
using $start and $ending to try and display both parts, but this only displayed EVERYTHING as "February" for some reason. No .htm , no number... weird
top5mov
03-12-2002, 09:04 AM
you're right, i forgot to assign the results of the eregi_replace back to the string, so it was just being thrown away each time.
put "$prettyname = " before all the eregi_replace lines and it should be fine. you almost had it right with the second snippet you posted, but the reason it didn't work is because you've got a stray "}". look:
while($f !== false)
{
if($col ==1) {
echo("<tr>");
}
$prettyname = $f;
$start = eregi_replace("jan", "January ", $prettyname); }
$start = eregi_replace("feb", "February ", $prettyname);
...
...so your while loop is ending right after the replacement for january. you really need to write your code with strict rules of proper indentation to help you catch errors like that. perhaps you did so originally and it just didn't carry over into the forums.
anyways, here's what it should look like:
<?php
$p = "results";
$d = opendir($p);
$f = readdir($d);
while($f !== false)
{
if($f != "." and $f != ".." and $f != "index.htm")
{
$prettyname = $f;
$prettyname = eregi_replace(".htm", "", $prettyname);
$prettyname = eregi_replace("jan", "January ", $prettyname);
$prettyname = eregi_replace("feb", "February ", $prettyname);
$prettyname = eregi_replace("mar", "March ", $prettyname);
$prettyname = eregi_replace("apr", "April ", $prettyname);
$prettyname = eregi_replace("may", "May ", $prettyname);
$prettyname = eregi_replace("jun", "June ", $prettyname);
$prettyname = eregi_replace("jul", "July ", $prettyname);
$prettyname = eregi_replace("aug", "August ", $prettyname);
$prettyname = eregi_replace("sep", "September ", $prettyname);
$prettyname = eregi_replace("oct", "October ", $prettyname);
$prettyname = eregi_replace("nov", "November ", $prettyname);
$prettyname = eregi_replace("dec", "December ", $prettyname);
echo(" <a href='$p/$f'>$prettyname</a> ");
}
$f = readdir($d) ;
}
closedir($d);
?>
Gizmo
03-12-2002, 09:39 AM
:D Brilliant!
Thanks top5mov. You are right about the layout of the code. I have a habit of beig a bit sloppy and just wanting to get it done quickly. Did not pick up on the '{'
Now I've just got to figure out how to put it all in a table, as I originally had it.
Should be OK though, if I get my 'order' the right way round
Thanks again!
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.