[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to Order By in a PHP While Loop

Posted on 2012-03-27
1
Medium Priority
?
340 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses

649 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