Ver Mensaje Individual
  #12 (permalink)  
Antiguo 20/02/2008, 15:22
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años, 9 meses
Puntos: 834
Re: Array PHP a javascript o DOM

Ahí va:
Código PHP:
<?php
$algo
=array(
    array(
'cod_empresa','num_identificacion','razon_social','representante','tipo_empresa','telefono','direccion'),
    array(
1,805029800,'Empresa 1','Juliana','Propia','475-89-96','Cr. 9 No. 9-54'),
    array(
2,14528964,'Empresa 2','Diego','Propia','4516789','Cll 1 No. 13-27')
);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>test</title>
<script>
function crear(){
algo=new Function('return <?php echo json_encode($algo?>')();
/*
alert(obj[1][3]); //devuelve Juliana
alert(obj[2][2]);//devuelve Empresa 2
*/
//creamos la tabla
t=document.createElement('table');
//le ponemos, si queremos, algún atributo
t.border='1';
//creamos un elemento tbody para que funcione en todos los navegadores DOM
tb=document.createElement('tbody');
//recorro los índices primarios del objeto
for (i in algo){
//creamos un renglón para cada índice primario del objeto
    tr=document.createElement('tr');
//recorremos los índices secundarios y creamos celdas en cada paso
    for (j in algo[i]){
        td=document.createElement('td');
//agregamos texto a la celda
        td.appendChild(document.createTextNode(algo[i][j]));
//agregamos la celda al renglón
        tr.appendChild(td);
    }
//agregamos el renglón al cuerpo de la tabla
    tb.appendChild(tr);
}
//agregamos el cuerpo a la tabla
t.appendChild(tb);
//agregamos la tabla al body de la página
document.getElementsByTagName('body')[0].appendChild(t);
}
//llamamos a la función que crea la tabla cuando se carga la página
window.onload=crear;
</script>

</head>

<body>
</body>
</html>