Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/11/2013, 09:24
Avatar de andyvedder
andyvedder
 
Fecha de Ingreso: julio-2013
Ubicación: Todas Partes
Mensajes: 27
Antigüedad: 10 años, 9 meses
Puntos: 1
Pregunta recorrido de arbol

buenos días amigos el foro. amigos tengo una estructura en forma de árbol en mi base de datos, necesito hacer una búsqueda e ir contando los hijos de cada nodo raíz, esto lo tengo que hacer para cinco niveles es decir cada nodo padre debo contar sus hijo, nietos, bisnietos hasta llegar a la 5ta generación. l lo hace perfecto hasta el 2 nivel pero los nietos se entrelazan como puedo diferenciar el conteo e identificar cada hijo con su padre. para ponerlo de otro modo, en el 2 nivel los nietos tienen 2 padres. he aquí mi código espero puedan ayudarme. aquí mi codigo:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Consultores</title>
<link rel="stylesheet" href="../css/styletable2.css" />
</head>
<body>
<div id="content">
<h1>Consutores Asociados a su Código</h1>

<?php
include("../Data_Base/conectar_bd.php");
conectar_bd();

session_start();

$CodPad= $_SESSION['$CodPad'];


$sql="select nivel from cliente where codigo='$CodPad'";

$ejsql=pg_query($sql);
if($row = pg_fetch_array($ejsql))
{
//Guardo los datos de la BD en las variables de $cantAcum
$cantAcum = $row["nivel"];
$Nivel1 = $cantAcum+1;
$Nivel2 = $cantAcum+2;
$Nivel3 = $cantAcum+3;
$Nivel4 = $cantAcum+4;
$Nivel5 = $cantAcum+5;
$var2 = $cantAcum+5;
}

echo '<table cellpadding="0" cellspacing="0" width="100%">';
echo '<thead><tr><td>Nivel 1</td><td>Nivel 2</td><td>Nivel 3</td><td>Nivel 4</td><td>Nivel 5</td></thead>';


$sql = "SELECT nivel FROM cliente Where nivel='$Nivel1' AND codigo_padre_p='$CodPad'"; // sentencia sql
$result = pg_query($sql);
$numero1 = pg_num_rows($result);
echo '<tr><td>'.$numero1.'</td>';


$sql = "SELECT nivel FROM cliente Where nivel='$Nivel2'"; // sentencia sql
$result = pg_query($sql);
$numero2 = pg_num_rows($result);
echo '<td>'.$numero2.'</td>';

$sql = "SELECT nivel FROM cliente Where nivel='$Nivel3'"; // sentencia sql
$result = pg_query($sql);
$numero3 = pg_num_rows($result);
echo '<td>'.$numero3.'</td>';

$sql = "SELECT nivel FROM cliente Where nivel='$Nivel4'"; // sentencia sql
$result = pg_query($sql);
$numero4 = pg_num_rows($result);
echo '<td>'.$numero4.'</td>';

$sql = "SELECT nivel FROM cliente Where nivel='$Nivel5'"; // sentencia sql
$result = pg_query($sql);
$numero5 = pg_num_rows($result);
echo '<td>'.$numero5.'</td>';
echo "</table>";

?>

</div>
</body>
</html>