...

# php sort values help needed

se4b4ss
05-15-2004, 08:25 PM
using php, i have found the values of variable[number]. how do i find the 5 highest values in order?

ie

variable[109]=14
variable[222]=99
variable[10]=157
variable[43]=200
variable[57]=398
variable[20]=28
variable[34]=46
variable[6]=100

would result in

variable[57]=398
variable[43]=200
variable[10]=157
variable[6]=100
variable[222]=99

steve

litebearer
05-15-2004, 09:09 PM
sorts the variable then lists the top five

sort(\$varible);
\$i = 0;
for (\$i = 0; \$i < 5; \$i ++) {
echo \$variable[\$i] . "<br>";
}

missing-score
05-16-2004, 01:07 AM
Due to they way your array is setup, I would reccomend using using asort(), as this will keep all your array keys set how you want them... sort() will assign new keys starting at 0.

asort(\$var);
\$i = 0;
for (\$i=0;\$i<5;\$i++) {
echo \$var[\$i].'<br />';
}

se4b4ss
05-17-2004, 07:52 PM
missing-score,

asort(\$qualityaverage);

for (\$i=0;\$i<5;\$i++) {
echo \$i." ".\$qualityaverage[\$i].'<br />';
}

0
1
2 4.5
3
4

it so happens that \$qualityaverage[2] does equal 4.5, but it's not one of the top 5. i got those same results without using "asort". any ideas? am i overlooking something simple?

steve

se4b4ss
05-18-2004, 03:52 AM
i have been working with multidimensional arrays. hope to have something by tomorrow.

regards,
steve

se4b4ss
05-18-2004, 07:32 PM
ok, i solved the problem using multidimensional arrays. the implementation can be seen here. (http://www.texascontractorratings.com)

let me know what you think.

//BEGIN LIST THE TOP 5 RATED CONTRACTORS

\$sql = "SELECT ratings.company_id, quality, cost, name FROM contractors INNER JOIN ratings ON ratings.company_id=contractors.company_id";
\$rs=odbc_exec(\$conn,\$sql);
if (!\$rs)
{exit("Error in SQL");
}
while (odbc_fetch_row(\$rs))
\$contractor_id=odbc_result(\$rs,"company_id");
\$contractor_name=odbc_result(\$rs,"name");
\$quality=odbc_result(\$rs,"quality");
\$cost=odbc_result(\$rs,"cost");
for (\$counter=0; \$counter<=\$totalcontractors; \$counter++)
{if (\$toprated[\$counter]["contractor_name"]==\$contractor_name)
{\$toprated[\$counter]["number"]=\$toprated[\$counter]["number"]+1;
\$toprated[\$counter]["quality_average"]=(\$toprated[\$counter]["quality_average"]+\$quality)/\$toprated[\$counter]["number"];
\$toprated[\$counter]["cost_average"]=(\$toprated[\$counter]["cost_average"]+\$cost)/\$toprated[\$counter]["number"];
\$toprated[\$counter]["total_average"]=\$quality+\$cost;
}
}
{\$toprated[\$counter]["contractor_name"]=\$contractor_name;
\$toprated[\$counter]["contractor_id"]=\$contractor_id;
\$toprated[\$counter]["quality_average"]=\$quality;
\$toprated[\$counter]["cost_average"]=\$cost;
\$toprated[\$counter]["total_average"]=\$toprated[\$counter]["quality_average"]+\$toprated[\$counter]["cost_average"];
\$toprated[\$counter]["number"]=1;
}
\$totalcontractors++;
}

foreach (\$toprated as \$key => \$row)
{\$total_average[\$key] = \$row["total_average"];
}

array_multisort(\$total_average, SORT_DESC, \$toprated);

echo "<h3>Current Top 5 Rated Contractors:</h3>";

echo "<table width=90% border=0 cellspacing=0 align=\"center\">";
echo "<tr>";
echo "<th width=33% align=\"left\" bgcolor=\"#dddddd\">Contractor Name</th>";
echo "<th width=33% bgcolor=\"#dddddd\">Average Quality Rating</th>";
echo "<th width=33% bgcolor=\"#dddddd\">Average Cost Rating</th>";
echo "</tr>";
for (\$counter=0; \$counter<5; \$counter++)
{echo "<tr>";
echo "<td align=\"left\"><a class=\"body\" href=\"/find_contractor2.html?contractor_id=".\$toprated[\$counter]["contractor_id"]."\">".\$toprated[\$counter]["contractor_name"]."</a></td>";
echo "<td align=\"center\">".\$toprated[\$counter]["quality_average"]."</td>";
echo "<td align=\"center\">".\$toprated[\$counter]["cost_average"]."</td>";
echo "</tr>";
}
echo "</table>";

//END LIST THE TOP 5 RATED CONTRACTORS

regards,
steve