Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

    thanks in advance,
    steve
    Last edited by se4b4ss; 05-15-2004 at 07:30 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    sorts the variable then lists the top five

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

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 />';


  • #4
    Regular Coder
    Join Date
    Feb 2004
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    missing-score,

    Code adjusted:

    Code:
    asort($qualityaverage); 
    
    for ($i=0;$i<5;$i++) {
    echo $i." ".$qualityaverage[$i].'<br />'; 
    }
    your codes results in this:

    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?

    thanks in advance,
    steve

  • #5
    Regular Coder
    Join Date
    Feb 2004
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i have been working with multidimensional arrays. hope to have something by tomorrow.

    regards,
    steve

  • #6
    Regular Coder
    Join Date
    Feb 2004
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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))
    		{$added=0;
    		$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;
    					$added=1;
    					}
    				}
    			if (!$added)
    				{$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
    •