SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: Sort by date via arrays.
-
May 3, 2009, 14:32 #1
- Join Date
- Feb 2001
- Location
- Missouri
- Posts
- 209
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sort by date via arrays.
Hey hey!
I'm having a bit of an issue sorting by date with arrays. For example...
I did some scouring around and found these dateAsc and dateDesc functions. It doesn't work at all.
PHP Code:function dateAsc($a, $b) {
return strcmp($a["date"], $b["date"]);
}
function dateDesc($a, $b) {
return strcmp($b["date"],$a["date"]);
}
while (false !== ($file = readdir($dir_handle))) { //loop through all the files in the path
if ($file == "." || $file == "..") {
continue;
} //ignore these
// these have to be htm files
if (substr_count($file, ".htm") > 0) {
// now we gotta make the files ledgible.
$arr_split_it = explode("__",$file);
$press_date[$count] = strtotime($arr_split_it[0]);
$press_title[$count] = $arr_split_it[1];
$press_list[$count] = "<li><a href=\"press.php?file=" . urlencode($file) . "\">" . str_replace("__"," ",str_replace(".htm","",stripslashes(urldecode($file)))) . "</a></li>";
$count++;
}
}
uasort($press_date, "dateDesc");
- 07-01-08 New Power Engineer
- 04-08-08 Data Network
- 04-08-09 Fiber to the Home
Basically this scripts reads files from a directory with the date in the file name, removes the double underscore and the .htm extension and displays it as a readable format on a web page.
All I need to be able to do now is sort them by the date descending. And no, databases are not an option here.
Any thoughts? Thanks!Sing in a band called Psychostick, Alfredo Afro.
-
May 3, 2009, 15:41 #2
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Storing and sorting the date, title and list in separate arrays won't work. Even if you sort the date array correctly, now the order of its values doesn't match the order of the values in the other arrays, and it becomes useless information.
Keep all the file information in a multidimensional array and sort that whole array by date.Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
May 3, 2009, 17:20 #3
- Join Date
- Feb 2001
- Location
- Missouri
- Posts
- 209
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hmmm. That's interesting. I'm not very familiar with multi-dimensional arrays.
Using my example above, how would you take that file information and place it into a multidimensional array?Sing in a band called Psychostick, Alfredo Afro.
-
May 3, 2009, 17:29 #4
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
In addition to your current arrays:
PHP Code:$data[$count]['date'] = strtotime($arr_split_it[0]);
$data[$count]['title'] = $arr_split_it[1];
$data[$count]['list'] = "<li><a href=\"press.php?file=" . urlencode($file) . "\">" . str_replace("__"," ",str_replace(".htm","",stripslashes(urldecode($file)))) . "</a></li>";
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
May 3, 2009, 18:42 #5
- Join Date
- Feb 2001
- Location
- Missouri
- Posts
- 209
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Cool I'll give it a try. Thanks!
Sing in a band called Psychostick, Alfredo Afro.
-
May 3, 2009, 20:36 #6
- Join Date
- Feb 2001
- Location
- Missouri
- Posts
- 209
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, it does work after converting the dates to unix timestamps and sorting them that way.
Thanks Dan!Sing in a band called Psychostick, Alfredo Afro.
Bookmarks