简体   繁体   中英

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:

How to display array result

Again, tried it but it doesn't work.

It doesn't work - meaning the page is coming up blank.

When I try varDump(...) and print_r(...), I get results but I can't seem to display the results to the user.

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
    )

'//markup,

<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'] 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++)

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];

and similar for the other inputs.

sourcename1, sourceaddress1 and income1 are arrays containing one item. For example, $sourcename[0] would return Jane Doe .

For more information regarding arrays please read the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM