html作为php变量未正确呈现
有以下方法的html作为php变量未正确呈现,php,html,var,Php,Html,Var,有以下方法的 public function printDropdownTree($tree, $r = 0, $p = null) { foreach ($tree as $i => $t) { $dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) . ' '; printf("\t<option value='%d'>%s%s</option&g
public function printDropdownTree($tree, $r = 0, $p = null) {
foreach ($tree as $i => $t) {
$dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) . ' ';
printf("\t<option value='%d'>%s%s</option>\n", $t['id'], $dash, $t['name']);
if ($t['parent'] == $p) {
// reset $r
$r = 0;
}
if (isset($t['_children'])) {
$this->printDropdownTree($t['_children'], ++$r, $t['parent']);
}
}
}
公共函数printDropdownTree($tree,$r=0,$p=null){
foreach($i=>t的树){
$dash=($t['parent']==0)?“”:str_重复('-',$r)。“”;
printf(“\t%s%s\n”、$t['id']、$dash、$t['name']);
如果($t['parent']=$p){
//重置$r
$r=0;
}
如果(isset($t[“U儿童])){
$this->printDropdownTree($t['u children',+$r,$t['parent']);
}
}
}
它打印出嵌套的选择选项,并且工作正常。但是我想把结果作为变量返回,我正试图实现这样的效果
public function printDropdownTree($tree, $r = 0, $p = null) {
$html = "";
foreach ($tree as $i => $t) {
$dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) . ' ';
$html .= '<option value="'.$t['id'].'">'.$dash.''.$t['name'].'</option>';
if ($t['parent'] == $p) {
// reset $r
$r = 0;
}
if (isset($t['_children'])) {
$this->printDropdownTree($t['_children'], ++$r, $t['parent']);
}
}
return $html;
}
公共函数printDropdownTree($tree,$r=0,$p=null){
$html=“”;
foreach($i=>t的树){
$dash=($t['parent']==0)?“”:str_重复('-',$r)。“”;
$html.=''.$dash.'.$t['name'].';
如果($t['parent']=$p){
//重置$r
$r=0;
}
如果(isset($t[“U儿童])){
$this->printDropdownTree($t['u children',+$r,$t['parent']);
}
}
返回$html;
}
但是,
$dash
不会被呈现为一个简单的解决方案:您必须获取函数递归调用的结果
public function printDropdownTree($tree, $r = 0, $p = null) {
$html = "";
foreach ($tree as $i => $t) {
$dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) . ' ';
$html .= '<option value="'.$t['id'].'">'.$dash.''.$t['name'].'</option>';
if ($t['parent'] == $p) {
// reset $r
$r = 0;
}
if (isset($t['_children'])) {
$html .= $this->printDropdownTree($t['_children'], ++$r, $t['parent']);
}
}
return $html;
}
公共函数printDropdownTree($tree,$r=0,$p=null){
$html=“”;
foreach($i=>t的树){
$dash=($t['parent']==0)?“”:str_重复('-',$r)。“”;
$html.=''.$dash.'.$t['name'].';
如果($t['parent']=$p){
//重置$r
$r=0;
}
如果(isset($t[“U儿童])){
$html.=$this->printDropdownTree($t[''u children',++$r,$t['parent']);
}
}
返回$html;
}
我认为返回必须超出foreach循环。此外,您还可以将printf
替换为sprintf
。哦,是的,很抱歉在posted中出现打字错误。此外,如果您没有获取此函数递归调用的返回值,则必须执行$html.=$this->printDropdownTree($t[''u children']..
。是的,你说得对。谢谢。你能把答案贴出来吗?我愿意接受