D
D
Danil Sysoev2015-04-28 02:27:39
Programming
Danil Sysoev, 2015-04-28 02:27:39

How to fix the code so that the branch is displayed correctly?

Guys, hello.
I wondered how to write a script so that the referral tree is automatically displayed. (In a matrix project)
This is how the script turned out. It sorts through users, builds a tree, but there is a problem, it is displayed crookedly. Everything wraps around to the left, and the table shrinks. Tried to add empty fields - to no avail. I think the best option is to make a "matryoshka", but I can't figure out how. Can you please tell me how to configure the display of branches here?
By "Matryoshka" I mean this:

<div class="1">
    <div class="1.1">
        <div class="1.1.1"></div>
        <div class="1.1.2"></div>
    </div>
    <div class="1.2">
        <div class="1.2.1"></div>
        <div class="1.2.2"></div>
    </div>
</div>

<?php

include 'header.php';
function select_refs($parent)
{
  global $parents;
  global $lowlevel_users;
  
  $refs = array();
  if ($parent == '')
    $res = mysql_query("SELECT user FROM mlm_users WHERE parent=user");
  else
    $res = mysql_query("SELECT user FROM mlm_users WHERE parent='$parent' AND parent!=user");
  $count = 0;
  while ($row = mysql_fetch_assoc($res))
  {
    $refs[] = $row['user'];
    $parents[$row['user']] = array('user'=>$parent, 'width'=>0);
    $count++;
  }
  if (!$count)
    $lowlevel_users[] = $parent;
  return $refs;
}


function next_level($level)
{
  $nextlevel = array();
  foreach ($level as $u)
  {
    if ($refs = select_refs($u))
      $nextlevel = array_merge($nextlevel, $refs);
  }
  return $nextlevel;
}

mysql_connect('localhost', '', '');
mysql_select_db('');
mysql_query("SET NAMES 'utf8'");

$parents = array();
$levels = array();
$lowlevel_users = array();
$levels[0] = select_refs('');//Верхушка ветки. Для авторизованных вставить $user.
$level = 0;
while ($nextlevel = next_level($levels[$level]))
{
  $level++;
  $levels[$level] = $nextlevel;
}

foreach ($lowlevel_users as $u)
{
  $nextparent = $parents[$u]['user'];
  while ($nextparent != '')
  {
    $parents[$nextparent]['width']++;
    $nextparent = $parents[$nextparent]['user'];
  }
}

echo "<table>";
foreach ($levels as $level)
{
  echo "<tr>";
  foreach ($level as $u)
  {
    $width = $parents[$u]['width'];
    echo "<td" . ($width ? " colspan='$width'" : "") . ">$u</td>";
  }
  echo "</tr>";
}
echo "</table>";

?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-04-28
@Aleserche

At one time I used slides .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question