minimihi wrote:Here is a list
[code=php]<?php
$database = "test";
$table = "chain_list";
$parent = "a001";
$con = mysql_connect("localhost", "root", "");
mysql_select_db($database, $con);
$query = "SELECT
t1.agent_code AS lev1,
t2.agent_code AS lev2,
t3.agent_code AS lev3,
t4.agent_code AS lev4,
t5.agent_code AS lev5,
t6.agent_code AS lev6,
t7.agent_code AS lev7
FROM
$table AS t1
LEFT JOIN $table AS t2
ON t2.introducer_code = t1.agent_code AND
t2.agent_code != t1.agent_code
LEFT JOIN $table AS t3 ON
t3.introducer_code = t2.agent_code
LEFT JOIN $table AS t4 ON
t4.introducer_code = t3.agent_code
LEFT JOIN $table AS t5 ON
t5.introducer_code = t4.agent_code
LEFT JOIN $table AS t6 ON
t6.introducer_code = t5.agent_code
LEFT JOIN $table AS t7 ON
t7.introducer_code = t6.agent_code
WHERE
t1.agent_code = '$parent'";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
$md_arr = m_dimension_struct($rows);
print_arr($md_arr);
mysql_close($con);
//=========================================//
function print_arr($array, $count=0) {
global $table;
$i=0;
$tab ='';
while($i != $count) {
$i++;
$tab .= "--";
}
foreach($array as $key => $value){
$query = "SELECT *
FROM
$table
WHERE
agent_code = '$key'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if(is_array($value)){
echo $tab.$key."/".$row['introducer_code']."/".$row['rank']."<br />";
$count++;
print_arr($value, $count);
$count--;
} else {
echo $tab.$key."/".$row['introducer_code']."/".$row['rank']."<br />";
}
}
$count--;
}
function m_dimension_struct($array) {
$hier_struct = array();
foreach($array as $i => $element) {
$single = null;
foreach($element as $key => $value) {
$single = add_dimension($single, $array[$i][$key]);
}
$hier_struct[] = $single;
}
$result = multimerge($hier_struct[0], $hier_struct[1]);
$result = multimerge($result, $hier_struct[2]);
// hide errors =D
ob_clean();
return $result;
}
function add_dimension($array, $dimension) {
if(empty($dimension))
return $array;
if(!is_array($array))
return array($dimension => $array);
foreach($array as $key => $value) {
$array[$key] = add_dimension($value, $dimension);
}
return $array;
}
function multimerge($array1, $array2) {
if (is_array($array2) && count($array2)) {
foreach ($array2 as $key => $value) {
if (is_array($value) && count($value)) {
$array1[$key] = multimerge($array1[$key], $value);
} else {
$array1[$key] = $value;
}
}
} else {
$array1 = $array2;
}
return $array1;
}
/* function array_depth($array) {
$max_indentation = 1;
$array_str = print_r($array, true);
$lines = explode("\n", $array_str);
foreach ($lines as $line) {
$indentation = (strlen($line) - strlen(ltrim($line))) / 4;
if ($indentation > $max_indentation) {
$max_indentation = $indentation;
}
}
return ceil(($max_indentation - 1) / 2) + 1;
} */
?>
thanks a lot you saved my day