Как сделать структурный вид из MySQL и PHP и jQuery

я должен показать treeview своих категорий, сохраненных в моей mysql базе данных.

Таблица базы данных:

таблица: кошки:

столбцы: идентификатор, имя, родительская проблема находится в php части:

//function to build tree menu from db table test1
function tree_set($index)
{
    global $menu;
    $q=mysql_query("select * from cats where parent='$index'");
    if(!mysql_num_rows($q))
        return;
    $menu .= '
    '."\n"; while($arr=mysql_fetch_assoc($q)) { $menu .= '
  • '; $menu .= ''.$arr['name'].'';//you can add another output there $menu .=tree_set("".$arr['id'].""); $menu .= '
  • '."\n"; } $menu.= '
'."\n"; return $menu; } //variable $menu must be defined before the function call $menu = '
    '."\n"; $menu .= tree_set(0); $menu .= '
'; echo $menu;

я даже спросил на этом форуме: http://forums.tizag.com/showthread.php?p=60649

проблема находится в php части моих кодов, которые я упомянул. я не могу показать подменю, я имею в виду, действительно я не знаю, как показать подменю

есть ли шанс про php кодера, помогающего мне здесь?

5
задан Mac Taylor 18 May 2010 в 17:39
поделиться

1 ответ

Похоже, вы отправляете повторяющиеся данные в переменную меню, которой не должно быть.

Я бы изменил вашу функцию так:

function tree_set($index)
{
    //global $menu; Remove this.
    $q=mysql_query("select * from cats where parent='$index'");
    if(mysql_num_rows($q) === 0)
    {
        return;
    }

    // User $tree instead of the $menu global as this way there shouldn't be any data duplication
    $tree = $index > 0 ? '<ul>' : ''; // If we are on index 0 then we don't need the enclosing ul
    while($arr=mysql_fetch_assoc($q))
    {
        $subFileCount=mysql_query("select * from cats where parent='{$arr['id']}'");
        if(mysql_num_rows($subFileCount) > 0)
        {
            $class = 'folder';
        }
        else
        {
            $class = 'file';
        }

        $tree .= '<li>';
        $tree .= '<span class="'.$class.'">'.$arr['name'].'</span>';
        $tree .=tree_set("".$arr['id']."");
        $tree .= '</li>'."\n";
    }
    $tree .= $index > 0 ? '</ul>' : ''; // If we are on index 0 then we don't need the enclosing ul

    return $tree;
}

//variable $menu must be defined before the function call
$menu = '....<ul id="browser" class="filetree">'."\n";
$menu .= tree_set(0);
$menu .= '</ul>';
echo $menu;

обновлено на основе комментариев к вопросу

3
ответ дан 15 December 2019 в 06:19
поделиться
Другие вопросы по тегам:

Похожие вопросы: