Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-06-2009, 10:39 PM   PM User | #1
needsomehelp
New Coder

 
Join Date: Oct 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
needsomehelp is an unknown quantity at this point
sort array in item order when numbers go over 10

how do i sort the following file examples so they are in number order?

abc - 23-11-2009 - Agenda Item 1 - Name of item 1.pdf
abc - 23-11-2009 - Agenda Item 2 - Name of item 2.pdf
abc - 23-11-2009 - Agenda Item 3 - Name of item 3.pdf
abc - 23-11-2009 - Agenda Item 4 - Name of item 4.pdf
abc - 23-11-2009 - Agenda Item 5 - Name of item 5.pdf
abc - 23-11-2009 - Agenda Item 6 - Name of item 6.pdf
abc - 23-11-2009 - Agenda Item 7 - Name of item 7.pdf
abc - 23-11-2009 - Agenda Item 8 - Name of item 8.pdf
abc - 23-11-2009 - Agenda Item 9 - Name of item 9.pdf
abc - 23-11-2009 - Agenda Item 10 - Name of item 10.pdf
abc - 23-11-2009 - Agenda Item 11 - Name of item 11.pdf
abc - 23-11-2009 - Agenda Item 12 - Name of item 12.pdf

as it is showing as

abc - 23-11-2009 - Agenda Item 1 - Name of item 1.pdf
abc - 23-11-2009 - Agenda Item 10 - Name of item 10.pdf
abc - 23-11-2009 - Agenda Item 11 - Name of item 11.pdf
abc - 23-11-2009 - Agenda Item 12 - Name of item 12.pdf
abc - 23-11-2009 - Agenda Item 2 - Name of item 2.pdf
abc - 23-11-2009 - Agenda Item 3 - Name of item 3.pdf
abc - 23-11-2009 - Agenda Item 4 - Name of item 4.pdf
abc - 23-11-2009 - Agenda Item 5 - Name of item 5.pdf
abc - 23-11-2009 - Agenda Item 6 - Name of item 6.pdf
abc - 23-11-2009 - Agenda Item 7 - Name of item 7.pdf
abc - 23-11-2009 - Agenda Item 8 - Name of item 8.pdf
abc - 23-11-2009 - Agenda Item 9 - Name of item 9.pdf

i used the natsort but this did not sort the file names into order.

this my code with natsort..

Code:
<?
$files = glob('agendas/*Agenda Item*.pdf');
      $sortfile = array();
      foreach($files as $file){
         //Extract date
         if (preg_match('/(\d+)-(\d+)-(\d+)[^.]*?\.pdf/', $file, $reg)) {
            //add date to array (file as key)
            $sortfile[$file] = mktime(0,0,0,$reg[2],$reg[1],$reg[3]);
         }
      }
      natsort($sortfile); //Sort an array (the dates) and maintain index association (file names)
      $sortfile = array_keys($sortfile);
            foreach ($files as $key=>$value) {
            $datestring = strtotime(basename(substr($sortfile[$key], 14, 10),".pdf"));
            // $anchortext = date('j M Y',$datestring);
            $itemnumber = basename(substr($sortfile[$key], 39, 2),".pdf");
            $itemname = basename(substr($sortfile[$key], 43),".pdf");
            $url = date('dmY',$datestring);
            ?><li><a href="?a=<?=$url;?>"><? if (strlen(date('j',$datestring)) == 1) { ?>&nbsp;&nbsp;<? } ?><strong><?=$itemnumber."=".$itemname;?></strong></a><? if (strlen(date('j',$datestring)) == 1) { ?>&nbsp;&nbsp;<? }
            } ?></li>
needsomehelp is offline   Reply With Quote
Old 11-07-2009, 12:54 AM   PM User | #2
Fumigator
Master Coder

 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 5,405
Thanks: 32
Thanked 373 Times in 364 Posts
Fumigator is just really niceFumigator is just really niceFumigator is just really niceFumigator is just really niceFumigator is just really nice
natsort() works fine for this, but your array's values aren't the filenames, your array INDEXes are the filenames. I don't see a ksort() option to do a natural sort, so you'll have to use array_flip(), natsort(), then flip back.
__________________
Fumigator is offline   Reply With Quote
Old 11-07-2009, 12:03 PM   PM User | #3
needsomehelp
New Coder

 
Join Date: Oct 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
needsomehelp is an unknown quantity at this point
thanks for you reply but still not getting the files to show in number order.

i was not sure where i do the sort or flip and messed around with the code but could not get it in order

i even put a leading '0' in the file names and still it did not show in order.

can you see where i may be going wrong with my code?


Code:
<?
$files = glob('agendas/*Agenda Item*.pdf');
		$sortfile = array();
		foreach($files as $file){
			//Extract date
			if (preg_match('/(\d+)-(\d+)-(\d+)[^.]*?\.pdf/', $file, $reg)) {
				//add date to array (file as key)
				$sortfile[$file] = mktime(0,0,0,$reg[2],$reg[1],$reg[3]);
			}
		}
//		natsort($sortfile); //Sort an array (the dates) and maintain index association (file names)
//array_flip($sortfile);
		natsort($sortfile); //Sort an array (the dates) and maintain index association (file names)
array_flip($sortfile);
		$sortfile = array_keys($sortfile);
				foreach ($files as $key=>$value) {
				$datestring = strtotime(basename(substr($sortfile[$key], 14, 10),".pdf"));
				// $anchortext = date('j M Y',$datestring);
				$itemnumber = basename(substr($sortfile[$key], 39, 2),".pdf");
				$itemname = basename(substr($sortfile[$key], 43),".pdf");
				$url = date('dmY',$datestring);
				?><li><a href="?a=<?=$url.'&in='.$itemnumber;?>"><?
						if (strlen(date('j',$datestring)) == 1) { ?>&nbsp;&nbsp;<?
						}
				?><strong><?=$itemnumber."=".$itemname;?></strong></a><?
						if (strlen(date('j',$datestring)) == 1) { ?>&nbsp;&nbsp;<?
						}
				} ?></li>
needsomehelp is offline   Reply With Quote
Old 11-07-2009, 03:48 PM   PM User | #4
needsomehelp
New Coder

 
Join Date: Oct 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
needsomehelp is an unknown quantity at this point
Quote:
Originally Posted by Fumigator View Post
natsort() works fine for this, but your array's values aren't the filenames, your array INDEXes are the filenames. I don't see a ksort() option to do a natural sort, so you'll have to use array_flip(), natsort(), then flip back.
if i have this wrong how do i get it to be setup correctly

i tried to use the array_flip and that but not having much luck as yet playing around with the lines of code moving them around to see what it shows by outputting the data each array at a time.
needsomehelp is offline   Reply With Quote
Old 11-08-2009, 10:40 AM   PM User | #5
Fumigator
Master Coder

 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 5,405
Thanks: 32
Thanked 373 Times in 364 Posts
Fumigator is just really niceFumigator is just really niceFumigator is just really niceFumigator is just really niceFumigator is just really nice
You need to understand your code if you are to ever have a hope of getting it straight.

The array_flip switches each array element's key with its value. If that makes no sense to you then take a step back and study up on arrays, associative arrays, etc.
__________________
Fumigator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting 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

Forum Jump


All times are GMT +1. The time now is 02:43 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.