Showing all of the children of each parent

phpchildrenparentsgeneticsgenesparent

Last Update : 2023-09-22 UTC 07:14:18 AM

Answers of > Showing all of the children of each parent

w3coded children parent w3coded children parent Why should I have to use jQuery to display w3coded children parent something as simple as the children of each w3coded children parent parent? Is there no PHP solution here? w3coded children parent – Chris G. w3coded children parent Nov 1 '11 at 20:15 w3coded children parent ,I have a table People. I want w3coded children parent to display an HTML table consisting of every w3coded children parent parent, with all of their children directly w3coded children parent underneath of them., w3coded children parent w3coded children parent When you have 2 parents, w3coded children parent do you want to display them inline with each w3coded children parent other, or as separate entities, with the same w3coded children parent children? w3coded children parent – Patrick Perini w3coded children parent Nov 1 '11 at 20:46 w3coded children parent w3coded children parent ,Second, you w3coded children parent should prefer using a "fetch_all" strategy (with w3coded children parent mysqli php extension, for example, but it is w3coded children parent available with PDO also), which will give you w3coded children parent the ability with one single operation to have w3coded children parent the whole result set fetched in a bi-dimensional w3coded children parent associative array.

2. If you really want to do this is in a single query, you can use a LEFT JOIN on the table against itself:

SELECT
  p.id AS parent_id,
  p.firstname AS parent_firstname,
  p.lastname  AS parent_lastname,
  c.id AS child_id,
  c.firstname AS child_firstname,
  c.lastname  AS child_lastname
FROM
  People AS p
  LEFT JOIN People AS c ON c.parent = p.id
WHERE p.parent = 0
ORDER BY p.id

Again, you really, really need an index on the parent column. The ORDER BY clause is there to ensure that the children of each parent are sorted together; you can change it e.g. to something like p.lastname, p.firstname, p.id, c.lastname, c.firstname, c.id if you want the names sorted in alphabetical order. In PHP, you then need to loop over the results and print a new header whenever the parent ID changes (and remember to handle the case where the child_* columns are NULL), something like:

$res = mysql_query( $sql );
$last_parent_id = 0;
while ( $row = mysql_fetch_object( $res ) ) {
    if ( $row->parent_id != $last_parent_id ) {
        // print parent header
        $last_parent_id = $row->parent_id;
    }
    if ( $row->child_id ) {
        // print child row
    }
}

3. The third option is to just fetch all the rows with a simple SELECT * FROM People query and build the tree in PHP:

$res = mysql_query( "SELECT * FROM People" );  // add WHERE clauses if needed
$names = array();
$parents = array();
$children = array();

while ( $row = mysql_fetch_object( $res ) ) {
    $names[ $row->id ] = array( $row->firstname, $row->lastname );
    if ( $row->parent == 0 ) {
        $parents[] = $row->id;
    } else {
        if ( !array_key_exists( $row->parent, $children ) )
            $children[ $row->parent ] = array();
        $children[ $row->parent ][] = $row->id;
    }
}

foreach ( $parents as $parent_id ) {
    // print parent header
    if ( array_key_exists( $parent_id, $children ) ) {
        foreach ( $children[ $parent_id ] as $child_id ) {
            // print child row
        }
    }
}

So, start with something like:

SELECT 
       a.id parent_id, a.firstname parent_name, a.lastname parent_lastname, 
       b.id child_id, b.firstname child_firstname, b.lastname child_lastname
FROM 
       People a LEFT OUTER JOIN People b ON a.id = b.parent
WHERE  
       a.parent = 0;

AJAX: if - for instance - your PHP script has been rather called via AJAX you could walk the query result array as well, but this time interpreting it to build a JSON structure with which you would respond to the call, just like that:

{
    "1": {
        "id": 1,
        "firstname": "James",
        "lastname": "Donovan",
        "children": {
            "6": {
                "id": 6,
                "firstname": "Nikolai",
                "lastname": "Donovan"   
            }
        }
    },
    "2": {
        "id": 2,
        "firstname": "Jeffrey",
        "lastname": "Williams",
        "children": {
            "4": {
                "id": 4,
                "firstname": "Carol",
                "lastname": "Williams"  
            },
            "5": {
                "id": 5,
                "firstname": "Sarah",
                "lastname": "Williams"  
            }
        }
    },
    "3": {
        "id": 3,
        "firstname": "Emmit",
        "lastname": "Herring",
        "children": { }
    }
}

You could use a loop in a loop:

$res = mysql_query("SELECT PARENT");
while( $row = mysql_fetch_assoc($res) )
{

  // echo parent

  $res2 = mysql_query("SELECT CHILD WHERE PARENT SOMETHING");
  while( $row2 = mysql_fetch_assoc($res2) )
  {

    // echo child
  }
}

Or, keep it for later, and store a flag.

$people = array();

$res = mysql_query("SELECT PARENT");
while( $row = mysql_fetch_assoc($res) )
{
  $people[] = array('is_parent' => true,
                    'info'      => $row);

  $res2 = mysql_query("SELECT CHILD WHERE PARENT SOMETHING");
  while( $row2 = mysql_fetch_assoc($res2) )
  {    
    $people[] = array('is_parent' => false,
                      'info'      => $row2);
  }
}

// later

foreach( $people as $person )
{
  if( $person['is_parent'] )
  {
    // echo parent
  }
  else
  {
    // echo child
  }
}

Current topics : Showing all of the children of each parent

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 11:52:51 AM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 11:52:44 AM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 11:52:26 AM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 11:52:10 AM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 11:52:00 AM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 11:51:47 AM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 11:51:40 AM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 11:51:21 AM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 11:51:12 AM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 11:51:02 AM

Top
© 2023 W3CODED - All Rights Reserved.