#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    PHP/MySql: Print parent and children from a table


    Hi everybody, and thank you in advance. I'm new to this forum and to PHP/MySql.

    I've a table:

    Code:
    CREATE TABLE IF NOT EXISTS `Test` ( 
    `ID` INT NOT NULL AUTO_INCREMENT , 
    `Parent` VARCHAR(45) NOT NULL , 
    `Child` VARCHAR(45) NOT NULL , 
    PRIMARY KEY (`ID`) ) 
    ENGINE = InnoDB; 
    
    
    INSERT INTO `Test` (`ID`,`Parent`,`Child`) VALUES ('1', 'X', 'a'); 
    INSERT INTO `Test` (`ID`,`Parent`,`Child`) VALUES ('2', 'X', 'b'); 
    INSERT INTO `Test` (`ID`,`Parent`,`Child`) VALUES ('3', 'X', 'c'); 
    INSERT INTO `Test` (`ID`,`Parent`,`Child`) VALUES ('4', 'Y', 'a'); 
    INSERT INTO `Test` (`ID`,`Parent`,`Child`) VALUES ('5', 'Y', 'b'); 
    INSERT INTO `Test` (`ID`,`Parent`,`Child`) VALUES ('6', 'Z', 'a'); 
    INSERT INTO `Test` (`ID`,`Parent`,`Child`) VALUES ('7', 'W', 'a'); 
    INSERT INTO `Test` (`ID`,`Parent`,`Child`) VALUES ('8', 'W', 'b'); 
    INSERT INTO `Test` (`ID`,`Parent`,`Child`) VALUES ('9', 'W', 'c');
    The idea is to print, using PHP (in a HTML page), this (for every parent every child into the table):

    X
    a
    b
    c

    Y
    a
    b

    Z
    a

    W
    a
    b
    c

    Do you have an idea/suggestion to make that?

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Code:
    $q = "select * from test order by Parent,Child";
    $qrslts = $pdo->query($q);
    if ($qrslts)
    {
         $last_parent = '';
        while ($row = $qrslts->fetch(PDO::FETCH_ASSOC))
         {
               if ($last_parent <> $row['Parent'])
               {
                   echo $row['Parent']."<br>";
                   $last_parent = $row['Parent'];
               }
               echo $row['Child']."<br>";
         }
    }
    else
    {
        echo "Query failed to run - error is ".$pdo->ErrorInfo();
        exit();
    }
    10:50am - corrected my original error in the error handling line.

    And yes - I'm assuming you have a $pdo object. You could be using mysqli instead, but hopefully NOT MySQL_****.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Jimmy's code assumes you have a valid PDO object, and there's a syntax error in the error handling code (ironically), and <> is generally not as well accepted as !=, but other than those three things it's the right answer, generally speaking.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo