0

专家有没有自动增加+1的选项category_level

例如:$cond_cat=" where category_level='1' and ID='$sleads' ";$cond_cat=" where category_level='2' and parent_id ='$sleads' ";

我的代码如下。

 <?php
    include('classes/global.php');
    $CT=new Product();
    $AdminObj=new Admin();
    $sleads=$_REQUEST['sleads'];
    $cond_cat=" where category_level='1' and ID='$sleads' ";
    $catResult_cond=$CT->getCategory($cond_cat);
    $cond_cat=" where category_level='2' and parent_id ='$sleads' ";
    $subCatResult_cond=$CT->getCategory($cond_cat);

    ?>
4

1 回答 1

0

您可能想尝试递归地获取子类别作为您所在子类别的子类别。

<?php

  function getSubCategories($level) {
    // you should obviously get the $sleads variable in here, for this to work.
    $cond_cat = "where category_level='%d' and ID='$sleads' ";
    $subcategories = $CT->getCategory(sprintf($cond_cat, $level));
    if (count($subcategories) > 0) {
      foreach($subcategories as $subcategory) {
        $subcategory->subcategories = $CT->getCategory(sprintf($cond_cat, $level+1));
      }
    }
    return $subcategories;
  }

  $i = 1;
  $categories = getSubCategories($i);

?>

这会让你得到类似的东西:

// $categories         // first level
// foreach($categories as $cat) {
  // $cat->subcategories // second level categories
  // foreach($cat->subcategories as $subcategory) {
    // $subcategory->subcategories // third level
  // }
// }
// and so on

请考虑到我假设您的$CT->getCategory函数返回一个类别记录数组。
如果不是这种情况(考虑到您还在查询中添加了 ID=""),您还应该更改该函数,或使用返回多个类别记录的函数。

于 2013-09-26T06:28:35.733 回答