?
Solved

How to Order By in a PHP While Loop

Posted on 2012-03-27
1
Medium Priority
?
333 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:Nathan Riley
[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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

765 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