向用户显示数组结果 [英] Display array results to the user
问题描述
我在这里看到并向用户尝试了很多示例,但没有一个对我有用。
I have seen and tried a lot of examples here on display array results to the user but none of them has worked for me.
与我最接近的示例我想寻找的是这样的:
The closest to what I am looking for, I suppose, is this:
再次尝试,但无法正常工作。
Again, tried it but it doesn't work.
它不起作用-意味着页面空白。
It doesn't work - meaning the page is coming up blank.
当我尝试varDump(...)和print_r(...)时,我得到了结果,但我似乎无法向用户显示结果。
When I try varDump(...) and print_r(...), I get results but I can't seem to display the results to the user.
有人知道我需要对下面的代码进行哪些修改才能显示结果sourcename,sourceaddress1和income1?
Does anyone know what modifications I need to make to the code below to get to display the results of sourcename, sourceaddress1 and income1?
$rowIDs = $_POST['rowIDs'];
if (is_array($rowIDs) || is_object($rowIDs))
{
foreach ($rowIDs as $id) {
$sourcename1 = $_POST['sourcename1'. $id];
$sourceaddress1 = $_POST['sourceaddress1'. $id];
$income1 = $_POST['income1'. $id];
echo $id;
echo $sourcename;
echo $sourceaddress1;
echo $income1;
}
[rowIDs] => 1
[sourcename1] => Array
(
[0] => Jane Doe
)
[sourceaddress1] => Array
(
[0] => 123 Main Street
)
[income1] => Array
(
[0] => $89,000.00
)
'//标记,
<script id="row-template" type="text/x-handlebars-template">
<div>
<!--reseed attribute IDs in case of gap resulting from deletions -->
<input type="hidden" name="rowIDs[]" value="{{rowNumber}}" />
<div class="form-group">
<input type="text" name="sourcename1{{rowNumber}}" id="sourcename1{{rowNumber}}" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<div class="form-group">
<input type="text" name="sourceaddress1{{rowNumber}}" id="sourceaddress1{{rowNumber}}" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<div class="form-group">
<input type="text" style="width:250px;" class="form-control" name="income1{{rowNumber}}" id="income1{{rowNumber}}" value="<?php if(isset($_POST['spouseIncome{{rowNumber}}'])) echo $_POST['spouseIncome{{rowNumber}}'];?>" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<input id="Button{{rowNumber}}" type="button" rel="remove-row" value="Remove" />
</div>
</script>
<div id="addrow">
<div>
<!--reseed attribute IDs in case of gap resulting from deletions -->
<input type="hidden" name="rowIDs[]" value="{{rowNumber}}" />
<div class="form-group">
<label for="sourcename">Name</label><br>
<input type="text" name="sourcename1[]" id="sourcename1" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<div class="form-group">
<label for="sourceaddress1">Address</label><br>
<input type="text" name="sourceaddress1[]" id="sourceaddress1" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<div class="form-group">
<label for="income1">Income</label><br>
<input type="text" name="income1[]" id="income1" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
<input type="button" value="Add More" rel="add-row" />
</div>
</div>
</div><br><br>
推荐答案
$ _ POST ['rowids ']
是一个数字,而不是数组,因此 foreach($ _ POST ['rowids']为$ id)
是错误的。我猜这包含其他输入数组中元素的数量,因此应该为:
$_POST['rowids']
is a number, not an array, so foreach ($_POST['rowids'] as $id)
is wrong. I'm guessing this contains the number of elements in the other input arrays, so it should be:
for ($id = 0; $id < $rowIDs; $id++)
您也不应拥有 if(is_array($ postIDs)|| is_object($ postIDs))
,因为该代码应在输入为数字时运行。
You also shouldn't have if (is_array($postIDs) || is_object($postIDs))
, since the code should run when this input is a number.
其他输入是数组,因此您需要使用数组索引来访问它们,而不是字符串连接。
The other inputs are arrays, so you need to use array indexing to access them, not string concatenation.
$sourcename1 = $_POST['sourcename1'][$id];
和其他输入类似。
这篇关于向用户显示数组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!