Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: php sort values help needed

1. ## php sort values help needed

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

• sorts the variable then lists the top five

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

• 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.

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

• missing-score,

Code:
```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

• i have been working with multidimensional arrays. hope to have something by tomorrow.

regards,
steve

• ok, i solved the problem using multidimensional arrays. the implementation can be seen here.

let me know what you think.

Code:
```//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

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•