0

I want to create a form submit, I've created a form using looping, but I need to change last loop input type from number to select option

there's some way how to do that with this code?

form:

<div class="form-group">
    <?php
    $stmt2 = $pgn2->readAll();
    while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)){
        extract($row2);
        ?>
        <label for="ik"><?php echo $nama_kriteria; ?></label>
        <input type="hidden" name="ik[]" id="ik" value=<?php echo $id_kriteria ?>>
        <input type="number" class="form-control" id="nn" name="nn[]" min="1" max="100"
        <!-- I just want to change last loop for id="nn" -->
        placeholder="1 - 100">
    <?php
    }
    ?>
</div>

here's a result form:

form

any idea would be appreciate

7
  • I don't quite understand what you are wanting here, could you try to explain a little more for me please? Commented Aug 18, 2017 at 10:15
  • 2
    maybe by count the number of result and a condition to change if it's the last one Commented Aug 18, 2017 at 10:16
  • 1
    Possible duplicate of PHP while loop find last row Commented Aug 18, 2017 at 10:17
  • @LewisBrowne my bad for wrong comment sign, i want to change input type of last loop, and i've tried to find it on internet, but not lucky yet Commented Aug 18, 2017 at 10:18
  • @MacBooc thanks for the suggest, ill try it, Commented Aug 18, 2017 at 10:21

2 Answers 2

2

You can check if it's the last row by counting. :)

<div class="form-group">
    <?php
    $stmt2 = $pgn2->readAll();
    $count = 1;
    $last = count($stmt2);

    while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)){
        extract($row2);
        ?>
        <label for="ik"><?php echo $nama_kriteria; ?></label>
        <input type="hidden" name="ik[]" id="ik" value=<?php echo $id_kriteria ?>>
        <?php if($last === $count): ?>
             // Add select input <--
        <?php else: ?>
            <input type="number" class="form-control" id="nn" name="nn[]" min="1" max="100" placeholder="1 - 100">
        <?php endif;
        $count++;
    }
    ?>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

get the count for the total array and check last row

<div class="form-group">
    <?php
    $stmt2 = $pgn2->readAll();
    $total_count = count($stmt2);
    $loop_cnt = 0; 
    while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)){
        extract($row2);
        $loop_cnt = $loop_cnt + 1;
        ?>
        <label for="ik"><?php echo $nama_kriteria; ?></label>
        <?php if($loop_cnt == $total_count): ?>
           //selec code here
       <?php else: ?>
           //input code here 
       <?php endif; ?>
    <?php
    }
    ?>
</div>

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.