猿问

数据库$query = "SELECT * FROM tree "; 怎么把模拟数据改成读取数据库?

php代码如下:
//模拟数据库
$data=array(
array('id'=>1,'pid'=>0,'name'=>'栏目一'),
array('id'=>2,'pid'=>1,'name'=>'子栏目'),
array('id'=>3,'pid'=>1,'name'=>'二级栏目一'),
array('id'=>4,'pid'=>3,'name'=>'三级栏目一'),
array('id'=>5,'pid'=>4,'name'=>'四级栏目一'),
array('id'=>6,'pid'=>0,'name'=>'一级栏目一'),
array('id'=>7,'pid'=>2,'name'=>'一级栏目二'),
array('id'=>8,'pid'=>1,'name'=>'二级栏目一'),
array('id'=>9,'pid'=>2,'name'=>'三级栏目一'),
array('id'=>10,'pid'=>3,'name'=>'四级栏目一'),

);
//转换数据
$tree_data=array();
foreach ($data as $key=>$value){
$tree_data[$value['id']]=array(
'id'=>$value['id'],
'parentid'=>$value['pid'],
'name'=>$value['name']
);
}
/**
* 输出树形结构
*/
$tree=new Tree();
$tree->init($tree_data);
echo "<div class='tree well'>";
echo $tree->get_treeview(0, $str);
echo "</div>";

繁华开满天机
浏览 109回答 1
1回答

海绵宝宝撒

$query = "SELECT id,pid,name FROM tree ";$tree_data=array();if ($res=mysql_query($query)) while ($row=mysql_fetch_array($res)) $tree_data[]=$row;
随时随地看视频慕课网APP
我要回答