我推动自己更频繁地使用功能,并且在功能恢复时发生了变化。
这里使用的代码是从更大的代码区域中提取出来的,以便更容易在这里工作,所以更像是测试代码,然后做一些有意义的事情。所有这些测试代码与我的完整代码执行相同并复制我的问题。
在代码的最后,我有用于此插图的示例信息。代码正在构建一个动态表单。
function formInputTypeSelector使用用($ {' doorFinish'。$ finish} [$ series])构建的变量来标识在调用函数时作为变量的一部分传递的数组。所以在这种情况下,这就是$ doorFinishTrack [140]。
该函数然后调用标识的相应输入类型函数(此测试代码中的函数radioInputs)并返回该输入类型的表单。
这些功能都很好。我遇到的问题是,如果我回显我的函数返回(echo $ inputForm),我的表单最终会退出文档流程而不是我想要的div里面。我知道我应该使用 return (返回$ inputForm)修复此问题,但是当我执行表单时不再包含函数输出。
我已经尝试了我能想到的所有内容并在网上搜索了几个小时。我知道我可能在这里遇到了服务器端脚本与本地HTML及其时间和首先调用的问题。
我的问题是,如何正确地将函数radioInputs的输出返回到动态构建的表单中并将其保存在文档流中?
<?php
$category = '';
// 4.0 Track
$doorFinishTrack = array(
'140' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 0, 'Gold' => 0, 'Curvable' => 0, 'Recessed Track Channel' => 1
),
'220' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 0, 'Recessed Track Channel' => 1
),
'240' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 1, 'Recessed Track Channel' => 1
),
'440' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 1, 'Recessed Track Channel' => 1
),
'540' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 1, 'Recessed Track Channel' => 1
),
'640' => array('Aluminum' => 1, 'Dark Bronze' => 0, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 1, 'Recessed Track Channel' => 1
),
'740' => array('Aluminum' => 1, 'Dark Bronze' => 0, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 1, 'Recessed Track Channel' => 1
),
'2100' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 0, 'Recessed Track Channel' => 0
),
'3300' => array('Aluminum' => 1, 'Dark Bronze' => 0, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 0, 'Recessed Track Channel' => 0, 'Heavy Duty Aluminum Ceiling Guard with Seperate Track' => 1
)
);
//parse the call and pas on the specific input type function
function formInputTypeSelector ($inputArray, $series, $finish, $inputType, $arg1, $arg2) {
// Call input type function
foreach ($inputArray as $key => $value) {
$inputName = $finish . $key;
$inputLabel = $key;
$inputFunction = $inputType . 'Inputs';
$inputFunction($inputName, $inputLabel, $arg1, $arg2);
}
} //end formInputTypeSelector
// Specific input types called by formInputTypeSelector
function radioInputs ($inputName, $inputLabel) {
$inputForm = '';
$inputForm .= '<input type="radio" name="' . $inputName . '" id="' . $inputName . '">' . $inputLabel . '</br>';
echo $inputForm;
} //end radioInputs
// other types of input functions
// ... code here
//dynamically build the form based on the array called and the input form type desired.
$finish = 'Track';
$series = 140;
$category .= 'Where the form starts</div> <!-- End tracksGroup -->';
$category .= '<div class="clearfix"></div>';
$category .= '<div><!-- inputs -->';
$category .= formInputTypeSelector((${'doorFinish' . $finish}[$series]), $series, $finish, 'radio', '', '');
$category .= '</div><!-- end inputs -->';
$category .= 'Where the form ends<div class="clearfix"></div>';
echo $category;
?>
答案 0 :(得分:4)
你应该让radioInputs()返回你正在回复的内容并在你想要的地方回显它。
function radioInputs ($inputName, $inputLabel) {
$inputForm = '';
$inputForm .= '<input type="radio" name="' . $inputName . '" id="' . $inputName . '">' . $inputLabel . '</br>';
return $inputForm;
} //end radioInputs
答案 1 :(得分:0)
我已经解决了这个问题。令人惊讶的是,一夜的睡眠和新鲜的眼睛会看到一个问题。
首先,我完全不知道我正在回显函数radioInputs的值,该函数是原始调用函数formInputTypeSelector调用的函数。我想要的是通过formInputTypeSelector中的foreach运行radioInputs的结果。我更改了函数radioInput以返回$ inputForm以使该部分理顺。
第二,确定上述内容后,我发现每次函数formInputTypeSelector中的foreach调用函数radioInputs时,我都没有收集循环结果。我添加了变量$ outputForm并且每次通过foreach循环连接函数radioInputs返回值。
第三,我没有通过formInputTypeSelector输出任何内容,因此在foreach循环完成运行后添加了$ outputForm变量的返回。
最后,我将动态表单变量$ category连接到函数formInputTypeSelector的调用,最终所有项目都被收集到$ category后,代码echo的$ category和我的函数调用现在保持在HTML文档流程一切都很好。
这是最终的测试代码,按我想要的方式工作。
<?php
$category = '';
$inputForm = '';
// 4.0 Track
$doorFinishTrack = array(
'140' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 0, 'Gold' => 0, 'Curvable' => 0, 'Recessed Track Channel' => 1
),
'220' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 0, 'Recessed Track Channel' => 1
),
'240' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 1, 'Recessed Track Channel' => 1
),
'440' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 1, 'Recessed Track Channel' => 1
),
'540' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 1, 'Recessed Track Channel' => 1
),
'640' => array('Aluminum' => 1, 'Dark Bronze' => 0, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 1, 'Recessed Track Channel' => 1
),
'740' => array('Aluminum' => 1, 'Dark Bronze' => 0, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 1, 'Recessed Track Channel' => 1
),
'2100' => array('Aluminum' => 1, 'Dark Bronze' => 1, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 0, 'Recessed Track Channel' => 0
),
'3300' => array('Aluminum' => 1, 'Dark Bronze' => 0, 'Clear' => 1, 'Gold' => 0, 'Curvable' => 0, 'Recessed Track Channel' => 0, 'Heavy Duty Aluminum Ceiling Guard with Seperate Track' => 1
)
);
//parse the call and pass on the specific input type function
function formInputTypeSelector ($inputArray, $series, $finish, $inputType, $arg1, $arg2) {
// Call input type function
$outputForm = '';
foreach ($inputArray as $key => $value) {
$inputName = $finish . $key;
$inputLabel = $key;
$inputFunction = $inputType . 'Inputs';
$outputForm .= $inputFunction($inputName, $inputLabel, $arg1, $arg2);
// echo $outputForm;
}
return $outputForm;
} //end formInputTypeSelector
// Specific input types called by formInputTypeSelector
function radioInputs ($inputName, $inputLabel) {
$inputForm = '';
$inputForm .= '<input type="radio" name="' . $inputName . '" id="' . $inputName . '">' . $inputLabel . '</br>';
return $inputForm;
} //end radioInputs
// other types of input functions
// ... code here
//dynamically build the form based on the array called and the input form type desired.
$finish = 'Track';
$series = 140;
$category .= 'Where the form starts</div> <!-- End tracksGroup -->';
$category .= '<div class="clearfix"></div>';
$category .= '<div><!-- inputs -->';
$category .= formInputTypeSelector((${'doorFinish' . $finish}[$series]), $series, $finish, 'radio', '', '');
$category .= '</div><!-- end inputs -->';
$category .= 'Where the form ends<div class="clearfix"></div>';
echo $category;
?>