Jump to content

[SOLVED] MySQL order by


james19

Recommended Posts

thanks, but i am trying to gather data to generate a Google Chart pie chart, and for each level, it selects a colour from a list. Currently it orders the selection by the title, but instead i would rather it order by the number of records with a specific title, so the largest percentage of the pie chart is always the same colour. Currently this is what i have:

 

		$location_query = mysql_query("SELECT title FROM ".TABLE." ORDER BY title ASC");		
	while ($location_row = mysql_fetch_array($location_query, MYSQL_NUM)) {
		if ($location_row[0]!=$prev_location_result) {
			switch ($location_type_no) {
				case 1:
					$vals_ChartColors .= COLOUR1;
					break;
				case 2:
					$vals_ChartColors .= COLOUR2;
					break;
				case 3:
					$vals_ChartColors .= COLOUR3;
					break;
			}
			$location_type_no = $location_type_no + 1;

			$result = mysql_query("SELECT COUNT(id) AS numrows FROM ".TABLE." WHERE title='{$location_row[0]}'");
			$row = mysql_fetch_array($result, MYSQL_NUM);
			$location_percentage = round(($row[0] / $total_stats)*100, 2);
			if ($location_percentage < 1) {
				$other_ChartData = $other_ChartData + $location_percentage;
			} else {
				$vals_ChartData .= $location_percentage.',';
				$vals_Labels .= $location_row[0].' ('.$location_percentage.'%) | ';
			}

			$prev_location_result = $location_row[0];
		}
	}

 

It display the results in alphabetical order, not number of records (%), so the colour of each section can tend to vary.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/86774-solved-mysql-order-by/#findComment-443502
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.