反转php递归函数的结果

时间:2015-02-24 16:36:22

标签: php

我已经写了一个php函数来显示类别,所有作品都可以找到,但回声显示像这样(椅子/家具/产品),但我需要它反转看起来像这样(产品/家具/椅子) )

    function sitemap($id) { 
    $query_rsCategoryId = "SELECT * FROM categories 
    WHERE category_id = '".$id."'";
    $rsCategoryId = mysql_query($query_rsCategoryId, $connection);
    $row_rsCategoryId = mysql_fetch_assoc($rsCategoryId);
    $parent = $row_rsCategoryId['category_parent'];

    echo '<li><span class="divider">/</span>
    <a href="products.php?category_id='.$row_rsCategoryId['category_id'].'">
    '.$row_rsCategoryId['category_name_en'].' </a>
    </li>';

    if ($parent == 0) {
    exit;
    } else {
    return sitemap($parent);
    }
}

2 个答案:

答案 0 :(得分:1)

稍微修改你的代码......

function sitemap($id) { 
   $query_rsCategoryId = "SELECT * FROM categories 
   WHERE category_id = '".$id."'";
   $rsCategoryId = mysql_query($query_rsCategoryId, $connection);
   $row_rsCategoryId = mysql_fetch_assoc($rsCategoryId);
   $parent = $row_rsCategoryId['category_parent'];


   if ($parent != 0) {
      sitemap($parent);
      echo '<li><span class="divider">/</span>';
   } else {
      echo '<li>';
   }

    echo '<a href="products.php?category_id='.$row_rsCategoryId['category_id'].'">
   '.$row_rsCategoryId['category_name_en'].' </a>
   </li>';
}

答案 1 :(得分:0)

尝试在查询中使用desc命令

SELECT * FROM categories 
WHERE category_id = '".$id."' ORDER BY category_name_en DESC