Solved

How to Order By in a PHP While Loop

Posted on 2012-03-27
1
308 Views
Last Modified: 2012-06-22
I configure a total point value that I would like to order by in my while loop.  I cannot do this in mysql b/c I haven't calculated that amount until the loop.

$query = "select author_name, 
       sum(case when new_topic = 0 then 1 else 0 end) post_total,
       sum(case when new_topic = 1 then 1 else 0 end) thread_total
from `posts` 
where from_unixtime(post_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(post_date) <" . "'" . $enddate . "'" . 
"group by author_name";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

//Create the result set
while ($line= mysql_fetch_array($result))
	 {
	echo "Username: " .  $line['author_name'];
	echo "</br>";
	echo "Threads: " .  $line['thread_total'];
	echo "</br>";
	echo "Posts: " .  $line['post_total'];
	echo "</br>";
	echo "Overall Points: " .  (($line['post_total'] * $posts) + ($line['thread_total'] * $topics));
	echo "</br>";
	echo "</br>";
	}

Open in new window

0
Comment
Question by:N R
1 Comment
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37773969
MySQL has some fairly robust math functions.  But if you would rather do it in PHP you can create an array from the rows of the results set.  You might do it something like this in lines 11 through the end of the code snippet.  Warning: This is untested code, and it cannot be tested without your data base, so you have to test it yourself!
$htm = array();
$cnt = 0;
while ($line= mysql_fetch_array($result))
{
    // COMPUTE THE SCORE
    $num = (($line['post_total'] * $posts) + ($line['thread_total'] * $topics));
    
    // PREPARE THE LINE OF HTML
    $out = NULL;
    $out .= "Username: " .  $line['author_name'];
    $out .= "</br>";
    $out .= "Threads: " .  $line['thread_total'];
    $out .= "</br>";
    $out .= "Posts: " .  $line['post_total'];
    $out .= "</br>";
    $out .= "Overall Points: " .  $num;
    $out .= "</br>";
    $out .= "</br>";
    $out .= PHP_EOL;
    
    // AVOID COLLISIONS ON KEYS
    if (isset($htm["A$num"]))
    {
        $key = "A$num" . '.' . $cnt;
        $cnt++;
    }
    else
    {
        $key = "A$num";
    }
    
    // ADD THE LINE OF HTML TO THE ARRAY
    $htm[$key] = $out; 
}

// SORT THE ROWS DESCENDING
krsort($htm);

// WRITE THE HTML STRING
foreach ($htm as $str) echo $str;

Open in new window

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now