We help IT Professionals succeed at work.
Get Started

How to populate advanceddatagrid with hierarchical data using php and MySQL?

salamay asked
Last Modified: 2013-11-05
Following is the hierarchy supposed to go in the grid

Project 1
         Well 1
         Well 2
         Well 3
Project 2
         Well 4
         Well 5
Project 3
         Well 6
         Well 7
         Well 8

I have two tables Projects and Wells in my db with a fk in wells table relating to the project_id

Projects table
project_id       project_name
1                     Project 1
2                     Project 2
3                     Project 3

Wells table
well_id           well_name        projects_project_id
1                     Well 1               1
2                     Well 2               1
3                     Well 3               1
4                     Well 4               2
5                     Well 5               2
6                     Well 6               3
7                     Well 7               3
8                     Well 8               3

I want to modify the following function to get hierarchical data
public function getAllProjectsAndWells() {

		$stmt = mysqli_prepare(
					$this->connection, "SELECT project_id, project_name, well_name 
					FROM projects, wells 
					WHERE projects.project_id = wells.projects_project_id");		
		$rows = array();
		mysqli_stmt_bind_result($stmt, $row->project_id, $row->project_name, $row->well_name);
	    while (mysqli_stmt_fetch($stmt)) {
	      $rows[] = $row;
	      $row = new stdClass();
	      mysqli_stmt_bind_result($stmt, $row->project_id, $row->project_name, $row->well_name);
	    return $rows;

Open in new window

Also How can I populate the grid with this array?

Watch Question
This problem has been solved!
Unlock 2 Answers and 4 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE