Home  >  Article  >  Backend Development  >  Two methods and techniques for implementing stepless recursive classification in PHP

Two methods and techniques for implementing stepless recursive classification in PHP

黄舟
黄舟Original
2017-03-04 11:28:131584browse

This article mainly introduces two methods of implementing stepless recursive classification in PHP, which has a good reference value. Let’s take a look at it with the editor.

Without further ado, please look at the code:

/**
* 无级递归分类 TP框架
* @param int $assortPid 要查询分类的父级id
* @param mixed $tag  上下级分类之间的分隔符
* @return string $tree 返回的分类树型结构结果 
*
*/
function recursiveAssort($assortPid, $tag = '')
{ 
 $assort = M('goods_class')->where("class_pid = $assortPid")->field('class_id, class_name')->select();
 foreach ($assort as $value) {
 $tree .= &#39;<option value="&#39; . $value[&#39;class_id&#39;] . &#39;">&#39; . $tag . $value[&#39;class_name&#39;] . &#39;</option>&#39;;
 $tree .= recursiveAssort($value[&#39;class_id&#39;], $tag . &#39; &#39;);
 }
 return $tree;
}

/**
 * 利用php的引用传递 CI框架
 *
 */
 public function get_access()
 {
 $access = array();
 $field = &#39;id, pid, method, name, description&#39;;
 $q_access = $this->db->select($field)->get(&#39;access&#39;);
 $q_result = $q_access->result_array();
 if (!empty($q_result)) {
  $items = array();
  foreach ($q_result as $value) {
  $items[$value[&#39;id&#39;]] = $value;
  }
  foreach ($items as $key => $item) {
  if ($item[&#39;pid&#39;] == 0) {
   $access[] = &$items[$key];
  } else {
   $items[$item[&#39;pid&#39;]][&#39;sub_access&#39;][] = &$items[$key];
  }
  }
 }
 return $access;
 }

The above is the content of the two methods and techniques for implementing stepless recursive classification in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn