Treeview in PHP

Salve a tutti,

mi sapete dire come è possibile realizzare una treeview in PHP utilizzando il seguente array?

$result[0]['id'] = 1
$result[0]['name'] = Programmazione
$result[0]['parent'] = 0

$result[0]['id'] = 2
$result[0]['name'] = PHP
$result[0]['parent'] = 1

$result[0]['id'] = 3
$result[0]['name'] = JSP
$result[0]['parent'] = 1

$result[0]['id'] = 4
$result[0]['name'] = Software
$result[0]['parent'] = 0

$result[0]['id'] = 5
$result[0]['name'] = Photoshop
$result[0]['parent'] = 4
.
.
.

Lo zero è il nodo padre. Vorrei creare il seguente albero:

- Programmazione
---- PHP
---- JSP
- Software
---- Photoshop

Grazie e Buona notte

inviato 9 anni fa
ulisse
X 0 X

La struttura dati che hai riportato non va bene perché hai gli elementi che si sovrascrivono a vicenda. L'algoritmo per realizzare l'albero deve essere necessariamente ricorsivo e deve iterare su tutti gli elementi quindi questi devono essere presenti in un array tutti insieme.

 :bye:

risposto 9 anni fa
Gianni Tomasicchio
X 0 X

Ciao Gianni,

l'array è uno solo ed intendevo realizzare una funzione ricorsiva ma non ci sono riuscito ancora. Ho sbagliato l'indice ma la struttura è la stessa. Ecco quella corretta:

$result[0]['id'] = 1
$result[0]['name'] = Programmazione
$result[0]['parent'] = 0

$result[1]['id'] = 2
$result[1]['name'] = PHP
$result[1]['parent'] = 1

$result[2]['id'] = 3
$result[2]['name'] = JSP
$result[2]['parent'] = 1

$result[3]['id'] = 4
$result[3]['name'] = Software
$result[3]['parent'] = 0

$result[4]['id'] = 5
$result[4]['name'] = Photoshop
$result[4]['parent'] = 4
.
.
.

Grazie

risposto 9 anni fa
ulisse
X 0 X

appena ho sotto mano PHP provo a realizzare del codice...

 :bye:

risposto 9 anni fa
Gianni Tomasicchio
X 0 X

prova questo:

<?php
$result[0]['id'] = 1;
$result[0]['name'] = 'Programmazione';
$result[0]['parent'] = 0;

$result[1]['id'] = 2;
$result[1]['name'] = 'PHP';
$result[1]['parent'] = 1;

$result[2]['id'] = 3;
$result[2]['name'] = 'JSP';
$result[2]['parent'] = 1;

$result[3]['id'] = 4;
$result[3]['name'] = 'Software';
$result[3]['parent'] = 0;

$result[4]['id'] = 5;
$result[4]['name'] = 'Photoshop';
$result[4]['parent'] = 4;

function tree(&$result, $parent, $level)
{
   foreach($result as $res)
   {
      if($res['parent'] == $parent)
      {
         echo str_repeat("-", $level);
         echo $res['name'] . "\n";
         $level++;
         tree($result, $res['id'],$level);
         $level--;
      }
   }
}

tree($result, 0,0);
?>

 :bye:

risposto 9 anni fa
Gianni Tomasicchio
X 0 X

Grazie Gianni del tuo aiuto. Ho provato ma non da il risultato richiesto. Forse sbaglio io in qualche cosa?

Ecco il print_r della variabile array:

Array ( [0] => Array ( [id] => 17 [name] => Translators [parent] => 0 ) [1] => Array ( [id] => 4 [name] => Project Leader [parent] => 0 ) [2] => Array ( [id] => 1 [name] => PHP Developer [parent] => 8 ) [3] => Array ( [id] => 18 [name] => Managers [parent] => 0 ) [4] => Array ( [id] => 24 [name] => Lithuanian [parent] => 17 ) [5] => Array ( [id] => 26 [name] => JSP [parent] => 8 ) [6] => Array ( [id] => 3 [name] => Graphics Designer [parent] => 0 ) [7] => Array ( [id] => 23 [name] => French [parent] => 17 ) [8] => Array ( [id] => 22 [name] => English [parent] => 17 ) [9] => Array ( [id] => 8 [name] => Developer [parent] => 0 ) [10] => Array ( [id] => 7 [name] => Database Administrators [parent] => 0 ) [11] => Array ( [id] => 6 [name] => Bookkeeper [parent] => 5 ) [12] => Array ( [id] => 2 [name] => ASP Developer [parent] => 8 ) [13] => Array ( [id] => 25 [name] => Arabian [parent] => 17 ) [14] => Array ( [id] => 5 [name] => Administrative [parent] => 0 ) )

Il risultato è:

Translators
Project Leader
PHP Developer
Managers
Lithuanian
JSP
Graphics Designer
French
English
Developer
Database Administrators
Bookkeeper
ASP Developer
Arabian
Administrative

il mio host ha php5.

Grazie ancora  :bye:

risposto 9 anni fa
ulisse
X 0 X

Se lanci il seguente codice cosa ottieni?

<?php
$result[0] = array ( 'id' => 17, 'name' => 'Translators', 'parent' => 0 ); 
$result[1] = array ( 'id' => 4, 'name' => 'Project Leader', 'parent' => 0 ); 
$result[2] = array ( 'id' => 1, 'name' => 'PHP Developer', 'parent' => 8 ); 
$result[3] = array ( 'id' => 18, 'name' => 'Managers', 'parent' => 0 ); 
$result[4] = array ( 'id' => 24, 'name' => 'Lithuanian', 'parent' => 17 ); 
$result[5] = array ( 'id' => 26, 'name' => 'JSP', 'parent' => 8 ); 
$result[6] = array ( 'id' => 3, 'name' => 'Graphics Designer', 'parent' => 0 ); 
$result[7] = array ( 'id' => 23, 'name' => 'French', 'parent' => 17 ); 
$result[8] = array ( 'id' => 22, 'name' => 'English', 'parent' => 17 ); 
$result[9] = array ( 'id' => 8, 'name' => 'Developer', 'parent' => 0 ); 
$result[10] = array ( 'id' => 7, 'name' => 'Database Administrators', 'parent' => 0 ); 
$result[11] = array ( 'id' => 6, 'name' => 'Bookkeeper', 'parent' => 5 ); 
$result[12] = array ( 'id' => 2, 'name' => 'ASP Developer', 'parent' => 8 ); 
$result[13] = array ( 'id' => 25, 'name' => 'Arabian', 'parent' => 17 ); 
$result[14] = array ( 'id' => 5, 'name' => 'Administrative', 'parent' => 0 ); 

function tree(&$result, $parent, $level)
{
   foreach($result as $res)
   {
      if($res['parent'] == $parent)
      {
         echo str_repeat("-", $level);
         echo $res['name'] . "\n";
         $level++;
         tree($result, $res['id'],$level);
         $level--;
      }
   }
}

tree($result, 0,0);
?>
risposto 9 anni fa
Gianni Tomasicchio
X 0 X

Perfetto Funziona!

Translators
-Lithuanian
-French
-English
-Arabian
Project Leader
Managers
-I.T. Consultant
Graphics Designer
Developer
-PHP Developer
-JSP
-ASP Developer
Database Administrators
Administrative
-Bookkeeper

 O0 

Grazie

:bye:

risposto 9 anni fa
ulisse
X 0 X

Mannaggia perche mi capitano ste cose!!!!

Ho spostato il tuo codice in un altro server con apache e mi visualizza i primi 2 elementi e poi esce dal ciclo.

L'array che sta utilizzando è il seguente:

Array ( [0] => Array ( [idcategory] => 17 [category] => Translators [idparent] => 0 [cvitems] => 0 ) [1] => Array ( [idcategory] => 39 [category] => Server Administrator [idparent] => 0 [cvitems] => 0 ) [2] => Array ( [idcategory] => 4 [category] => Project Leader [idparent] => 0 [cvitems] => 0 ) [3] => Array ( [idcategory] => 34 [category] => Portuguese [idparent] => 17 [cvitems] => 0 ) [4] => Array ( [idcategory] => 1 [category] => PHP Developer [idparent] => 8 [cvitems] => 1 ) [5] => Array ( [idcategory] => 30 [category] => Oracle Server [idparent] => 7 [cvitems] => 0 ) [6] => Array ( [idcategory] => 31 [category] => My SQL [idparent] => 7 [cvitems] => 0 ) [7] => Array ( [idcategory] => 32 [category] => MS SQL Server [idparent] => 7 [cvitems] => 0 ) [8] => Array ( [idcategory] => 18 [category] => Managers [idparent] => 0 [cvitems] => 2 ) [9] => Array ( [idcategory] => 24 [category] => Lithuanian [idparent] => 17 [cvitems] => 0 ) [10] => Array ( [idcategory] => 40 [category] => Linux [idparent] => 39 [cvitems] => 0 ) [11] => Array ( [idcategory] => 38 [category] => Legal and Financial [idparent] => 18 [cvitems] => 0 ) [12] => Array ( [idcategory] => 26 [category] => JSP [idparent] => 8 [cvitems] => 0 ) [13] => Array ( [idcategory] => 27 [category] => I.T. Consultant [idparent] => 18 [cvitems] => 0 ) [14] => Array ( [idcategory] => 3 [category] => Graphics Designer [idparent] => 0 [cvitems] => 0 ) [15] => Array ( [idcategory] => 35 [category] => German [idparent] => 17 [cvitems] => 0 ) [16] => Array ( [idcategory] => 28 [category] => General Advertising [idparent] => 3 [cvitems] => 2 ) [17] => Array ( [idcategory] => 23 [category] => French [idparent] => 17 [cvitems] => 0 ) [18] => Array ( [idcategory] => 37 [category] => Finanza [idparent] => 18 [cvitems] => 1 ) [19] => Array ( [idcategory] => 22 [category] => English [idparent] => 17 [cvitems] => 0 ) [20] => Array ( [idcategory] => 50 [category] => Doctors [idparent] => 0 [cvitems] => 1 ) [21] => Array ( [idcategory] => 8 [category] => Developer [idparent] => 0 [cvitems] => 2 ) [22] => Array ( [idcategory] => 7 [category] => Database Administrators [idparent] => 0 [cvitems] => 0 ) [23] => Array ( [idcategory] => 36 [category] => Construction [idparent] => 0 [cvitems] => 0 ) [24] => Array ( [idcategory] => 33 [category] => C [idparent] => 8 [cvitems] => 0 ) [25] => Array ( [idcategory] => 6 [category] => Bookkeeper [idparent] => 5 [cvitems] => 0 ) [26] => Array ( [idcategory] => 29 [category] => Banners [idparent] => 3 [cvitems] => 0 ) [27] => Array ( [idcategory] => 2 [category] => ASP Developer [idparent] => 8 [cvitems] => 0 ) [28] => Array ( [idcategory] => 25 [category] => Arabian [idparent] => 17 [cvitems] => 0 ) [29] => Array ( [idcategory] => 49 [category] => Analyst [idparent] => 0 [cvitems] => 0 ) [30] => Array ( [idcategory] => 5 [category] => Administrative [idparent] => 0 [cvitems] => 0 ) )

Grazie mille

risposto 9 anni fa
ulisse
X 0 X

hai adattato il codice in modo da considerare idcategory e idparent?

risposto 9 anni fa
Gianni Tomasicchio
X 0 X

ecco il codice modificato:

function treeview(&$result, $parent, $level)
{
   foreach($result as $res)
   {
      if($res['idparent'] == $parent)
      {
         echo "<div onClick=\"open_category(".$res['idcategory'].")\" id=\"category_item\">" . str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $level) . $res['category'] . " (<b>" . $res['cvitems'] . "</b>)</div>";
         $level++;
         treeview($result, $res['idcategory'],$level);
         $level--;
      }
   }
}

Grazie Gianni  :bye:

risposto 9 anni fa
ulisse
X 0 X

A me così funziona:

<pre>
<?php

$result[0] = Array ( 'idcategory' => 17, 'category' => 'Translators', 'idparent' => 0, 'cvitems' => 0 );
$result[1] = Array ( 'idcategory' => 39, 'category' => 'Server Administrator', 'idparent' => 0, 'cvitems' => 0 ) ;
$result[2] = Array ( 'idcategory' => 4, 'category' => 'Project Leader', 'idparent' => 0, 'cvitems' => 0 ); 
$result[3] = Array ( 'idcategory' => 34, 'category' => 'Portuguese', 'idparent' => 17, 'cvitems' => 0 ) ;
$result[4] = Array ( 'idcategory' => 1, 'category' => 'PHP Developer', 'idparent' => 8, 'cvitems' => 1 ) ;
$result[5] = Array ( 'idcategory' => 30, 'category' => 'Oracle Server', 'idparent' => 7, 'cvitems' => 0 ) ;
$result[6] = Array ( 'idcategory' => 31, 'category' => 'My SQL', 'idparent' => 7, 'cvitems' => 0 ) ;
$result[7] = Array ( 'idcategory' => 32, 'category' => 'MS SQL Server', 'idparent' => 7, 'cvitems' => 0 ) ;
$result[8] = Array ( 'idcategory' => 18, 'category' => 'Managers', 'idparent' => 0, 'cvitems' => 2 ) ;
$result[9] = Array ( 'idcategory' => 24, 'category' => 'Lithuanian', 'idparent' => 17, 'cvitems' => 0 ) ;
$result[10] = Array ( 'idcategory' => 40, 'category' => 'Linux', 'idparent' => 39, 'cvitems' => 0 ) ;
$result[11] = Array ( 'idcategory' => 38, 'category' => 'Legal and Financial', 'idparent' => 18, 'cvitems' => 0 ) ;
$result[12] = Array ( 'idcategory' => 26, 'category' => 'JSP', 'idparent' => 8, 'cvitems' => 0 ) ;
$result[13] = Array ( 'idcategory' => 27, 'category' => 'I.T. Consultant', 'idparent' => 18, 'cvitems' => 0 ) ;
$result[14] = Array ( 'idcategory' => 3, 'category' => 'Graphics Designer', 'idparent' => 0, 'cvitems' => 0 ) ;
$result[15] = Array ( 'idcategory' => 35, 'category' => 'German', 'idparent' => 17, 'cvitems' => 0 ) ;
$result[16] = Array ( 'idcategory' => 28, 'category' => 'General Advertising', 'idparent' => 3, 'cvitems' => 2 ) ;
$result[17] = Array ( 'idcategory' => 23, 'category' => 'French', 'idparent' => 17, 'cvitems' => 0 ) ;
$result[18] = Array ( 'idcategory' => 37, 'category' => 'Finanza', 'idparent' => 18, 'cvitems' => 1 ) ;
$result[19] = Array ( 'idcategory' => 22, 'category' => 'English', 'idparent' => 17, 'cvitems' => 0 ) ;
$result[20] = Array ( 'idcategory' => 50, 'category' => 'Doctors', 'idparent' => 0, 'cvitems' => 1 ) ;
$result[21] = Array ( 'idcategory' => 8, 'category' => 'Developer', 'idparent' => 0, 'cvitems' => 2 ) ;
$result[22] = Array ( 'idcategory' => 7, 'category' => 'Database Administrators', 'idparent' => 0, 'cvitems' => 0 ) ;
$result[23] = Array ( 'idcategory' => 36, 'category' => 'Construction', 'idparent' => 0, 'cvitems' => 0 ) ;
$result[24] = Array ( 'idcategory' => 33, 'category' => 'C', 'idparent' => 8, 'cvitems' => 0 ) ;
$result[25] = Array ( 'idcategory' => 6, 'category' => 'Bookkeeper', 'idparent' => 5, 'cvitems' => 0 ) ;
$result[26] = Array ( 'idcategory' => 29, 'category' => 'Banners', 'idparent' => 3, 'cvitems' => 0 ) ;
$result[27] = Array ( 'idcategory' => 2, 'category' => 'ASP Developer', 'idparent' => 8, 'cvitems' => 0 ) ;
$result[28] = Array ( 'idcategory' => 25, 'category' => 'Arabian', 'idparent' => 17, 'cvitems' => 0 ) ;
$result[29] = Array ( 'idcategory' => 49, 'category' => 'Analyst', 'idparent' => 0, 'cvitems' => 0 ) ;
$result[30] = Array ( 'idcategory' => 5, 'category' => 'Administrative', 'idparent' => 0, 'cvitems' => 0 ) ;

function tree(&$result, $parent, $level)
{
   foreach($result as $res)
   {
      if($res['idparent'] == $parent)
      {
         echo str_repeat("-", $level);
         echo $res['category'] . "\n";
         $level++;
         tree($result, $res['idcategory'],$level);
         $level--;
      }
   }
}

tree($result, 0,0);
?>
</pre>
risposto 9 anni fa
Gianni Tomasicchio
X 0 X

il tuo codice mi mostra esclusivamente questo risultato:

Administrative 
   -Bookkeeper

Non capisco  :-\

risposto 9 anni fa
ulisse
X 0 X

a me mostra questo:

[tt]Translators

-Portuguese

-Lithuanian

-German

-French

-English

-Arabian

Server Administrator

-Linux

Project Leader

Managers

-Legal and Financial

-I.T. Consultant

-Finanza

Graphics Designer

-General Advertising

-Banners

Doctors

Developer

-PHP Developer

-JSP

-C

-ASP Developer

Database Administrators

-Oracle Server

-My SQL

-MS SQL Server

Construction

Analyst

Administrative

-Bookkeeper[/tt]

 :dunno:

risposto 9 anni fa
Gianni Tomasicchio
X 0 X

E' possibile che lo stesso codice funzioni diversamente se messo in 2 sistemi linux differenti?

Devo usare php4 o 5?

Forse c'è qualcosa che non viene ben interpretata.

 :buck:

risposto 9 anni fa
ulisse
X 0 X

io sto usando PHP 5 ma non vedo dove possa influire tale scelta

risposto 9 anni fa
Gianni Tomasicchio
X 0 X

Beh neanche io!

Non so quale sia il problema. se il codice è lo stesso!

 :-\

risposto 9 anni fa
ulisse
X 0 X
Effettua l'accesso o registrati per rispondere a questa domanda