在按标签排序的列表中显示标记的项目

时间:2013-04-10 18:00:14

标签: php arrays codeigniter tags relationship

我的任务包含这种格式的标记:

Array 
(
   [0] => stdClass Object
      (
         [task_id] => 10,
         [task_text] => Mow and fertilize,
         [tags] => Array
            (
               [0] => stdClass Object
                  (
                     [tag_id] => 1,
                     [tag_text] => House,
                     [tt_tag_id] => 1, //from relational table, tt = task_tag
                     [tt_task_id] => 10,
                     [tt_id] => 20, //auto id from relational table
                     [tt_order] => 0
                  ),
               [1] => stdClass Object
                  (
                     [tag_id] => 2,
                     [tag_text] => Yard,
                     [task_tag_id] => 2, 
                     [task_task_id] => 10,
                     [tt_id] => 21,
                     [tt_order] => 1
                  )
               [2] => stdClass Object
                  (
                     [tag_id] => 5,
                     [tag_text] => Lawn,
                     [task_tag_id] => 5, 
                     [task_task_id] => 10,
                     [tt_id] => 22,
                     [tt_order] => 2
                  )
             )
       )
   [1] => stdClass Object
      (
         [task_id] => 11,
         [task_text] => Unclog the sink drain,
         [tags] => Array
            (
               [0] => stdClass Object
                  (
                     [tag_id] => 1,
                     [tag_text] => House,
                     [task_tag_id] => 1, 
                     [task_task_id] => 11
                     [tt_id] => 30,
                     [tt_order] => 0
                  ),
               [1] => stdClass Object
                  (
                     [tag_id] => 3,
                     [tag_text] => Kitchen,
                     [task_tag_id] => 3, 
                     [task_task_id] => 11,
                     [tt_id] => 31,
                     [tt_order] => 1
                  )
             )
       )
   [2] => stdClass Object
      (
         [task_id] => 12,
         [task_text] => Purchase new microwave,
         [tags] => Array
            (
               [0] => stdClass Object
                  (
                     [tag_id] => 4,
                     [tag_text] => Apartment 1,
                     [task_tag_id] => 4, 
                     [task_task_id] => 12
                     [tt_id] => 40,
                     [tt_order] => 0
                  ),
               [1] => stdClass Object
                  (
                     [tag_id] => 3,
                     [tag_text] => Kitchen,
                     [task_tag_id] => 3, 
                     [task_task_id] => 12,
                     [tt_id] => 41,
                     [tt_order] => 1
                  )
             )
       )
)

我希望能够通过这样的标签对任务进行排序:

Array 
(
 [0] => stdClass Object
      (
         [tag_id] => 1,
         [task_text] => House,
         [child_tags] => Array
            (
               [0] => stdClass Object
                  (
                     [tag_id] => 2,
                     [tag_text] => Yard,
                     [task_tag_id] => 2, 
                     [task_task_id] => 10,
                     [tt_id] => 21,
                     [child_tags] => Array
                        (
                           [1] => stdClass Object
                              (
                                  [tag_id] => 5,
                                  [tag_text] => Lawn,
                                  [task_tag_id] => 5, 
                                  [task_task_id] => 10,
                                  [tt_id] => 22,
                                  [task_id] => 10,
                                  [task_text] => Mow and fertilize
                              )
                        ),
               [1] => stdClass Object
                 (
                    [tag_id] => 3,
                    [tag_text] => Kitchen,
                    [task_tag_id] => 3, 
                    [task_task_id] => 11,
                    [tt_id] => 31,
                    [task_id] => 11,
                    [task_text] => Unclog the sink drain,
                 )
             )
       )
 [1] => stdClass Object
      (
         [tag_id] => 4,
         [tag_text] => Apartment 1,
         [child_tags] => Array
            (
               [0] => stdClass Object
                  (
                     [tag_id] => 2,
                     [tag_text] => Kitchen,
                     [task_tag_id] => 2, 
                     [task_task_id] => 10,
                     [tt_id] => 21,
                     [task_id] => 12,
                     [task_text] => Purchase new microwave
                  )
            )
       )
)

最终我要拍的是:

<ul>
   <lh>House</lh>
   <li>
      <ul>
         <lh>Kitchen</lh>
         <li>Unclog the sink drain</li>
      </ul>
   </li>
   <li>
      <ul>
         <lh>Yard</lh>
          <li>
             <ul>
               <lh>Lawn</lh>
               <li>Mow and fertilize</li>
             </ul>
          </li>
      </ul>
   </li>
</ul>
<ul>
   <lh>Apartment 1</lh>
   <li>
      <ul>
         <lh>Kitchen</lh>
         <li>Purchase new microwave</li>
      </ul>
   </li>
</ul>

数据库正是您的想象。一个任务表和一个带有n2n关系表的标签表。

似乎没有一种简单的方法可以循环执行任务和标记,以便以这种方式对它们进行排序。我应该尝试不同的方法吗?这真傻吗?

1 个答案:

答案 0 :(得分:0)

如果您可以在层次结构中构建数组(即拉动所有标记并将任务指定为每个标记的嵌套数组),类似于最终结构的未排序版本,则可以尝试两个递归循环:< / p>

  1. 取1级物品,排序()此
  2. 对于每个1级项目,将其打印出来并对Level 2进行排序
  3. 重复直到最低级别