Foros del Web » Programando para Internet » PHP »

creando treeview

Estas en el tema de creando treeview en el foro de PHP en Foros del Web. hola, disculpen si esto es el foro equivocado pero creo que intervienen mas php, aqui vamos: Pretendo hacer un treeview de 2 niveles... primer nivel ...
  #1 (permalink)  
Antiguo 03/03/2009, 12:44
Avatar de cesarpunk  
Fecha de Ingreso: enero-2008
Ubicación: Lima
Mensajes: 943
Antigüedad: 16 años, 3 meses
Puntos: 9
creando treeview

hola, disculpen si esto es el foro equivocado pero creo que intervienen mas php, aqui vamos:

Pretendo hacer un treeview de 2 niveles... primer nivel de categoria de productos y el segundo su subcategoria.

La categoria la genero de una busqueda de una bd, debajo esta sus subcategorias generadas tambien con una consulta pero la tabla que contiene esa subcategoria esta dentro de un div: <div id="<?php echo $idcategoria;?>" style="display:none;"> que tiene el id de su categoria a la que pertenece (generado con while) y que esta oculto con la intencion de presionar un boton y hacerlo aparecer y desaparecer simulando el treeview....

El problema es que esa accion lo hago con funciones js pero quisiera enganchar esa funcion con el $idcategoria:

function btnAbre() {
document.getElementById('iddiv').style.display='bl ock';
}

bueno intente poner un textbox oculto cuyo value sea tambien el $idcategoria y pasarlo al js y hacerlo asi:

function btnAbre() {
var codigo = document.formu.idtextbox.value;
document.getElementById(codigo).style.display='blo ck';
}

pero si ningun resultado, el error siempre me sale "esperando elemento", como podria capturar ese id para saber que div abrir? , espero me puedan dar una idea.... les dejo mi enorme codigo para que vean lo que intento hacer:

Código PHP:
<script type="text/JavaScript">
<!--
function btnAbre() {
var codigo = document.getElementById('idcat').value;
document.getElementById(codigo).style.display='block';
}

function btnCierra() {
var codigo = document.getElementById('idcat').value; 
document.getElementById(codigo).style.display='none';
}
//-->
</script>
<style type="text/css"> 
form {
margin:0px;
padding:0px;
}
</style>
<?php
require_once("includes/functions.php");
function 
modulo_Category(){
?>
<form name="frmcat" id="frmcat">
 <table width="200" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td height="24" colspan="3" align="center" valign="bottom" background="http://www.forosdelweb.com/images/trazos/emily_difusion_cabecera_iz.jpg" class="fuenteCabLateral"><strong>Categorias</strong></td>
            </tr>
          <tr>
            <td width="2" rowspan="3" bgcolor="#00892F"></td>
            <td>
            <?php
                
//LISTA DE CATEGORIAS
                
$sql="SELECT * FROM categoria where categoria_estado='1' order by categoria_nombre"
                
$res=mysql_query($sql); 
                
$num=mysql_num_rows($res);
                
//PREGUNTAMOS SI EXISTEN REGISTROS PARA GENERAR LA TABLA DE EMPRESAS HABILITADAS
                
if($num>0){
            
?>
            <table width="196" border="0" cellpadding="0" cellspacing="0">
                <tr>
                <td height="2"></td>
                </tr>
            <?php
            
//GENERANDO NUMERO, CAMPOS DE REGISTROS
                
while( $fila mysql_fetch_array$res ) ) {
               
//generamos los campos que van en las tablas
               
$idcategoria $fila["categoria_id"];
               
$nomcategoria $fila["categoria_nombre"];
            
?>
              <tr>
                <td width="5"></td>
                <td width="9">
                <?php
                $catnum 
return_subcategorias($idcategoria);
                if(
$catnum>0){
                    echo 
'<div id="abre'.$idcategoria.'"><img src="http://www.forosdelweb.com/images/treeItem1.gif" width="9" height="9" style="cursor:pointer;" onclick="javascript:btnAbre();" /></div>';
                    echo 
'<div id="cierra'.$idcategoria.'" style="display:none;"><img src="http://www.forosdelweb.com/images/treeItem2.gif" width="9" height="9" style="cursor:pointer;" onclick="javascript:btnCierra();" /></div>';
                    
                }
                else{
                    echo 
'<img src="http://www.forosdelweb.com/images/treeItem0.gif" width="9" height="9" />';
                }
                
?>
                </td>
                <td width="5"></td>
                <td height="15" align="left" class="fuenteMenuCategoria"><a href="<?php echo "categorias.php?catID=$idcategoria"?>" class="enlaceCategoria"><?php echo $nomcategoria?></a><input name="idcat" type="text" id="idcat" size="2" value="<?php echo $idcategoria?>" /></td>
              </tr>
              
              <tr>
              <td colspan="3"></td>
              <td>
              <div id="<?php echo $idcategoria;?>" style="display:none;">
              <?php 
              
//LISTA DE SUBCATEGORIAS
                
$sql2="SELECT * FROM subcategoria where subcategoria_estado='1' and categoria_id='$idcategoria'"
                
$res2=mysql_query($sql2); 
                  
$num2=mysql_num_rows($res2);
                if(
$num2>0){
              
?>
                  <table width="100%">
              <?php
                
//GENERANDO NUMERO, CAMPOS DE REGISTROS
                
while( $fila2 mysql_fetch_array$res2 ) ) {
                
//generamos los campos que van en las tablas
                
$idsubcategoria $fila2["subcategoria_id"];
                
$nomsubcategoria $fila2["subcategoria_nombre"];
              
?>
                      <tr>
                        <td width="5"></td>
                        <td width="9"><img src="http://www.forosdelweb.com/images/iconos/emily_difusion_vineta.jpg" width="9" height="7" /></td>
                        <td width="5"></td>
                        <td height="15" align="left" class="fuenteMenuCategoria"><a href="<?php echo "categorias.php?catID=$idsubcategoria"?>" class="enlaceCategoria"><?php echo $nomsubcategoria?></a></td>
                      </tr>
             <?php
                

             
?>
                  </table>
             <?php
            
//CERRAMOS EL IF DE LISTA DE SUBCATEGORIAS
            
}
            
?>
              </div></td></tr>
            <?php
                

            
?>
              <tr>
                <td height="2"></td>
                </tr>
            </table>
            <?php
            
//CERRAMOS EL IF DE LISTA DE CATEGORIAS
            
}
            else{
                echo 
" ";
            }
            
?>
            </td>
            <td width="2" rowspan="3" bgcolor="#00892F"></td>
          </tr>
          <tr>
            <td height="1" bgcolor="#00892F"></td>
            </tr>
          <tr>
            <td><table width="196" border="0" cellpadding="0" cellspacing="0">
              <tr>
                  <td height="5"></td>
              </tr> 
              <tr>
                <td width="5"></td>
                <td width="9"><img src="http://www.forosdelweb.com/images/iconos/emily_difusion_vineta.jpg" width="9" height="7" /></td>
                <td width="5"></td>
                <td align="left" class="fuenteMenuCategoria"><a href="categorias.php" class="enlaceCategoria">Todas las Categor&iacute;as </a></td>
              </tr>
            </table></td>
            </tr>
          <tr>
            <td colspan="3"><strong><img src="http://www.forosdelweb.com/images/trazos/emily_difusion_pie_iz.jpg" width="200" height="18" /></strong></td>
            </tr>
        </table>
</form>
<?php
}
?>
__________________
Quitenme la vida pero no la bebida.
  #2 (permalink)  
Antiguo 03/03/2009, 13:17
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: creando treeview

Solo agrega la id que quieres abrir o cerrar directamente en la funcion que mandas llamar desde onclick:

onclick="javascript:btnAbre('abre$idCategoria');"
onclick="javascript:btnCierra('cierra$idCategoria' );"
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 03/03/2009, 13:45
Avatar de cesarpunk  
Fecha de Ingreso: enero-2008
Ubicación: Lima
Mensajes: 943
Antigüedad: 16 años, 3 meses
Puntos: 9
Respuesta: creando treeview

hola, gracias por tu respuesta.... pero veo que pones como parametro la variable $idcategoria, entonces como pondria mi funcion js? asi?:

function btnAbre(id) {
document.getElementById(id).style.display='block';
}
__________________
Quitenme la vida pero no la bebida.
  #4 (permalink)  
Antiguo 03/03/2009, 13:50
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: creando treeview

Exacto, envias el nombre completo del div y en js es lo unico que necesitas para abrir y cerrar.
__________________
- León, Guanajuato
- GV-Foto
  #5 (permalink)  
Antiguo 03/03/2009, 13:56
Avatar de cesarpunk  
Fecha de Ingreso: enero-2008
Ubicación: Lima
Mensajes: 943
Antigüedad: 16 años, 3 meses
Puntos: 9
Respuesta: creando treeview

hola denuevo, disculpa si me he equivocado.... puse un el js asi:

Código:
function btnAbre(id) {
document.getElementById(id).style.display='block';
}
y en el boton de abrir el div puse esto:


Código PHP:
<?php
echo '<img src="http://www.forosdelweb.com/images/treeItem1.gif" width="9" height="9" style="cursor:pointer;" onclick="javascript:btnAbre(\'$idcategoria\');" />';
?>

pero me sale el error "object required"
__________________
Quitenme la vida pero no la bebida.
  #6 (permalink)  
Antiguo 03/03/2009, 14:02
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: creando treeview

Es problema de comillas, para que una variable sea reemplazada dentro de una cadena tienes que "envolver" esa cadena entre comillas dobles:

Código php:
Ver original
  1. echo "<img src=\"http://www.forosdelweb.com/images/treeItem1.gif\" width=\"9\" height=\"9\" style=\"cursor:pointer;\" onclick=\"javascript:btnAbre('$idcategoria');\" />";

Ahora, tienes que enviar el id exacto del div o la funcion no podra reconocerlo y no abrira/cerrara
__________________
- León, Guanajuato
- GV-Foto
  #7 (permalink)  
Antiguo 03/03/2009, 14:33
Avatar de cesarpunk  
Fecha de Ingreso: enero-2008
Ubicación: Lima
Mensajes: 943
Antigüedad: 16 años, 3 meses
Puntos: 9
Respuesta: creando treeview

excelente, el problema era las comillas.... ahora disfruto de un treeview simple xd ... muchisismas gracias
__________________
Quitenme la vida pero no la bebida.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:54.