从邻接列表类别创建html表

时间:2012-12-20 09:24:17

标签: php html

我正在使用此代码段从mySql db获取所有类别项并列出它们。正如您所猜测的那样,主要类别及其子类如A类及其子/后代,然后是B类及其子/后代等。

现在我喜欢为所有类别创建一个HTML表格 - 比如4个cols - 并将每个类别及其子项放入一个表格中,例如:

更新

    Html Table 1

  Category A    Child3      Child6   Child11
  Child1        Child4      Child7   Child12
  Child 1.1     Child5      Child8   Child13
  Child 1.2     Child 5.1   Child9
  Child2        Child 5.2   Child10

这是我用来获取类别和子项列表的代码段。它正确列出了所有类别和子类别。现在我想在HTML表格中显示带列的列表。我被困在这里。我怎么能做到这一点?

        $categoryArr = Array();

        while($categoryRow = mysql_fetch_array($category_query_result)){
            $categoryArr[] = array('parentid'=>$categoryRow['parent_id'],
            'childof'=>$categoryRow['child_of'],
            'categorytitle'=>$categoryRow['category_title'],
            'categoryfilename'=>$categoryRow['category_file_name'],
            'level'=>$categoryRow['level_num']);
        }



        function display_categories($Arr,$thisparent,$level){
            foreach ($Arr AS $key => $catNode ){
                if($catNode['childof']=="$thisparent"){
                    echo "" .$catNode['categorytitle'] ."<br>";

                      display_categories($Arr, $catNode['parentid'] ,$level+1);

                } 

            }

        }

        display_categories($categoryArr,0,1);

1 个答案:

答案 0 :(得分:0)

<强>更新

$categoryArr=array(array("parentid"=>1,"title"=>"Father 1","childof"=>0),array("parentid"=>2,"title"=>"Son 1.2","childof"=>1),array("parentid"=>3,"title"=>"Grandson 1.2.3","childof"=>2),array("parentid"=>4,"title"=>"Son 1.4","childof"=>1),array("parentid"=>5,"title"=>"Son 1.5","childof"=>1),array("parentid"=>6,"title"=>"Son 1.6","childof"=>1),array("parentid"=>7,"title"=>"Grandson 1.5.7","childof"=>5),array("parentid"=>8,"title"=>"Father 8","childof"=>0),array("parentid"=>9,"title"=>"Son 8.9","childof"=>8),array("parentid"=>10,"title"=>"Grandgrandson 1.2.3.10","childof"=>3));
function rebuild($data,$find)
{
    $cache=array_filter($data,create_function('$n','return $n["childof"]=='.$find.';'));
    $rt=array();
    foreach($cache as $node)
    {
        $rt[]=$node;
        $rt=array_merge($rt,rebuild($data,$node["parentid"]));
    }
    return $rt;
}
$cache=array_filter($categoryArr,function($n){return $n["childof"]==0;});
$base=array();
foreach($cache as $root)
{
    $base[]=array_merge(array($root),rebuild($categoryArr,$root["parentid"]));
}
$xml=new DOMDocument("1.0","UTF-8");
$xml->formatOutput=true; // just to debug
$root=$xml->createElement("root");
$xml->appendChild($root);
foreach($base as $list)
{
    $table=$xml->createElement("table");
    $root->appendChild($table);
    $rows=ceil(count($list)/4);//change 4 to whatever column you need
    foreach(range(1,$rows) as $idx)
    {
        $tr=$xml->createElement("tr");
        $table->appendChild($tr);
    }
    $cur=0;
    foreach($list as $node)
    {
        if($cur>=$rows) $cur=0;
        $td=$xml->createElement("td");
        $td->appendChild($xml->createTextNode($node["title"]));
        $table->getElementsByTagName("tr")->item($cur)->appendChild($td);
        $cur++;
    }
}
echo $xml->saveXML();

输出:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <table>
    <tr>
      <td>Father 1</td>
      <td>Grandson 1.2.3</td>
      <td>Son 1.4</td>
      <td>Grandson 1.5.7</td>
    </tr>
    <tr>
      <td>Son 1.2</td>
      <td>Grandgrandson 1.2.3.10</td>
      <td>Son 1.5</td>
      <td>Son 1.6</td>
    </tr>
  </table>
  <table>
    <tr>
      <td>Father 8</td>
      <td>Son 8.9</td>
    </tr>
  </table>
</root>

P.S。使用DOMDocument不是必须的;由于我们已经知道每个表有多少项,因此可以只计算每个表格单元格上的位置并直接输出它,但对我来说,DOMDocument更加懒惰:)


首次尝试:

可能不是一个优雅的解决方案,但它应该有效:

$categoryArr=array(array("parentid"=>1,"title"=>"Father","childof"=>0),array("parentid"=>2,"title"=>"Son 2","childof"=>1),array("parentid"=>3,"title"=>"Grandson 2.3","childof"=>2),array("parentid"=>4,"title"=>"Son 4","childof"=>1),array("parentid"=>5,"title"=>"Son 5","childof"=>1),array("parentid"=>6,"title"=>"Son 6","childof"=>1),array("parentid"=>7,"title"=>"Grandson 5.6","childof"=>5));
$xml=new DOMDocument("1.0","UTF-8");
$xml->formatOutput=true; // just to debug
$root=$xml->createElement("table");
$xml->appendChild($root);
foreach(range(1,ceil(count($categoryArr)/4)) as $idx) // change 4 to whatever column you need
{
    $tr=$xml->createElement("tr");
    $root->appendChild($tr);
}
function addNode(&$xml,$data,$currID,&$currRow)
{
    if($currRow>=$xml->getElementsByTagName("tr")->length) $currRow=0;
    $cache=array_filter($data,create_function('$n','return $n["parentid"]=='.$currID.';'));
    if(count($cache)==1)
    {
        $td=$xml->createElement("td");
        $cache=array_pop($cache);
        $td->appendChild($xml->createTextNode($cache["title"]));
        $xml->getElementsByTagName("tr")->item($currRow)->appendChild($td);
        $currRow+=1;
    }
    $cache=array_filter($data,create_function('$n','return $n["childof"]=='.$currID.";"));
    foreach($cache as $node)
    {
        addNode($xml,$data,$node["parentid"],$currRow);
    }
}
$cur=0;
addNode($xml,$categoryArr,0,$cur);
echo $xml->saveXML();

输出:

<?xml version="1.0" encoding="UTF-8"?>
<table>
  <tr>
    <td>Father</td>
    <td>Grandson 2.3</td>
    <td>Son 5</td>
    <td>Son 6</td>
  </tr>
  <tr>
    <td>Son 2</td>
    <td>Son 4</td>
    <td>Grandson 5.6</td>
  </tr>
</table>

没有“真实”数据需要测试,所以如果出现问题请告诉我。

相关问题