du2229 2012-12-04 07:08
浏览 5

怎么写条件?

$tree = taxonomy_get_tree($vid);
print "<li>";                    // 1 line 
foreach ($tree as $term ) {      // 2 lines
   $diffdepth=0;
   if ($term->depth > $depth) {
      print "<ul class='haschild'><li>";
      $depth = $term->depth;
   }

the above is the original code, now I want according to $term->depth > $depth make a condition to output.( print "<li>"; ) this line.

namely,

if ($term->depth > $depth) { 
   echo '<li class="parent">'; 
} 
else { 
   print "<li>"; 
}

but $term->depth can use after foreach loop, but i want to use it at 1 line, how do i do?

  • 写回答

3条回答 默认 最新

  • drddx3115 2012-12-04 07:20
    关注

    Instead of printing in-line, assign your desired output to variables and then send it to the browser after the logic is complete.

    $tree = taxonomy_get_tree($vid);
    $parent = ""; // 1 line 
    $child  = "";
    foreach ($tree as $term) { // 2 line
        $diffdepth=0;
        if ($term->depth > $depth) {
            $parent = "<li class='parent'>";
            $child .= "<ul class='haschild'><li>";
            $depth = $term->depth;
        } else {
            $parent = "<li>";
        }
    }
    echo $parent . $child;
    

    Please note you'll need to finish this up by adding all the applicable </li>s and whatnot, but this should get you started.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序