0

Please I need help fixing this. I am trying to fill a form using values of array but its messing up the form.

I want to display the af_rating in the input fields

<?php
    $af_skill = array(12, 9, 6, 3, 4, 5, 7);
    foreach ($af_skill as $k => $row_af):
    ?>
    <div class="form-group">
        <div class="col-sm-5">
            <?php
            $af_skill_rating = array(1, 2, 5, 4, 3, 2, 4);
            foreach ($af_skill_rating as $k2 => $af_rating):
                ?>
                <input type="number" class="form-control" name="af_rating-<?= $row_af[$k]; ?>" value="<?php echo $af_rating[$k2]; ?>" max="5" min="1" required> <br>
            <?php endforeach; ?>
        </div>
    </div>
<?php endforeach; ?>

it should look like this below image output image

3
  • 1
    $af_rating is a number, there's no [$k2] index for it. Commented Jun 29, 2017 at 14:09
  • Just remove the index from <?php echo $af_rating[$k2]; ?> Commented Jun 29, 2017 at 14:23
  • Yes, I have removed the index, but foreach loop is now 7*7 Commented Jun 29, 2017 at 14:27

3 Answers 3

1

You are using a simple array as PHP Associative Arrays.

You could use your code if you had something like this

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $key => $value){
   //first time in this loop $key = Peter 
   //and $value = "35"
}

I changed that in your code.

<?php
$af_skill = array("12" => "1", "9" => "2", "6" => "5", "3" => "4", "4" => "3", "5" => "2", "7" => "4");

foreach ($af_skill as $key => $value): ?>
    <div class="form-group">


        <div class="col-sm-5">


                <input type="number" class="form-control" name="af_rating-<?php echo $key ?>"
                       value="<?php echo $value; ?>" max="5" min="1" required> <br>

        </div>


    </div>

<?php endforeach; ?>

Also check your foreach because you are using foreach loop inside each other so that will produce 7*7 inputs

Sign up to request clarification or add additional context in comments.

3 Comments

I have used your code, but I think theres a conflict with the foreach loops. I just want for each af_skill, there should be an af_rating
Yes the foreach loops is 7*7 time and I need it just once
I replaced your arrays with one Associative array so you can try it now.
1

you have error in this line, you are using values as an array. So change this line

<input type="number" class="form-control" name="af_rating-<?= $row_af[$k]; ?>" value="<?php echo $af_rating[$k2]; ?>" max="5" min="1" required/>

with

<input type="number" class="form-control" name="af_rating-<?= $row_af[$k]; ?>" value="<?php echo $af_rating; ?>" max="5" min="1" required/>

3 Comments

I did that, the numbers have now displayed but it created 42 inputs instead of just 7
you have two for loops, for each outer loop the inner loop is repeating 7 times
Yes exactly, thats the problem I am facing. I want it resolved in the sense that for each af_skill, there should be a af_rating in the input field
1

You don't need to loop twice. With your setup, the keys from one array will match the keys from the second (af_skill[0] will correspond with af_skill_rating[0]). I submit the following code:

<?php
    $af_skill = array(12, 9, 6, 3, 4, 5, 7);
    $af_skill_rating = array(1, 2, 5, 4, 3, 2, 4);
    foreach ($af_skill as $k => $row_af):
?>
<div class="form-group">
    <div class="col-sm-5">
            <input type="number" class="form-control" name="af_rating-<?= $row_af; ?>" value="<?php echo $af_skill_rating[$k]; ?>" max="5" min="1" required> <br>
    </div>
</div>
<?php endforeach; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.