Order of CSV rows

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
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Order of CSV rows

Post by salto »

Could anyone help please? Bear with me, still a beginner.
I would like to echo data of rows from a CSV file in the order I assign.
With the code below all data is echoed properly, but the order of the data is ascending (first row 12, followed by rows 16, 24).
Is there any way to echo the data on the page in the order I prefer (row 16, row 24, row 12) ?

Code: Select all

<?php
$row = 0; 
while (($data = fgetcsv($handle, 10000, "	")) !== FALSE) { 
   $num = count($data); 
   $row++;  
   if ($row == 16 || $row == 24 ||$row==12) 
   { 
       echo "<p>" . $data[11] . $data[4]  .  $data[3]  .  "</p>"; 
   }   
} 
fclose($handle); 
?>
No matter what I tried, nothing worked the way I wanted. Thanks very much for your help!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I think what you are looking for is ksort() function :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

because you are wanting to do in neither ascending nor descending order, you have to resort to custom sorting. usort()
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Post by salto »

Thanks for both your replies Jenk and feyd and pointing me to the functions. I have tried the function usort() but I would not know how to create the necessary 'user-supplied comparison function' with this set of values ($row16,$row24,$row12). Am afraid this is way over my head, unfortunately!
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Code: Select all

function cmp($a, $b) {
  if ($a == $b) return 0;
  $goodsort = array(1=>0, 3=>1, 2=>2, 0=>3, 4=>4);
  return ($goodsort[$a] < $goodsort[$b]) ? -1 : 1;
}
$data = range(0,4);
usort($data,"cmp");
print_r($data);
/*
Array
(
    [0] => 1
    [1] => 3
    [2] => 2
    [3] => 0
    [4] => 4
)
*/
Wow, that was harder than I thought... Maybe someone else can come up with a better way to phrase it.

Anyway, just load the $goodsort keys with the order you want and increment the value of each key by one each time. Basically, the key and value are reversed. O.o
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Post by salto »

Hi Skara, thanks for your reply! I have tried your code and several modifications, but it does not give the desired result.
From what I understand the function fgetcsv() 'stores' the $rows in an array. I think the rows should be 'usorted' first to echo them in the proper order. $data are the cells in the rows.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

I don't see where you have a $rows array anywhere, so I'm kinda lost there.

Give me the results of print_r($data);

My code should work if it's set up properly. You may need uksort() instead of usort(), though.
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Post by salto »

Hi Skara, results of print_r($data) are

Code: Select all

Array ( [0] => 1 [1] => 3 [2] => 2 [3] => 0 [4] => 4 )
$row is in my code in the first message of this thread. These are the rows of the CSV.
$data are the 'cells' in $row

What I try to accomplish is to show $row16 , $row 24, $row12 in this exact order of rows on the page. Not ascending, descending or any other order, just as is.

As far as I understand $row is an array in itself already.
Hope I made myself clear....
Post Reply