Solved

How to Order By in a PHP While Loop

Posted on 2012-03-27
1
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

724 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