crazy sorting question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

crazy sorting question

Post by wesnoel »

OK I have a few .csv files in a a directory called files.

I want to be able to list all of them which I can already do. The problem I'm having is figuring out how to sort them based on what information is in the file.

I split all of the contents up by using

$var1 = $split[1];
$var2 = $split[2];
$var3 = $split[3];

I would like to sort the files by $var1. $var1 will be 1 of three things. Closed, Pending, or Open. I would like all of the closed to be at the end of the file list and all of the open to be at the beginning of the file list leaving all of the pending in the middle.

So when the file list is read I loop through each file in the dir. and pull out only the info I need i.e client name, account number, and status. Status being what I want to sort by. I can do everything up to this point. It is the sorting I have no clue how to do, but I'm sure it can be done.

Anyone know how? Or can you point me in the right direction?

TIA Guys...

Wes/
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Here's an example of the sort of thing you could use.....

Code: Select all

$arr1 = array("Bob","Andy","Jemma","Rodney"); // people
$arr2 = array("12","17","83","11"); // their ages
$arr3 = array("Red","Blue","Green","Yellow"); // their fav colours

// Sort out $arr1 so that the names are in alphabetical order and then
// shift the other arrays so that they match.

$arr1 = natsort($arr1);

foreach($arr1 as $key => $value)
{
     $tempArr2[] = $arr2[$key];
     $tempArr3[] = $arr3[$key];
}

$arr2 = $tempArr2;
$arr3 = $tempArr3;

unset($tempArr2);
unset($tempArr3);

// $arr2 and $arr3 should now be rebuilt in the same order as $arr1
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

I would create an array of each record you are pulling back, and add each record to a seperate array by status:

Code: Select all

<?php
for <each row>
{
   $row = split(',', $rowin);

   switch ($row[0])
   {
      case 'Closed':
         $closed[] = $row;
         break;

      case 'Pending':
         $pending[] = $row;
         break;

      default:
         $open[] = $row;
         break;
   }
}
   // now you have all records of each status in it's own array with sub-elements

   print_r ($pending[0]); // should return an array with status, name and account number.
?>
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

OK this is the code I'm using to pull the file list.

Code: Select all

<?php
//Directory to look in
$current_dir = "files";


//Draws a nice looking table
echo "<br><table width="95%" border="0" cellspacing="0" cellpadding="1">";


//Split line color start
$colors = array("#DCDCDC","#FFFFFF"); 
$curcolor = 0; 



//This starts the looping through the directory
$dir = opendir($current_dir);
while($file = readdir($dir))
    {
  if(($file=='.')||($file=='..')) continue;
  
  
//this pulls out the info I need from the csv files.  
  $mytext =file("files/$file");
 
foreach ($mytext as $myarray)
{ 

     $split = explode(",", $myarray); 
 	     $businessname =$split[5];
 	     $cid =$split[1];
	     $date     = $split[2];
	     $status     = $split[28];
	     $sticker     = $split[30];


  
  //This sets the status color	     
switch ($status) {
    case OPEN:
        $color= "#2E8B57";//green
        break;
    case PENDING:
        $color= "#003366";//blueish
        break;
    case CLOSED:
        $color= "#DC143C";//red
        break;
    default:
        $color= "#003366";//blueish
 
 }
  
  }
  

 //Write the file listing
echo "<tr  width="600" bgcolor=$colors[$curcolor]> <td width="500" ><font size="2" face="Verdana"><b>$cid</b> $businessname</td><td width="130" align="center"><font size="1" face="Verdana"><a href="javascript:popUp2('view.php?note=files/$file')">VIEW</a></font></td><td NOWRAP width="100" align="center"><font size="1" face="Verdana"><a href="javascript:popUp('excel.php?note=files/$file')">SPREAD SHEET</a></font></td><td width="130" align="center"><font size="1" face="Verdana"><a href="list.php?del=files/$file" target="_self" onclick="return confirmSubmit('Are you sure you want to DELETE this record?');">DELETE</a></td><td width="150" align="center">  </td><td width="150" align="center"><font size="1" face="Arial"  color="$color"><b>$status</b></font></td><td align="center" width="300" class="date">$date</td><td></td></tr>";

//Split line color finnish		
$curcolor++; 
 if( $curcolor >= count($colors) ) $curcolor =0; 
}

//Close the directory
closedir($dir);

?>

How do I work your code into mine?
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

Well, I would do it like follows, although you might do it a little different:

Code: Select all

$colors[OPEN] = "#2E8B57";
$colors[PENDING] = "#003366";
$colors[CLOSED] = "#DC143C";

foreach ($mytext as $myarray) 
{ 

     $split = explode(",", $myarray); 
     $businessname = $split[5]; 
     $cid       = $split[1]; 
     $date     = $split[2]; 
     $status     = $split[28]; 
     $sticker     = $split[30]; 
     $color        = $colors[$status];

     $row = array($businessname, $cid, $date, $sticker, $color);
  
  //This sets the status color         
switch ($status) { 
    case OPEN: 
        $open[] = $row;
        break; 
    case PENDING: 
        $pending[] = $row;
        break; 
    case CLOSED: 
        $closed[] = $row;
        break; 
    default: 
        $unknown[] = $row; // not sure exactly how to handle this line color= "#003366";//blueish 

}
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

Oh, I'm not sure that shows me how to impliment you code into mine.

Although I do like how simple and clean you make it.

This is how my code lists the files:

Image

I just want to group the list with all of the closed at the bottom, all of the pending in the middle and all of the open at the top.

I hope this clarifies.

Thanks everone,

Wes/[/img]
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

Well, you would basically take your HTML code and loop through each array of items and print them out. I am not going to write all your code for you, you gotta do some of it to get the credit ;).
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

WooooHoooo!!!!!

Ok this is what I got. Thanks everyone for the help I got it all to work.
Use this code freely if you want to...Me giving back!

Code: Select all

<?php
// Delete function

$del = $_GET['del'];
if(isset($del)){
unlink("$del");
}

$file = $_GET['file'];




$cid1 = array();
$date1 = array();
$businessname1 = array();
$status1 = array();
$files = array();
$fileNames = array();



// Root directory to the files below 
$current_dir = "files";

$dir = opendir($current_dir);
while($file = readdir($dir)){
  if(($file=='.')||($file=='..')) continue;

	  //split color start
	$colors = array("#FFFFFF","F5F5F5"); 
	$curcolor = 0; 
	$fileName = "$current_dir/$file";
	$lines = file( $fileName );

	foreach( $lines AS $id => $line ) { // all files
	    //echo "$file<br>";
	    $parts = explode( ",", $line );

	    
	$cid1[] = $parts[1];
	$date1[] = $parts[2];
	$businessname1[] = $parts[5];
	$status1[] = $parts[28];
	$fileNames[] = $fileName;
	
	
	}// $lines foreach

	
	
	 
	

	    



}// While

isset($_GET['orderby']) ? $orderby = $_GET['orderby'] : $orderby = "date1";
echo "<center><table width="98%" border="0" cellspacing="0" cellpadding="1">";



    echo "<center><tr  width="100%" bgcolor="D3D3D3">";
    echo "<td bgcolor="#D3D3D3"><b><a href="".$_SERVER['PHP_SELF']."?orderby=cid1">Client ID</a></b></td>";
    echo "<td colspan="1" bgcolor="#D3D3D3"><b><a href="".$_SERVER['PHP_SELF']."?orderby=businessname1">Business Name</a></b></td>";
    echo "<td colspan="3" bgcolor="#D3D3D3"><B>File Details</B></td>";
    echo "<td bgcolor="#D3D3D3"><b><a href="".$_SERVER['PHP_SELF']."?orderby=status1">Status</a></b></td>";
    echo "<td bgcolor="#D3D3D3" width="30%"></td>";
    echo "</tr></center>";
    
   
   
	
    

switch( $orderby ) {
    case "cid1":
        array_multisort( $cid1, SORT_ASC, SORT_STRING, $status1, $businessname1, $date1, $fileNames);
        break;
    case "status1":
        array_multisort( $status1 , SORT_DESC, SORT_STRING,  $cid1, $fileNames );
        break;
  case "businessname1":
        sort( $businessname1, SORT_DESC );
        break;
        
      	
          
   		 } 
     
     
       
   

foreach( $date1 AS $id => $value ) {

 
 switch( $status1 ) {
       case "OPEN":
       	$color = "#2E8B57";
       	break;
       	case "PENDING":
		$color = "#003366";
		break;
		case "CLOSED":
		$color = "#DC143C";
		break;
		default:
		$color = "#CD5C5C";	

}
     
	
echo "<tr  width="100%" bgcolor=$colors[$curcolor]>";
echo "<td width="80"><font size="2" face="Verdana"><b>$cid1[$id]</b></font></td>";
echo "<td width="200"><font size="1" face="Verdana">$businessname1[$id]</td></font>";
echo "<td width="60"><font size="1" face="Verdana"><a href="javascript:popUp2('view.php?note=$fileNames[$id]')">VIEW</a></font></td>";
echo "<td width="110"><font size="1" face="Verdana"><a href="javascript:popUp('excel.php?note=$fileNames[$id]')">SPREAD SHEET</a></font></td>";
echo "<td width="150"><font size="1" face="Verdana"><a href="list.php?del=$fileNames[$id]" target="_self" onclick="return confirmSubmit('Are you sure you want to DELETE this record?');">DELETE</a></td><td><font size="1" face="Arial"  color="$color"><b>".$status1[$id]."</b></font></td><td class="date">".$date1[id]."</td><td></td></tr></center>";


		


    //split color finnish		
$curcolor++; 
 if( $curcolor >= count($colors) ) $curcolor =0; 

  

}


echo "</table>";

closedir($dir);



?>


Thanks again everyone!

Wes/
Post Reply