How to pass a MySql Hierarchical data to a array in PHP?

zwitterion

I have this function:

function displayTree($parente, $level) {
    $link = dbCLASS::dbConnect();
    $result = mysql_query("SELECT * FROM procediemntos_profissionais where  PROCEDIEMNTOS_PROFISSIONAIS_PARENTE_ID = $parente ");
    while ($row = mysql_fetch_array($result)) {

        echo str_repeat('&nbsp', $level) . $row['PROCEDIEMNTOS_PROFISSIONAIS_NOME'] .  "<br/>";

        self::displayTree($row['PROCEDIEMNTOS_PROFISSIONAIS_Pk'], $level + 1);
    }
}

How could I change this code if I want to return a Array whith all elements instead of print it?

Rasclatt

I don't know exactly what your database class was doing, but you weren't using it so I am just using a dumbed down db class I normally use to fetch data. Also, your query method (mysql_) is outdated and deprecated.

<?php
    class DBEngine
        {
            protected   $con;
            // Create a default database element
            public  function __construct($host = '',$db = '',$user = '',$pass = '')
                {
                    try {
                            $this->con  =   new PDO("mysql:host=$host;dbname=$db",$user,$pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
                        }
                    catch (Exception $e) {
                          return 0;
                        }
                }

            // Simple fetch and return method
            public  function Fetch($_sql)
                {
                    $query  =   $this->con->prepare($_sql);
                    $query->execute();

                    if($query->rowCount() > 0) {
                            while($array = $query->fetch(PDO::FETCH_ASSOC)) {
                                    $rows[]   =   $array;
                                }
                        }

                    return (isset($rows) && $rows !== 0 && !empty($rows))? $rows: 0;
                }

            // Simple write to db method
            public  function Write($_sql)
                {
                    $query  =   $this->con->prepare($_sql);
                    $query->execute();
                }
        }

    class   MakeTree
        {
            public  $_TreeArray;

            public function display($parente = '', $level = '', $_makeArray = false)
                {
                    if(!empty($parente)) {
                            // Create database connection
                            $con    =   new DBEngine('localhost','mydatabase','dbusename','dbpassword');
                            // Fetch results from database
                            $result =   $con->Fetch("SELECT * FROM procediemntos_profissionais where  PROCEDIEMNTOS_PROFISSIONAIS_PARENTE_ID = '$parent'");
                            // If not empty
                            if($result !== 0) {
                                    foreach($result as $row) {
                                            // Write if $_makeArray is false
                                            if($_makeArray !== true)
                                                echo str_repeat('&nbsp', $level) . $row['PROCEDIEMNTOS_PROFISSIONAIS_NOME'] .  "<br/>";
                                            // Save to array
                                            else
                                                $this->_TreeArray[] =   str_repeat('&nbsp', $level) . $row['PROCEDIEMNTOS_PROFISSIONAIS_NOME'] .  "<br/>";

                                            if(isset($row['PROCEDIEMNTOS_PROFISSIONAIS_Pk']))
                                                $this->display($row['PROCEDIEMNTOS_PROFISSIONAIS_Pk'], $level + 1, $_makeArray);
                                        }

                                    // Return the array if isset
                                    if(isset($this->_TreeArray))
                                        return $this->_TreeArray;
                                }
                        }
                }
        }

    // Create instance
    $tree   =   new MakeTree();
    // Set the value as true to start array
    $array  =   $tree->display('value','level',true);
    print_r($array); ?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass a MySql Hierarchical data to a array in PHP?

From Dev

php - convert data from database to hierarchical array

From Dev

how to pass an array of data through cURL in php?

From Dev

PHP Hierarchical MYSQL query

From Dev

GET Hierarchical data in MySQL

From Dev

Hierarchical string into PHP array

From Dev

Search in hierarchical data in PHP

From Dev

Sorting data into mulit-dimensional, hierarchical array with PHP

From Dev

how to pass and process data (php) to database (mysql) for best performance?

From Dev

How to pass data of many textbox values with POST array in PHP

From Dev

How to get join table data and pass inside array in php

From Dev

How do I store this hierarchical data using MySQL?

From Dev

How to index a hierarchical data?

From Dev

Hierarchical tree menu - PHP / MySQL

From Dev

Somewhat-Hierarchical Data With MySQL

From Dev

How to pass array of data to controller?

From Dev

how to pass array in data (ajax)

From Dev

How to pass an pass user input into an array however many times they want and then print the data in PHP?

From Dev

php/mysql - while loop ..pass result in array?

From Dev

How to work with data from mysql in php-multidimensional array

From Dev

PHP How to save data from array to mysql using laravel 5

From Dev

How to print mysql query data into php multidimensional array?

From Dev

Insert array data in php/mysql

From Dev

Pass MYSQL data from PHP to HTML

From Dev

Pass php variable/array data to angular js

From Dev

pass form values into my php data array

From Dev

How to search Hierarchical Data with Linq

From Dev

Selecting hierarchical data using MySQL variable

From Dev

Selecting hierarchical data using MySQL variable

Related Related

HotTag

Archive