Forum Moderators: coopster

Message Too Old, No Replies

Help me for sorting

         

anhnguyen

5:34 am on Sep 16, 2003 (gmt 0)

10+ Year Member



my program is working properly..but i dont know how to sort
following average rank
thank u very much
<html>
<head>
<title> Administrator Page</title>
</head>
<body>
<h1 align="center"> All Reviews Infomation</h1>
<?php>

$review = file("review.txt");
$number_of_review = count($review);

if ($number_of_review ==0)
{
echo 'No review';
}
echo "<table border=1>\n";
echo '<tr><th bgcolor="#CCCCFF">Full Name</th>
<th bgcolor="#CCCCFF">Gender</th>
<th bgcolor="#CCCCFF">Group Age</th>
<th bgcolor="#CCCCFF">Movie Magic</th>
<th bgcolor="#CCCCFF">Acting Picture</th>
<th bgcolor="#CCCCFF">Sound Quality</th>
<th bgcolor="#CCCCFF">Special Effect</th>
<th bgcolor="#CCCCFF">Average Rank</th>
</tr>';

for ($i=0; $i<$number_of_review;$i++)
{
$out = explode("\t",$review[$i]);


echo "<tr><td> $out[0]</td>
<td> $out[1]</td>
<td> $out[2]</td>
<td> $out[3]</td>
<td> $out[4]</td>
<td> $out[5]</td>
<td> $out[6]</td>
<td> $out[7]</td>

</tr>";
}

echo "</table>";

?>

ergophobe

6:43 am on Sep 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome Ahn,

Can you put your reviews in a database? That would making sorting so easy just using ORDER BY. Otherwise, I think you will need to put all reviews in an array then sort the array.

Tom

anhnguyen

8:02 am on Sep 16, 2003 (gmt 0)

10+ Year Member



can i ask ..how to put all review in array and sort them..

thanks

justageek

11:07 am on Sep 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



sort($out);

anhnguyen

4:08 am on Sep 17, 2003 (gmt 0)

10+ Year Member



nope ..it is not work for sort average rank....anyone help?
in review.txt like this

peter Male under 16 2 4 4 6 3.5
Tim Male under 16 2 4 4 6 2.5
Tim Male under 16 2 4 4 6 4.5

if i sort avereage rank..it display like this

Tim Male under 16 2 4 4 6 2.5
peter Male under 16 2 4 4 6 3.5
Tim Male under 16 2 4 4 6 4.5

justageek

11:54 am on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I didn't know you had multiple values. Make the value you want to sort is the first array value and do:

array_multisort($out,SORT_DESC);

or

array_multisort($out,DESC);

JAG

anhnguyen

2:21 pm on Sep 17, 2003 (gmt 0)

10+ Year Member



first, i want to thank u for reply

if array_multisort($out,DESC); ..it will sort all value
but in my case only sort in avarage rank column (out[7])

something like this:
------------------------average rank
tim male under18 3 3 3 3 3
tim male under18 2 2 2 2 2
tim male under18 4 4 4 4 4

but after sort i will be
-------------------- ----average rank
tim male under18 2 2 2 2 2
tim male under18 3 3 3 3 3
tim male under18 4 4 4 4 4

justageek

3:31 pm on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure I understand the question. Array_multisort will keep your keys intact which I assume is what you would want?

JAG

ergophobe

4:21 pm on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Check the online annotated manual, which discusses exactly your situation at length.

[us2.php.net...]

BTW JAG, his problem is that he doesn't want to sort on $out[7] as he says, but rather on $out[][7], which would work great if multisort allowed such arguments.

One simple solution would be to put the average review as the first element of the array, then it's easy.

$ar = array(array(3.5, "Peter", "Male", "under 16", 2, 4, 4, 6),
array(2.5, "Tim", "Male", "under 16", 2, 4, 4, 6),
array(4.5, "Sam", "Male", "under 16", 2, 4, 4, 6));
array_multisort($ar);

print_r ($ar[0]);
print "</br>";
print_r ($ar[1]);
print "</br>";
print_r ($ar[2]);
print "</br>";

Output is

Array ( [0] => 2.5 [1] => Tim [2] => Male [3] => under 16 [4] => 2 [5] => 4 [6] => 4 [7] => 6 )
Array ( [0] => 3.5 [1] => Peter [2] => Male [3] => under 16 [4] => 2 [5] => 4 [6] => 4 [7] => 6 )
Array ( [0] => 4.5 [1] => Sam [2] => Male [3] => under 16 [4] => 2 [5] => 4 [6] => 4 [7] => 6 )

Tom

justageek

5:31 pm on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW JAG, his problem is that he doesn't want to sort on $out[7] as he says, but rather on $out[][7], which would work great if multisort allowed such arguments.

Yes. That's what I was trying to get him to do 2 posts ago ;-)

ergophobe

6:53 pm on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Yes. That's what I was trying to get him to do 2 posts ago ;-)

Oh sorry. I didn't see that post. In any case, it should be clear now.

Tom