[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

07/03/2009 at 02:02PM PDT, ID: 24543076
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

counting number of rows using a JOIN table and hierarchical data.

Asked by cookiejest in PHP and Databases, PHP Scripting Language, MySQL Server

Hi there, I am storing hierarchical info about zones and a list of projects in my database. They are being displayed using the first function shown below. This works fine. I am now trying to create another function that will count the number of projects that is listed in the root zone and also all of its child zones. For example say i have the zone structure

>webdesign
>>php
>>asp

and the root zone is webdesign, I would want the function to output a count of all projects in webdesign + php + asp. The aim is that the function can be used to count any given "root" zone. I have made a crude attempt but all it returns is 0. Here it is:

////////////////////////////////////////////////////////////// COUNT THE PROJECTS
function countzone_projects($root, $searchstring) {
   // retrieve the left and right value of the $root node
   $rootzoneprojects = 0;
   $result = mysql_query('SELECT nleft, nright FROM zones '.
                          'WHERE zone_ID="'.$root.'";');
   $row = mysql_fetch_array($result);

   // start with an empty $right stack
   $right = array();
      $zoneinfo = array();
   // now, retrieve all descendants of the $root node
   $result = mysql_query('SELECT zone_ID, zone_Name, nleft, nright FROM zones '.
                          'WHERE nleft BETWEEN '.$row['nleft'].' AND '.
                          $row['nright'].' ORDER BY nleft ASC;');

   // display each row
   while ($row = mysql_fetch_array($result)) {
       // only check stack if there is one
       if (count($right)>0) {
           // check if we should remove a node from the stack
           while ($right[count($right)-1]<$row['nright']) {
               array_pop($right);
           }
       }
        
         $subzoneid = $row['zone_ID'];
                           //for each subzone find out how many projects there are for it including searchstring.
            $subzonesql =  " link_projects_zones.zone_ID   =   {$subzoneid}";
            $subzonesearchstring = $subzonesql . $searchstring;
            $subzonetotalsql = "SELECT COUNT(*) FROM projects JOIN link_projects_zones ON projects.proj_ID = link_projects_zones.proj_ID WHERE" . $subzonesearchstring;
            $result = mysql_query($subzonetotalsql);
            $r = mysql_fetch_row($result);
            $numrows = $r[0];
                        
                        //add that number to the total count for the root zone. ($rootzoneprojects)
                        $rootzoneprojects + $numrows;
                        
                        
       // display indented node title
        //echo str_repeat('>',count($right));


       // add this node to the stack
       $right[] = $row['nright'];
   }
   return $rootzoneprojects;
}
////////////////////////////////////////////////////////////////////////////////

my overall aim is to generate ouput like so
webdesign (300 projects)
>>php(200 projects)
>>asp(100 projects)

I hope I have explained myself clearly enough! Thanks for the help
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
/////////////////////////////////////////////////////////////CREATE ARRAY WITH TREE INFO:
 
function display_tree($root) {
   // retrieve the left and right value of the $root node
   $result = mysql_query('SELECT nleft, nright FROM zones '.
                          'WHERE zone_ID="'.$root.'";');
   $row = mysql_fetch_array($result);
 
   // start with an empty $right stack
   $right = array();
	$zoneinfo = array();
   // now, retrieve all descendants of the $root node
   $result = mysql_query('SELECT zone_ID, zone_Name, nleft, nright FROM zones '.
                          'WHERE nleft BETWEEN '.$row['nleft'].' AND '.
                          $row['nright'].' ORDER BY nleft ASC;');
 
   // display each row
   while ($row = mysql_fetch_array($result)) {
       // only check stack if there is one
       if (count($right)>0) {
           // check if we should remove a node from the stack
           while ($right[count($right)-1]<$row['nright']) {
               array_pop($right);
           }
       }
	   
	   			$array_row['zone_ID'] = $row['zone_ID'];
				$array_row['nright'] = $row['nright'];
				$array_row['nleft'] = $row['nleft'];
				$array_row['zone_Name'] = $row['zone_Name'];
				$array_row['zone_Description'] = $row['zone_Description'];
				$array_row['zone_indent'] = count($right);
				$array_row['zone_numberofprojects'] = count($right);
				$zoneinfo[] = $array_row;
				
       // display indented node title
        //echo str_repeat('>',count($right)); 
 
 
       // add this node to the stack
       $right[] = $row['nright'];
   }
   return $zoneinfo;
   //print_r($zoneinfo);
}
Attachments:
 
database structure
 
[+][-]07/04/09 03:22 AM, ID: 24776507

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP and Databases, PHP Scripting Language, MySQL Server
Sign Up Now!
Solution Provided By: angelIII
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07/03/09 02:03 PM, ID: 24774408

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091028-EE-VQP-86 - Hierarchy / EE_QW_3_20080625