Solved

How to Order By in a PHP While Loop

Posted on 2012-03-27
1
321 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

732 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