soycharliente Posted May 30, 2008 Share Posted May 30, 2008 This is a really long post, but I'm having multiple problems and I didn't really think splitting it up and making separate threads was right. I am writing a script to generate a graph in an image. I'm using a library I found online. I cant figure out how to sort the array I created so that the graph is in numerical order. I am trying to count how many tags I have for each post on my blog and show a graph that will display how many posts have that number of tags. (I hope that makes sense.) Also, the data doesn't even look correct. I built the query in phpMyAdmin so I know exactly what is coming back and it's the correct data. So there's got to be something wrong with the code that is building the array. I am trying to make an array where the key is the number of tags and the value is how many posts have that number of tags. Here's my code so far: <?php require($_SERVER['DOCUMENT_ROOT']."/path/path/main.php"); dbconnect(); $sql = "SELECT COUNT(*) AS total FROM wp_posts p JOIN wp_term_relationships r ON p.ID=r.object_id WHERE p.post_type='post' GROUP BY p.ID"; $result = mysql_query($sql) OR DIE ("$sql<br />".mysql_error()); if (mysql_num_rows($result) > 0) { $arr = array(); while ($row = mysql_fetch_array($result)) { $num = $row['total']; if (array_key_exists($num, $arr)) { $arr[$num]++; } else { $arr[$num] = 1; } } require($_SERVER['DOCUMENT_ROOT']."/path/path/libchart.php"); $chart = new HorizontalBarChart(450, 450); $dataSet = new XYDataSet(); /*sort($arr, SORT_NUMERIC);*/ foreach ($arr as $key => $value) { $dataSet->addPoint(new Point("$key", $value - 1)); } $chart->setDataSet($dataSet); $chart->setTitle("Number of tags per post"); $chart->render(); } dbclose(); ?> Images attached. (I tried to use sort also, but it screwed it all up.) As you can see in the image, 7 is getting 6 back, when it should be 1. (See below.) The correct data is something like (num tags - num posts with that # of tags): 1 - 3 2 - 3 3 - 2 4 - 10 5 - 9 6 - 5 7 - 1 8 - 2 9 - 2 10 - 1 11 - 1 12 - 2 13 - 0 14 - 1 15 - 0 16 - 1 Also, I don't really know how it's handling when no posts have that number of tags (like 13 and 15). But those numbers still need to be included. So ideally, my array would have keys that go from the smallest number of tags on a post (which is 1) to the largest (which could vary). [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
craygo Posted May 30, 2008 Share Posted May 30, 2008 why not just order it with mysql instead of sorting an array. use the id as the key(since it is unique) and the posts as the values. <?php require($_SERVER['DOCUMENT_ROOT']."/path/path/main.php"); dbconnect(); $sql = "SELECT p.ID AS id, COUNT(*) AS total FROM wp_posts p JOIN wp_term_relationships r ON p.ID=r.object_id WHERE p.post_type='post' GROUP BY p.ID ORDER BY total DESC"; $result = mysql_query($sql) OR DIE ("$sql<br />".mysql_error()); if (mysql_num_rows($result) > 0) { $arr = array(); while ($row = mysql_fetch_array($result)) { $num = $row['total']; $id = $row['id']; $arr[$id] = $num; } require($_SERVER['DOCUMENT_ROOT']."/path/path/libchart.php"); $chart = new HorizontalBarChart(450, 450); $dataSet = new XYDataSet(); /*sort($arr, SORT_NUMERIC);*/ foreach ($arr as $key => $value) { $dataSet->addPoint(new Point("$key", $value - 1)); } $chart->setDataSet($dataSet); $chart->setTitle("Number of tags per post"); $chart->render(); } dbclose(); ?> Ray Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 30, 2008 Author Share Posted May 30, 2008 I have about 40 posts. I feel like your solution would just give me 40 results with the number of tags for each post in numerical order. I need an array of length 16 with a count for each. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 30, 2008 Author Share Posted May 30, 2008 Actually, ordering it won't do anything. It's still the same data. I still have the problem of counting the posts for each tag number. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 30, 2008 Author Share Posted May 30, 2008 why not just order it with mysql instead of sorting an array. use the id as the key(since it is unique) and the posts as the values. I get what that means now. When I changed it, I got the same result as when I originally wrote the code. I don't know what I did, but it's working now. The data is being pulled correctly and it's sorted! Now all that's left is adding in the spaces that have 0 posts with that number. So if I have an array like: Array ( [16] => 1 [14] => 1 [12] => 2 [11] => 1 [10] => 1 [9] => 2 [8] => 2 [7] => 1 [6] => 7 [5] => 8 [4] => 10 [3] => 2 [2] => 3 [1] => 2 ) How can I get 15 and 13 in there in the right spot and have it be 0? And not just 15 and 13 per say. Those numbers in this example. But take care of any situation or any number that I might have to do that for. Quote Link to comment Share on other sites More sharing options...
craygo Posted May 30, 2008 Share Posted May 30, 2008 try this $count = max(array_keys($arr)); for($i=1; $i<=$count; $i++){ if(!isset($arr[$i])){ $arr[$i] = 0; } } krsort($arr); print_r($arr); Ray Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 30, 2008 Author Share Posted May 30, 2008 Yep! Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.