Page 1 of 1
Order of CSV rows
Posted: Tue Sep 27, 2005 10:22 am
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!
Posted: Tue Sep 27, 2005 10:24 am
by Jenk
I think what you are looking for is ksort() function

Posted: Tue Sep 27, 2005 10:27 am
by feyd
because you are wanting to do in neither ascending nor descending order, you have to resort to custom sorting.
usort()
Posted: Tue Sep 27, 2005 4:47 pm
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!
Posted: Tue Sep 27, 2005 8:03 pm
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
Posted: Wed Sep 28, 2005 2:05 am
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.
Posted: Wed Sep 28, 2005 11:19 am
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.
Posted: Wed Sep 28, 2005 5:50 pm
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....