很多站长朋友们都不太清楚php无限分类评论,今天小编就来给大家整理php无限分类评论,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 PHP如何通过传引用的思想实现无限分类(代码 2、 php无限分类怎么弄 3、 如何使用PHP实现无限级分类 4、 php 无限分类 5、 php无限分类,请高手帮忙 PHP如何通过传引用的思想实现无限分类(代码在我的Simpla中,用到了无限分类,使用了PHP的传引用思想实现无限分类的方法,可以完美展示类似这样的分类模式。
id pid name
1 0 四川
2 0 重庆
3 1 成都
4
1 绵阳
5 3 高新区
代码如下所示:
/**
* 数组变成无限级分类--传引用思想
* @param array $items
* @return array
*/
public static function get_tree($orig) {
//解决下标不是1开始的问题
$items = array();
foreach ($orig as $key => $value) {
$items[$value[‘id‘]] = $value;
}
//开始组装
$tree = array();
foreach ($items as $key => $item) {
if ($item[‘pid‘] == 0) { //为0,则为1级分类
$tree[] = $items[$key];
} else {
if (isset($items[$item[‘pid‘]])) { //存在值则为二级分类
$items[$item[‘pid‘]][‘child‘][] = $items[$key]; //传引用直接赋值与改变
} else { //至少三级分类
//由于是传引用思想,这里将不会有值
$tree[] = $items[$key];
}
}
}
return $tree;
}
以上内容很简单吧,如有错误或者更好的方法,希望可以相互交流。谢谢。!
php无限分类怎么弄这样来做。。 一个表分三个字段 ID 名 《父类ID》
如果为顶级类则父类ID 设为 0
如果为子类则 把子类的父类ID 设为所在《父类ID》即可。。 如果为第三层子类 则把父类ID设为第二层的 父类ID
依次!! 这样就可以无限了。。 当然。。 如果要优化的话可能就需要加一些别的字段来帮助优化了
如何使用PHP实现无限级分类你还在用浪费时间又浪费内存的递归遍历无限极分类吗,看了该篇文章,我觉得你应该换换了。
这是我在OSChina上看到的一段非常精简的PHP无限极分类生成树方法,巧在引用,整理分享了。
复制代码代码如下:
function generateTree($items){
$tree = array();
foreach($items as $item){
if(isset($items[$item['pid']])){
$items[$item['pid']]['son'][] = $items[$item['id']];
}else{
$tree[] = $items[$item['id']];
}
}
return $tree;
}
$items = array(
1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'),
2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'),
4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'),
5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
);
print_r(generateTree($items));
可以看到下面打印的结果:
复制代码代码如下:
Array
(
[0] => Array
(
[id] => 1
[pid] => 0
[name] => 安徽省
[son] => Array
(
[0] => Array
(
[id] => 3
[pid] => 1
[name] => 合肥市
[son] => Array
(
[0] => Array
(
[id] => 4
[pid] => 3
[name] => 长丰县
)
)
)
[1] => Array
(
[id] => 5
[pid] => 1
[name] => 安庆市
)
)
)
[1] => Array
(
[id] => 2
[pid] => 0
[name] => 浙江省
)
)
上面生成树方法还可以精简到5行:
复制代码代码如下:
function generateTree($items){
foreach($items as $item)
$items[$item['pid']]['son'][$item['id']] = $items[$item['id']];
return isset($items[0]['son']) ? $items[0]['son'] : array();
}
上面这种无限极分类数据树形结构化的方法值得借鉴。但是我觉得这段代码实际用途并不明显啊,你想取出格式化的树形数据还是要递归啊:
复制代码代码如下:
/**
* 如何取数据格式化的树形数据
*/
$tree = generateTree($items);
function getTreeData($tree){
foreach($tree as $t){
echo $t['name'].'<br>';
if(isset($t['son'])){
getTreeData($t['son']);
}
}
}
getTreeData($tree);
php 无限分类可以在表A中增加一个字段,该字段关联表B的SID,或者可以新建一张关联表C (ID,NEWSID,SID) 起到关联作用
select * from b2b_news as a,b2b_news_sort as b where a.sid= b.sid
是关联查询吗 ,如果有其他条件的话,在后面加and吧
php无限分类,请高手帮忙建个类 不更好吗?
比如
<?php
class cate{
private $cid;
private $parent_id;
private $descr;
private $sum;
public function __construct(){
$this->init();
}
function init(){
global $db;
$sql = $db->query('SELECT * FROM category');
$i = 0;
while($temp = $db->fetch_array($sql)){
$this->cid[$i] = $temp['cid'];
$this->parent_id[$i] = $temp['parent_id'];
$this->descr[$i] = $temp['descr'];
$i++;
}
$this->sum = $i;
}
public function getCate($type,$value=0){
$j = 0;
$temp;
for($i = 0; $i < $this->sum; $i++){
if($this->{$type}[$i] == $value){
$temp['cid'][$j] = $this->cid[$i];
$temp['parent_id'][$j] = $this->parent_id[$i];
$temp['descr'][$j] = $this->descr[$i];
$j++;
}
}
return $temp;
}
public function getAllCate(){
$temp['cid'] = $this->cid;
$temp['parent_id'] = $this->parent_id;
$temp['descr'] = $this->descr;
return $temp;
}
public function getSum(){
return $this->sum;
}
}
?>
这个类调用global的数据库连接类的对象$db的
我写的数据库连接的类可能跟你的不一样,你改改
并查询所有category表里的记录
里面getCate()方法,是这样用的,比如你要查parent_id为2的所有记录
$c = new cate();
$t = $c->getCate("parent_id",2);
之后$t就是个2维数组
取第一个的是
$t['cid'][0];
$t['parent_id'][0];
$t['descr'][0];
关于php无限分类评论的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php无限分类评论 php无限分类评论怎么写的详细内容...