1

I have a function that outputs details of a property and need to sort the results by price/sqft.

<?php
    //get search results!

    if ($dbh)
    { 

        $sql = sql_spShowMemberSearch($_SESSION['r_search']); //create query from search vals
        $tout .= "<p><b>sql_spShowMemberSearch</b>: " . $sql . "</p>";


        $result=odbc_exec($dbh, $sql); //create db recordset

        if (isset($r_results)) { unset($r_results); }
        if (isset($_SESSION['r_results'])) { unset($_SESSION['r_results']); }
        //store recordset in array for use after closing db connection

        while(odbc_fetch_row($result)) // getting data
        {
            $rowcount++;
            //store values needed       
            //$r_results[$rowcount]['IsUs']             = odbc_result($result, 1); // IsUs
            $r_results[$rowcount]['intID']          = odbc_result($result, 2); // intID
            $r_results[$rowcount]['currentprice']   = odbc_result($result, 3); // CurrentPrice
            $r_results[$rowcount]['beds']           = odbc_result($result, 4); // Beds
            $r_results[$rowcount]['bath']           = odbc_result($result, 5); // Bath
            $r_results[$rowcount]['year']           = odbc_result($result, 6); // Year
            $r_results[$rowcount]['sf']             = odbc_result($result, 7); // SF
            $r_results[$rowcount]['address']        = odbc_result($result, 8); // Address
            $r_results[$rowcount]['community']      = odbc_result($result, 9); // Community
            $r_results[$rowcount]['office']         = odbc_result($result, 10); // Office
            $r_results[$rowcount]['agent']          = odbc_result($result, 11); // Agent
            //$r_results[$rowcount]['Type']             = odbc_result($result, 12); // Type
            $r_results[$rowcount]['photopath']      = odbc_result($result, 13); // PhotoPath
            $r_results[$rowcount]['pics']           = odbc_result($result, 14); // Pics
            $r_results[$rowcount]['mlsid']          = odbc_result($result, 15); // MLSID
            $r_results[$rowcount]['city']           = odbc_result($result, 16); // City
            $r_results[$rowcount]['long']           = odbc_result($result, 17); // Long
            $r_results[$rowcount]['lat']            = odbc_result($result, 18); // Lat
            $r_results[$rowcount]['source']         = odbc_result($result, 19); // Source
            //$r_results[$rowcount]['Tracked']      = odbc_result($result, 20); // Tracked
            $r_results[$rowcount]['status']         = odbc_result($result, 21); // Status
            //$r_results[$rowcount]['PageRow']      = odbc_result($result, 22); // PageRow

            $r_results[$rowcount]['ppsq']       = number_format(odbc_result($result, 3)/odbc_result($result, 7)); // Status


            $plcity = strtoupper(trim(preg_replace("/[^A-Za-z0-9[:space:]]/","",$r_results[$rowcount]['city'])));
            $plcity = str_replace(" ","-",$plcity);
            $pladdr = strtoupper(trim(preg_replace("/[^A-Za-z0-9[:space:]]/","",$r_results[$rowcount]['address'])));
            $pladdr = str_replace(" ","-",$pladdr);
            $plmls = trim($r_results[$rowcount]['mlsid']);
            $r_results[$rowcount]['permalink'] = "http://www.fairplay.com/realty/details/".$plcity."/".$pladdr."/".$plmls;

        }


        if ($rowcount==0) {
            if ($page_current == 1 ) {
                $e_msg[] = 'No results found! Please modify your search parameters (above).';
            } else {
                $e_msg[] = 'We ran out of search results, please go back or modify your search criteria to find additional matches.';
            }

        } 

        if ( $rowcount < $page_size ) {
            //since we can't rely on DB result for total count try not to let users page to non existant pages
            $_SESSION['r_search']['total_results'] = ($_SESSION['r_search']['page_start'] + ($rowcount-1)); 

         }

        //close connection
        odbc_close ($dbh);
    } //if $dbh
    else echo "odbc not connected <br>";
    //end get search results

function display_results($r_results) {
    $rowcount = 0;


    foreach ($r_results as $key=>$value) {

        $rowcount ++;       
        if ( $rowcount == 1 ) {
            echo "\n<h3>Results for Real Estate in <span class='location'>".strtoupper($_SESSION['r_search']['searchtext'])."</span></h3>";
            echo "\n<h4><strong>".number_format($_SESSION['r_search']['total_results'])."</strong> properties match your search</h4>\n";
            echo "\n<table id=\"realty\">\n";
            echo "<thead>\n";                                          
            echo "<th class=\"photos\">Photos</th> \n";                                        
            echo "<th class=\"address\">Address</th> \n"; 
            echo "<th class=\"community\">Community</th> \n";                                                  
            echo "<th class=\"numeric\">Price</th> \n";                                                            
            echo "<th class=\"beds\">Beds</th> \n";                                    
            echo "<th class=\"bath\">Bath</th> \n";                                    
            echo "<th class=\"year\">Year</th> \n";
            echo "<th class=\"numeric sf\">SQFT</th> \n";                                                                                  
            echo "<th class=\"mls\">MLS #</th> \n";
            echo "<th class=\"status\">Status</th> \n";
            echo "<th>&nbsp;</th> \n";                 
            echo "</thead> \n";
        }

        //  echo "Row: $key<br/>";
        //  make row
        echo "<tr> \n";
        echo "<td class=\"photos\"><a style=\"cursor:pointer;\" target=\"_blank\" href=\"/realty/detail/".$r_results[$rowcount]['intID']."\"><img src=\"";

        if ( isset($r_results[$key]['photopath']) ) {
            echo "http://www.foreclosuredatasystems.com/images/FullMLS/" . str_replace("\\","/",$r_results[$key]['photopath'])."/".$r_results[$key]['mlsid'] . "-01.jpg";
        } else {
            echo "http://fairplay.com/img/fr_home_thumb.jpg";
        }

        //echo str_replace("\\","/",$r_results[$key]['photopath'])."/".$r_results[$key]['mlsid'] ;
        echo "\" alt=\"photographs provided by MLS listing service providers\" width=\"100\" /></a></td> \n";    
        echo "<td class=\"address\"> \n";
        echo "<p>".$r_results[$key]['address']."<br /> \n";
        echo " ".$r_results[$key]['city']."</p> \n";
        echo "<!--<a href=\"#?lat=".$r_results[$key]['lat']."&long=".$r_results[$key]['long']."\" class=\"map\">Map this</a>--> \n";
        echo "</td> \n";
        echo "<td class=\"community\">".$r_results[$key]['community']."</td>                             \n";
        echo "<td class=\"price\">\$".number_format($r_results[$key]['currentprice'])."</td>                         \n";
        echo "<td class=\"beds\">".$r_results[$key]['beds']."</td> \n";
        echo "<td class=\"bath\">".$r_results[$key]['bath']."</td> \n";
        echo "<td class=\"year\">".$r_results[$key]['year']."</td> \n";
        echo "<td class=\"sf\">".number_format($r_results[$key]['sf'])."</td> \n";
        echo "<td class=\"mls\">".$r_results[$key]['mlsid']."</td> \n";
        echo "<td class=\"status\">".$r_results[$key]['status']."</td> \n";
        echo "<td class=\"action\"><a href=\"/realty/detail/".$r_results[$rowcount]['intID']."\">View Details</a><!--".$key."--></td> \n";       
        echo "</tr> \n";
        echo "<tr class=\"extra\"> \n";
        echo "<td class=\"photos\"><!--<a href=\"#\">".$r_results[$key]['pics']." photos</a>--></td> \n";
        echo "<td colspan=\"10\" class=\"source\">Listing courtesy of: ".$r_results[$key]['source']." / ".$r_results[$key]['office']."</td> \n";
        echo "</tr> \n";    

    } // END FOREACH LOOP

    echo "</table >\n";
} //function display_results()

?>

How can I sort the output of these results by $r_results[$rowcount]['ppsq']?

2
  • 1
    By looking at a few of the related questions. Commented Oct 20, 2011 at 22:46
  • You should order using sql. And to let us help you, you have to give as the query you run. Commented Oct 20, 2011 at 22:47

1 Answer 1

1

You are in luck, this is a php built in function:

http://www.php.net/manual/en/function.array-multisort.php

Example:

<?php

$r_results = array();
$r_results[0] = array('photopath' => '/photo/1', 'ppsq' => 12);
$r_results[1] = array('photopath' => '/photo/7', 'ppsq' => 456);
$r_results[2] = array('photopath' => '/photo/2', 'ppsq' => 789);
$r_results[3] = array('photopath' => '/photo/9', 'ppsq' => 123);
$r_results[4] = array('photopath' => '/photo/3', 'ppsq' => 23);
$r_results[5] = array('photopath' => '/photo/5', 'ppsq' => 1);

$ppsqs = array();

foreach ($r_results as $key => $arr) {
  $ppsqs[$key] = $arr['ppsq'];
}

array_multisort($ppsqs, SORT_DESC, $r_results);

echo $r_results[0]['photopath'];
echo "\n";
echo $r_results[1]['photopath'];

?>
Sign up to request clarification or add additional context in comments.

2 Comments

how would i output an image on only the top two rows of lets say...10 results?
Editing question with your request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.