Inicio
Buscar
Ingresar
Registrarse
Starfield: el juego que revolucionará el espacio y la tecnología
Hack x Crack - Comunidad de Seguridad informática
»
Programación
»
Programación Web
»
Arbol de categorias a tabla html
Imprimir
Páginas: [
1
]
Ir Abajo
Autor
Tema: Arbol de categorias a tabla html (Leído 2402 veces)
Takkote
{ L0 } Ñuub
Mensajes: 1
Arbol de categorias a tabla html
«
en:
Septiembre 01, 2014, 06:57:39 am »
Buenas a todos.
Recurro a pedir ayuda por internet ya que llevo unos días atrancado en un problema y no consigo dar con la tecla.
Como indico en el título, estoy intentando crear una tabla a partir de un arbol de categorías, pero a la hora de hacer el algoritmo que construya la tabla no doy con la solución.
Código: PHP
/*
* Construye el array de categorias en forma de arbol
* */
$raw
=
$DB
->
get_records
(
'question_categories'
,
array
(
'parent'
=>
0
,
'contextid'
=>
$this
->
context
->
id
)
,
'sortorder ASC'
)
;
foreach
(
$raw
as
$i
)
{
$this
->
categoriasArbol
[
$i
->
id
]
=
array
(
'id'
=>
$i
->
id
,
'nombre'
=>
$i
->
name
,
'parent'
=>
$i
->
parent
,
'nsubcat'
=>
$DB
->
count_records
(
'question_categories'
,
array
(
'parent'
=>
$i
->
id
,
'contextid'
=>
$this
->
context
->
id
)
)
,
'subcat'
=>
array
(
)
)
;
if
(
$this
->
categoriasArbol
[
$i
->
id
]
[
'nsubcat'
]
>
0
)
{
$this
->
categoriasArbol
[
$i
->
id
]
[
'subcat'
]
=
$this
->
lee_subcategorias
(
$i
->
id
)
;
}
}
Código: PHP
private
function
lee_subcategorias
(
$id
)
{
global
$DB
;
$temp
=
array
(
)
;
$raw
=
$DB
->
get_records
(
'question_categories'
,
array
(
'parent'
=>
$id
,
'contextid'
=>
$this
->
context
->
id
)
,
'sortorder ASC'
)
;
foreach
(
$raw
as
$i
)
{
$temp
[
$i
->
id
]
=
array
(
'id'
=>
$i
->
id
,
'nombre'
=>
$i
->
name
,
'parent'
=>
$i
->
parent
,
'nsubcat'
=>
$DB
->
count_records
(
'question_categories'
,
array
(
'parent'
=>
$i
->
id
,
'contextid'
=>
$this
->
context
->
id
)
)
,
'subcat'
=>
array
(
)
)
;
if
(
$temp
[
$i
->
id
]
[
'nsubcat'
]
>
0
)
{
$temp
[
$i
->
id
]
[
'subcat'
]
=
$this
->
lee_subcategorias
(
$i
->
id
)
;
}
}
return
$temp
;
Con esto construyo el arbol de categorías.
Lo siguiente es crear las celdas, empezando por el padre y recorriendo todos los hijos.
Código: PHP
private
function
creaCeldasCategorias
(
$cat
)
{
$celda
=
new
html_table_cell
(
)
;
$celda
->
idcat
=
$cat
[
'id'
]
;
$celda
->
parent
=
$cat
[
'parent'
]
;
$celda
->
rowspan
=
$this
->
calculaRowspan
(
$cat
)
;
$celda
->
text
=
$cat
[
'nombre'
]
;
$celda
->
nsubcat
=
$cat
[
'nsubcat'
]
;
if
(
$cat
[
'nsubcat'
]
>
0
)
{
$celda
->
subcat
=
array
(
)
;
foreach
(
$cat
[
'subcat'
]
as
$c
)
{
$celda
->
subcat
[
]
=
$this
->
creaCeldasCategorias
(
$c
)
;
}
}
return
$celda
;
}
private
function
calculaRowspan
(
$cat
)
{
$n
=
0
;
if
(
$cat
[
'nsubcat'
]
==
0
)
{
return
1
;
}
else
{
foreach
(
$cat
[
'subcat'
]
as
$c
)
{
$n
+=
$this
->
calculaRowspan
(
$c
)
;
}
}
return
$n
;
}
$celdasCat
=
$this
->
creaCeldasCategorias
(
$this
->
categoriasArbol
[
126
]
)
;
Con esto tengo ya un array de objetos html_table_cell. Lo siguiente es donde tengo el problema, y es a la hora de recorrer el arbol y meter cada celda en la fila que le corresponda.
Un ejemplo de una rama de categorias seria esto
Código: HTML
<
table
border
=
1
>
<
tr
>
<
td
rowspan
=
20
>
Categoria 1
<
/
td
>
<
td
rowspan
=
10
>
Categoria 1-1
<
/
td
>
<
td
rowspan
=
2
>
Categoria 1-1-1
<
/
td
>
<
td
rowspan
=
1
>
Categoria 1-1-1-1
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-1-1-2
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-1-2
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-1-3
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-1-4
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
"5"
>
Categoria 1-1-5
<
/
td
>
<
td
rowspan
=
1
>
Categoria 1-1-5-1
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-1-5-2
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
"3"
>
Categoria 1-1-5-3
<
/
td
>
<
td
rowspan
=
1
>
Categoria 1-1-5-3-1
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-1-5-3-2
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-1-5-3-3
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
"3"
>
Categoria 1-2
<
/
td
>
<
td
rowspan
=
1
>
Categoria 1-2-1
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-2-2
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-2-3
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
"2"
>
Categoria 1-3
<
/
td
>
<
td
rowspan
=
1
>
Categoria 1-3-1
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-3-2
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
"2"
>
Categoria 1-4
<
/
td
>
<
td
rowspan
=
1
>
Categoria 1-4-1
<
/
td
>
<
/
tr
>
<
tr
>
<
td
rowspan
=
1
>
Categoria 1-4-2
<
/
td
>
<
/
tr
>
<
/
table
>
Llevo dándole vueltas y par de días, he intentado hacerlo tanto recursivo como iterativo pero no lo consigo. A ver si alguien puede echarme un cable.
Gracias.
En línea
Imprimir
Páginas: [
1
]
Ir Arriba
Hack x Crack - Comunidad de Seguridad informática
»
Programación
»
Programación Web
»
Arbol de categorias a tabla html
Va un mudo y le dice a un sordo: Hack x Crack usa cookies. Pues eso...
OK
Learn more