Red River Red River - 2 months ago 10
PHP Question

PHP - change last looping input type?

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

Answer Source

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>