Forum Moderators: coopster

Message Too Old, No Replies

PHP Code Help

What part is controlling the display order?

         

Matoca

8:26 pm on May 18, 2010 (gmt 0)

10+ Year Member



I am new to this forum. I inherited a website 6 months ago with bits of php code on most pages. I have been studying lynda.com, W3C and books and I am beginning to understand more, but at a advanced beginner level. I use Dreamweaver CS4 and have been able to work around any code I don't understand (mostly because it is still working!). Any time it gets in my way, I comment it out and do the same thing with html.

I would like to be able to take some of the more complex code and really understand what it is doing. I can pick out php terms and I understand them quite well, but sometimes putting it all together doesn't make it very clear. I can figure out the general idea of what is happening, but not the specifics. How long does this take someone who is self study to learn?

Right now my specific problem I need help with from this forum is that I have a php code script that basically looks at all the files in a directory, figures out how big they are, assigns them MB, KB etc using php math and then echoes all the file names, along with these file types and sizes (ex: This Is The File Name (PDF 47 KB)) on the page. This much of the php and how it works, I sort of understand.

I cannot understand how the php defines how files are ordered when placed on the page. I want more control over how they are displayed (whether alphabetical, by date going in or some other criteria). It would be helpful if I at least understood how this is happening now!

They just seem to go into the directory and pop up randomly in the list. This week I put in a new file into this directory and was surprised to see it go to near the bottom of the list, along with other files beginning with "b" (but at the top of the "b's"). I retitled it with a word beginning with "d" as an experiment and it did move it to another location in the list but not into any kind of alphabetical order that makes sense. The way I am reading this code is that it somehow puts it in reverse sort, with newer issues above issue 1. But the newest file is not going at the top or bottom of the list either! Nor does it seems to have anything to do with file size!

If someone could point to the part of this code that is controlling this, I might be able to break it apart and understand it better.
Thank you.

<?php

function get_list() {
$subdir = ".";
$articledir = getcwd() . "/" . $subdir;



$arg = getenv("QUERY_STRING");


$dir = opendir($articledir);

while ($file=readdir($dir)) {

if ((substr($file,0,1) != ".") && ($file!="index.php") &&
($file!="livemeeting.wmv")) {
$filelist[] = $file;
}
}

rsort ($filelist); // Reverse Sort (newer issues will be above issue 1)

foreach ($filelist as $file){
$len = strlen($file) - 4;
$title = substr($file, 0, $len);
$ext = mb_strtoupper (substr($file,$len+1));
$fullfile = $articledir . "/" . $file;
$size = filesize($fullfile);
$hsize = human_file_size ($size);
echo "<li><a href=\"$subdir/$file\">$title</a> ($ext, $hsize)</li>\n";
}
}

function human_file_size($size) {
$filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return round($size/pow(1024, ($i = floor(log($size, 1024)))), 1) .
$filesizename[$i];
}

?>

Readie

8:38 pm on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How long does this take someone who is self study to learn?

I started learning early December 2009. I'm now being trialled for a full time job as a PHP developer. I managed this by going out of my way to solve the problems people have on this forum, especially the one's I myself didn't at the time know how to solve, but with some creative Googling and searching of www.php.net I managed to develop workable solutions.

As for your problem, the sort here is this line:

rsort ($filelist);

And as far as I can tell it's sorting by file name alphabetically.

To get it to sort by something else would involve changing a lot, and I'm a bit strained for time right now, but if someone else hasn't helped you with this and you havn't solved it yourself by tomorrow evening we'll see about some code.

Matthew1980

8:56 pm on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Matoca,

Welcome to the forum!

Check this out: [uk2.php.net ]

As Readie points out:-

rsort($filelist);

sorts the chosen array in reverse order (last value to first I think..)

But if you read up on some of the other functions for array sorting from the link I have given you should be able to see a difference in how things will be listed, my own favourite is shuffle(); as It would give you a different 'random' result each time the script is run! Cool!

With regards to learning, everyone is different, just depends on how much time you are willing to spend on reading and understanding and of course trial and error!

Have fun anyway!

Cheers,
MRb

Matoca

4:09 am on May 19, 2010 (gmt 0)

10+ Year Member



Thank you for the quick responses! I looked at the uk2.php.net and was delighted to see so many options. Maybe something here can help me.

The items that are going into this directory are pdf's for a company that hosts a private site for investors. So stuff like forms, reports etc. I want some semblance of organization (so alphabetical is good) but dumping them all in there like that tends to hide new additions to the group. I know the company wants to draw attention to newer items and doesn't understand why they end up buried in the list.

Up until now, I have had no clue what is happening and was reluctant to mess it up. So I guess now my job is to figure out how to do both of these requests together: by date into the server (but that has it's own problems doesn't it?) and then alphabetically. Or maybe just number them? Wonder if the sort sees numbers as an alphabetical order? It would be a simple matter to renumber each item when a new pdf is put in. I wouldn't have to echo the number, right? However, it would be part of the file name, no getting around that.... So maybe that won't work after all.

I wish I had all day to play with this stuff but I have a full time job and this would be my second part time job. I fit in the code exploration when I am not scrambling to get something done for them on the site. I am surprised how much I like this part of web design. I was very excited when I figured out how to add a php footer with a copyright that changes the year for me!

rocknbil

7:29 pm on May 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, the **best** way is to store these in a DB, I don't know how you'd sort by date THEN file name otherwise. But this gets them by date if you're on 'nix.

<?php
header("Content-type:text/html");
$target="/full/server/path/to/directory";
exec("ls $target -lt",$filelist);
foreach ($filelist as $file) { echo "$file<br>\n"; }
?>

ls documentation [opengroup.org]

Readie

12:22 pm on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> Rock

An Idea I had for sorting by date, and then by name is to build as associative array, with the key name being the primary sort, and the value being the actual file.

You could then do:

arsort($some_array);
krsort($some_array);
foreach($some_array as $key => $val) {
// Build the list
}

Matoca

4:04 am on May 26, 2010 (gmt 0)

10+ Year Member



Thank you for your replies. I have been playing with some of your suggestions and I like the idea of an associative array. There aren't a lot of files, so this would work quite well. I can alter the order of the array at any time fairly easily.

I also like the sort by date going into the server THEN file name. This would be more in keeping with what I wanted to do: add new files and have the new ones that begin with the same word as older files pop up at the top (or in the rsort case, the bottom) of the alphabetical order. (ie "yearly business report 2010", "yearly business report 2009" etc). I have discovered that rsort puts numbers, alphabetically, at the bottom of the list, so thus "2009 business report" is going to the bottom. Changing it to "yearly...2009" moves it to near the top of the rsort list.

It's interesting how I can study php, read a description of how an associative array works, see examples, but not fully grasp the application of it until I see it put into practical application.

Readie

11:24 am on May 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



asort()
arsort()

arsort() is descending

You need to use asort or arsort, rather than just sort() and rsort() when dealing with associative arrays, otherwise the array index is lost and it just becomes numeric again.